Protocol

Structured Output

Why Structured Output Is Needed

A model's default output is free-form text — humans can understand it, but programs struggle to reliably continue processing.

What you need is:

  • Writing results to a database
  • Direct rendering by frontend components
  • Triggering the next step in an automation flow
  • Checking fields one by one

These all require programs to "understand" the model's output. Structured Output transforms the model's output from "a paragraph of text" into "structured data."


What Is Structured Output

One-line definition: Structured Output is the ability to have models return results in a predefined structure, such as JSON, object fields, or fixed enumerated values, rather than free-form text.

Two common implementations:

Tool Calling / Function Calling (recommended): let the model call a "fake tool," where the tool's parameters themselves are structured data.

JSON Mode: directly request the model to answer in JSON format.

This relies on Function Schema to define the output structure.


How to Do It: When to Use

Scenarios suited for Structured Output:

  • Data extraction: extracting structured data from unstructured text (resume parsing, invoice recognition)
  • API responses: Agent's output needs to be further processed by programs
  • Multi-field responses: answering multiple fields at once (title, description, tags)
  • Classification and routing: judging intent category, priority

Common Pitfalls

  • JSON Mode doesn't guarantee format correctness: models may output incomplete JSON, requiring validation and retry
  • Tool Calling is more reliable: because tool parameters have Schema constraints, format is usually more stable
  • More complex structures lead to more errors: error rates increase with deeper nesting levels
  • Correct format doesn't mean correct semantics: field names may all be right, but field values could still be fabricated

Remember this: Structured Output transforms the model's output from "a paragraph of text" into "data" — programs can read it to continue processing.

Related terms: Function Schema · Tool Calling