Re:Structured Outputs in LLMs
by cyptus on Tuesday September 23, 2025 @08:48AM
(#45346294)
What if the converted version is not in the wanted syntax?
[ Parent
| on HN ]
Re:Structured Outputs in LLMs
by NitpickLawyer on Tuesday September 23, 2025 @09:18AM
(#45346617)
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.
I'm pretty sure the grammar is generated from the Json schema, it doesn't just constrain json syntax, it constraints on the schema (including enums and such). The schema is also given to the model (at least in openai) you can put instructions in the json schema as well that will be taken into account.
Re:Structured Outputs in LLMs
by NitpickLawyer on Tuesday September 23, 2025 @10:03AM
(#45347202)
Perhaps I worded that poorly. What I mean by semantic correctness is that the model could output nonsensical values for some things. Say in a game, "normal" health is ~100hp and the model creates a wizard with 50hp but then a mouse with 10000hp. So you're guaranteed to get a parsable json object (syntactically correct) but what the values are in that json is not guaranteed to make sense in the given context.
Re:Structured Outputs in LLMs
by viralpraxis on Tuesday September 23, 2025 @11:46AM
(#45348684)
You can specify `minimum` and `maximum` property for these fields. So this schema
{
"$id": "https://example.com/test.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"hp": {
"type": "integer",
"description": "HP",
"minimum": 1,
"maximum": 15
}
}
}
is converted to this BNF-like representation: hp ::= ([1-9] | "1" [0-5]) space
hp-kv ::= "\"hp\"" space ":" space hp
root ::= "{" space (hp-kv )? "}" space
space ::= | " " | "\n"{1,2} [ \t]{0,20}
Re:Structured Outputs in LLMs
by michaelgiba on Tuesday September 23, 2025 @02:11PM
(#45350761)
For anyone curious here is an interactive write up about this http://michaelgiba.com/grammar-based/index.html
Re:Structured Outputs in LLMs
by lubujackson on Tuesday September 23, 2025 @10:59AM
(#45348022)
I find it does pretty well given a reasonable prompt and (especially) well-named keys/JSON structure. So if you had boss.mouse.hp you would get higher HP than random_enemies.mouse.hp, or better: enemies.level_1.mouse.hp.