Why Structured Output is Needed

In practical applications, AI text output needs to be parsed and used by downstream systems. Structured output (such as JSON, XML) enables automated machine processing. Whether building APIs, populating databases, or triggering automated workflows, structured output serves as the bridge connecting AI with business systems.

JSON Mode Prompt

Through carefully designed prompts, you can make the model output formatted JSON data:

You are an information extraction assistant. Please extract key information from the following user message and output it in JSON format.

Output format:
{
  "intent": "user intent",
  "entities": [
    {"name": "entity name", "type": "entity type"}
  ],
  "sentiment": "positive/negative/neutral",
  "summary": "one-sentence summary"
}

User message: I want to book a flight from Beijing to Shanghai tomorrow, economy class, window seat.

JSON Schema Constraints

For deeper control, use JSON Schema to define the output structure:

Please output your analysis results according to the following JSON Schema:

{
  "type": "object",
  "properties": {
    "analysis": {
      "type": "object",
      "properties": {
        "overall_score": {"type": "number", "minimum": 0, "maximum": 100},
        "strengths": {"type": "array", "items": {"type": "string"}},
        "weaknesses": {"type": "array", "items": {"type": "string"}},
        "recommendations": {"type": "array", "items": {
          "type": "object",
          "properties": {
            "action": {"type": "string"},
            "priority": {"type": "string", "enum": ["high", "medium", "low"]},
            "expected_impact": {"type": "string"}
          },
          "required": ["action", "priority"]
        }}
      },
      "required": ["overall_score", "strengths", "weaknesses"]
    }
  },
  "required": ["analysis"]
}

Using Function Calling for Structured Output

Modern LLM APIs (such as OpenAI, DeepSeek) provide Function Calling capability, which is the best way to achieve structured output:

import json
from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": "You are a data analysis assistant"},
        {"role": "user", "content": "Analyze Q3 sales data: total revenue 5 million, up 15% year-over-year"}
    ],
    functions=[{
        "name": "generate_report",
        "description": "Generate sales data analysis report",
        "parameters": {
            "type": "object",
            "properties": {
                "total_revenue": {"type": "number"},
                "growth_rate": {"type": "number"},
                "analysis": {"type": "string"},
                "recommendations": {
                    "type": "array",
                    "items": {"type": "string"}
                }
            },
            "required": ["total_revenue", "growth_rate", "analysis"]
        }
    }],
    function_call={"name": "generate_report"}
)

result = json.loads(
    response.choices[0].message.function_call.arguments
)
print(json.dumps(result, indent=2, ensure_ascii=False))

Best Practices for Structured Output

  • Prefer Function Calling: This is the most reliable way for structured output
  • Add fallback handling: Model output may not conform to the format; need JSON parsing exception handling
  • Use enum constraints: For fields with limited options, use enum to restrict possible values
  • Add validation layer: Use Pydantic or JSON Schema to validate output on the backend
  • Provide output examples: Include examples of the desired output format in the prompt

Common Issues and Solutions

Issue 1: Output is not valid JSON. Solution: Use regex to extract JSON blocks, or use Function Calling. Issue 2: Missing fields in output. Solution: Set required fields and provide default values in code. Issue 3: Model "creatively" adds extra fields. Solution: Clearly state "only output the specified fields, do not add extra content".