Sarghaby Araghatta
Home / Knowledge Base / Structured output / JSON mode / function schemas
Prompting & context · AI Engineering

Structured output / JSON mode / function schemas

Constraining the model to emit valid JSON matching a schema. Delivered via JSON mode, tool/function calling, or strict "structured outputs" that guarantee schema conformance.

Input JSON SchemaConstrained DecodingValid JSON OutputTool/Function CallingProgrammatic Consumer
Flow of Structured Output Generation

How it works

Structured output begins with a JSON Schema, which defines the expected structure and types of the data. When you pass this schema to the model, it uses it as a guide to ensure that the output conforms to the specified format. In strict modes, the model employs Constrained decoding, which actively masks any tokens that would lead to invalid JSON. This means that if the model tries to generate a string that doesn't fit the schema, it simply won't be allowed to output it, ensuring that the final result is always valid. For example, if your schema requires an integer and the model attempts to output a string, it will be blocked from doing so, effectively enforcing the rules at generation time. This mechanism is crucial because it prevents errors that could arise from malformed JSON, which would break integration with downstream systems.

Function calling builds on this structured output by allowing the model to select a specific tool or API based on the context of the conversation. Once a tool is chosen, the model fills in the required arguments according to the schema. This process is tightly integrated with the JSON Schema, ensuring that the arguments passed to the tool are valid and match the expected types. For instance, if a function requires a date and a string, the model will only generate outputs that conform to those types. This tight coupling between the schema and the output not only makes the integration smoother but also reduces the likelihood of runtime errors due to type mismatches or invalid data.

The combination of these mechanisms creates a powerful system where the model can produce structured outputs that are both syntactically and semantically correct. The model's ability to generate valid JSON while adhering to a predefined schema means that developers can trust the outputs for further processing or integration into applications. This structured approach is particularly useful when data integrity is paramount, such as in financial applications or API responses.

  • Define JSON Schema for output.
  • Use constrained decoding to enforce schema.
  • Generate valid JSON output.
  • Use tool/function calling for integration.
  • Ensure programmatic consumers receive structured data.

The software-engineering analogue

You can think of structured output as similar to a typed API contract or a serializer in traditional software engineering. Just like a contract specifies what data types are expected, the JSON Schema outlines the structure and types of the data the model should produce. This ensures that when a function is called, the data received is in the correct format. However, the key difference here is that while traditional contracts enforce types at compile time, structured output enforces them at generation time through token masking. This means that the model can still generate outputs that are probabilistically correct but not semantically valid, which introduces an element of uncertainty in the values themselves.

Why not the obvious alternative

A common mistake is to rely solely on a prompt that instructs the model to 'please return JSON.' This naive approach often leads to issues, as the model might append trailing prose, include markdown fences, or accidentally introduce syntax errors like misplaced commas. Such problems can cause parsers to fail, resulting in a frustrating experience where the output is unusable. In contrast, schema-constrained decoding actively prevents these syntactic failures by ensuring that only valid JSON is generated. This eliminates the need for complex retry loops to correct errors, streamlining the entire process.

Without schema enforcement, developers face the constant challenge of handling malformed outputs. This not only increases development time but also leads to potential runtime errors in production systems. By using structured output with a defined schema, you remove these risks entirely, allowing for smoother integration and more reliable data handling. The model's ability to generate valid JSON from the outset means that developers can focus on building features rather than troubleshooting output errors.

When to use which

Use structured or strict output when you are working with any programmatic consumer, such as an API client or an agent that requires precise data formats. This ensures that the data received is always valid and ready for processing. On the other hand, free text is more suitable for scenarios involving chat, drafting, or explanations where the exact structure isn't as critical. In these cases, you can still validate values even when the overall structure is guaranteed, allowing for flexibility in communication while maintaining data integrity.

Common Misunderstanding Many assume that JSON output is always valid; however, without schema enforcement, it can still contain errors that break integration.

In production: when it breaks

The scenario The model emits invalid JSON 25% of the time, causing data processing failures.
Invalid JSON Output RateSchema Validation FailureFunction Call ErrorsTool Configuration CheckInput Data Quality CheckUpdate Function SchemaRevise Tool Settings
Debugging JSON output issues

Why it happens. The failure often occurs due to mismatched data types in the structured output, where the model generates strings instead of integers, or includes additional fields not defined in the schema. This can happen if the model has not been properly fine-tuned to adhere to the specified JSON schema or if the input prompts are ambiguous, leading to incorrect output formats.

  • Check the logs for specific JSON validation errors and their frequency.
  • Review the input prompts used during the API calls to identify any ambiguities.
  • Test the model with controlled inputs that are known to produce valid JSON outputs.
The call To fix the issue, refine the input prompts to be more explicit about the expected output structure. This may reduce model flexibility and require more training data to ensure schema adherence. Monitor the rate of JSON validation errors and the types of errors logged to catch any regressions early.

What it delivers

The primary benefit of structured output is reliable integration. By ensuring that the LLM produces valid, typed data, it effectively becomes a callable function that developers can rely on for consistent and accurate results. This reliability opens up new possibilities for building applications that require precise data handling, making it easier to integrate AI capabilities into existing systems.