Every Way to Get Structured Output from LLMs
boundaryml.com
Every Way to Get Structured Output from LLMs
1–10 of 89 posts
Re: Every Way to Get Structured Output from LLMs
#2Re: Every Way to Get Structured Output from LLMs
#3Re: Every Way to Get Structured Output from LLMs
#4https://docs.anthropic.com/en/docs/build-with-claude/prompt-...
Give it examples and instructions in tags, ask it to output in tags, and force it to return early by completing for it. (Assistant:).
When you think about it, it makes a lot of sense. Even if the output is chatty, parsing it is easy because you're not looking for } which may or may not match an opening {, instead you're looking for which is much easier to parse for.
Re: Every Way to Get Structured Output from LLMs
#5Re: Every Way to Get Structured Output from LLMs
#6Structured output should not be assumed is limited to JSON. Claude performs very well with XML, as it has been trained with it, so there's no real need to put in extra work. Not XML as in conformant, schema-compliant XML, just XML as delimiters. https://docs.anthropic.com/en/docs/build-with-claude/prompt-... Give it examples and instructions in tags, ask it to output in tags, and force it to return early by completin…
> XML is a many more tokens (much slower + $$$ for complex schemas)
> regardless of if you're looking for } or its really a matter of "does your parser work". when you have three tokens that need to be correct "", the odds of a mistake are higher, instead of when you just need "}".
That said, the parser is much easier to write, we're actually considering supporting XML in BAML. have you found any reductions of accuracy?
Also, not sure if you saw this, but apparently Claude doesn't actually prefer XML, it just happens to work well with it. Was recently new info for myself as well. https://x.com/alexalbert__/status/1778550859598807178 (devrel @ Anthropic)
Re: Every Way to Get Structured Output from LLMs
#7Re: Every Way to Get Structured Output from LLMs
#8We just use openai function calls (tools) and then use Pydantic to verify the JSON. When validation fails we try the prompt again.
We instead had to write a parser to catch small mistakes like missing commas, quotes etc, and parse content even if there's things like reasoning in the response, like here: https://www.promptfiddle.com/Chain-of-Thought-KcSBh
Re: Every Way to Get Structured Output from LLMs
#9Why do OpenAI/Anthropic/... not support constraining token generation? I'd imagine producing valid structured output would be at the top of their feature request lists.
Re: Every Way to Get Structured Output from LLMs
#10Hey everyone! One of the creators of BAML here! Appreciate sharing this post. For anyone interested in playing around with an interactive version of BAML online, check it out here: https://www.promptfiddle.com