This constrains the output of the LLM to some grammar. However, why not use a grammar that does not have invalid sentences, and from there convert to any grammar that you want?
Sampling and structured outputs in LLMs
11–20 of 99 posts
Re: Sampling and structured outputs in LLMs
#12Re: Sampling and structured outputs in LLMs
#13It's still baffling to me that the various API providers don't let us upload our custom grammars. It would enable so many use cases, like HTML generation for example, at essentially no cost on their part.
Re: Sampling and structured outputs in LLMs
#14Hmm, so if structured output affects the quality of the response maybe it's better to convert the output to a structured format as a post-processing step?
It's a tradeoff between getting "good enough" performance w/ guided/constrained generation and using 2x calls to do the same task. Sometimes it works, sometimes it's better to have a separate model. One good case of 2 calls is the "code merging" thing, where you "chat" with a model giving it a source file + some instruction, and if it replies with something like ... //unchanged code here ... some new code ... //the r…
Re: Sampling and structured outputs in LLMs
#15This post dives into that "black magic" layer, especially in the context of emerging thinking models and tools like Ollama or GPT-OSS. It’s a thoughtful look at why sampling, formatting, and standardization are not just implementation details, but core to the future of working with LLMs.
Re: Sampling and structured outputs in LLMs
#16This constrains the output of the LLM to some grammar. However, why not use a grammar that does not have invalid sentences, and from there convert to any grammar that you want?
What if the converted version is not in the wanted syntax?
With a 2nd pass you basically "condition" it on the text right above, hoping to get better semantic understanding.
Re: Sampling and structured outputs in LLMs
#17If the current position in the structure only has one possibility (like a comma, bracket, etc.) do you just force that as the next token and continue?
Re: Sampling and structured outputs in LLMs
#18Earlier quoted context omitted.
What if the converted version is not in the wanted syntax?
Constrained generation guarantees syntax. It does not guarantee semantic correctness tho. Imagine you want a json object with "hp" and "damage". If you use a grammar, the model will be forced to output a json object with those two values. But it's not guaranteed to get sensible values. With a 2nd pass you basically "condition" it on the text right above, hoping to get better semantic understanding.
Re: Sampling and structured outputs in LLMs
#19It's still baffling to me that the various API providers don't let us upload our custom grammars. It would enable so many use cases, like HTML generation for example, at essentially no cost on their part.
As to why providers don't give you a nice API, maybe it's hard to implement efficiently.
It's not too bad if inference is happening token by token and reverting to the CPU every time, but I understand high performance LLM inference uses speculative decoding, with a smaller model guessing multiple tokens in advance and the main model doing verification. Doing grammar constraints across multiple tokens is tougher, there's an exponential number of states that need precomputing.
So you'd need to think about putting the parser automaton onto the GPU/TPU and use it during inference without needing to stall a pipeline by going back CPU.
And then you start thinking about how big that automaton is going to be. How many states, pushdown stack. You're basically taking code from the API call and running it on your hardware. There's dragons here, around fair use, denial of service etc.
Re: Sampling and structured outputs in LLMs
#20When doing structured sampling, why is the token sampled, checked against the grammar, and resampled if it's wrong by applying the mask ? Why wouldn't we apply the mask immediately for the first sampling? Is this an optimization somehow, is masking expensive?
> is masking expensive?
It's not expensive per-se; A single element-wise multiplication of the output vector.
The real "expense" is that you need to prepare masks for every element of your grammar as they are expensive to recompute as needed; LLM tokens do not cleanly map onto elements of your grammar. (Consider JSON: LLM tokens often combine various special characters such as curly braces, colons, and quotes.)
This isn't that hard to compute, it's just more work to implement.