Understanding JSON Schema in LLM Function Calling & Structured Outputs
Traditional text generation from LLMs is non-deterministic, making automated extraction prone to syntax errors, missing fields, or hallucinated types. Modern AI providers (OpenAI, Anthropic, Google) solve this via Constrained Decoding—guiding the model's token sampling logits at every generation step using a formal grammar compiled from a JSON Schema. When constrained decoding is enabled, the LLM is mathematically incapable of generating tokens that violate the specified JSON Schema (RFC Draft-07 / 2020-12). This enables 100% reliable downstream parsing in microservices, database inserts, and automated workflows.
// Example: RFC Draft-07 Strict JSON Schema for OpenAI Structured Outputs
{
"name": "extract_user_profile",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": { "type": "string", "description": "Full name of user" },
"role": { "type": "string", "enum": ["admin", "editor", "viewer"] },
"age": { "type": ["number", "null"] }
},
"required": ["name", "role", "age"],
"additionalProperties": false
}
}