Stop regex-ing the LLM's prose
Ask an LLM a question and it hands you back prose. Lovely to read, miserable to program against. You wanted the one number buried in the middle of it, and now you're writing a regular expression to fish a word out of three well-written paragraphs that phrase themselves slightly differently every single time you run them. There's a much better way, and it's the difference between forever…
When you ask an LLM for a response, you receive a paragraph of text. While it's easy to read, it's difficult to program against. You may be searching for a specific word buried within paragraphs that vary each time you use the model. The solution lies in asking the LLM for structured data rather than paragraphs of text. This approach eliminates the need for regular expressions and enables you to build upon the LLM's responses more effectively.
To accomplish this, the LLM is asked to return a value with specific fields, such as severity and fix. For example, a Go struct can be defined with these fields. A framework can then generate a JSON schema from this struct and send it to the model as the required response format. When the model sends back a response, it is automatically unmarshalled into the struct, giving you typed, ready-to-use data.
The key to this approach is that the struct acts as the schema, ensuring that the model's response matches the expected structure. This prevents the need to constantly update schemas or handle mismatches between the schema and the actual response. By using this method, an LLM becomes a reliable source of structured data that can be consumed by programs, leading to more robust and maintainable code.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.