Structured Output
JSON, XML, YAML
The Problem: AI often returns free-form text, but your code needs structured data like JSON or specific formats. How can we get consistent, parseable output?
The Solution: Fill Out the Form
Structured Output prompting specifies the exact format AI should use for its response. It's like giving someone a form to fill out instead of asking them to write a free-form letter. Essential for Prompt Chaining where each step needs parseable data, and few-shot examples help the model learn the exact format.
Think of it like filling out a structured form:
- 1. Define schema: Specify fields: name, email, department
- 2. Provide example: Show the exact JSON structure expected
- 3. Request data: "Extract contact info from this email"
- 4. Get structured result: Clean JSON ready to parse
Where Is This Used?
- API Responses: Generating JSON for web applications
- Data Extraction: Pulling structured info from unstructured text
- Form Generation: Creating structured records from descriptions
- Code Generation: Specific function signatures or class structures
Fun Fact: Modern APIs like OpenAI and Anthropic now support "function calling" which guarantees valid JSON output! This eliminates parsing errors and makes AI much more reliable for production use.
Try It Yourself!
Use the interactive example below to see how specifying output format leads to consistent, machine-readable responses.
Structured Output
Get data in a predictable format for programmatic processing
Popular output formats:
- • Hard to parse programmatically
- • Format may vary
- • Requires NLP to extract
Prompt Template
Analyze the following text and return the result ONLY in JSON format.
Text: "{TEXT}"
Use the following structure:
{
"field1": "type and description",
"field2": "type and description"
}
Important: return ONLY valid JSON without additional text.response = client.chat.completions.create(
model="gpt-4",
response_format={ "type": "json_object" },
messages=[
{"role": "system", "content": "Return JSON only"},
{"role": "user", "content": "Analyze this review..."}
]
)response = client.messages.create(
model="claude-3-opus",
tools=[{
"name": "extract_data",
"input_schema": {
"type": "object",
"properties": {
"sentiment": {"enum": ["positive", "negative"]},
"score": {"type": "number"}
}
}
}],
tool_choice={"type": "tool", "name": "extract_data"}
)- • Always specify "return ONLY JSON without additional text"
- • Use JSON Schema for client-side validation
- • Provide an example of expected structure in the prompt
- • Use response_format or tool_use for guaranteed JSON
- • Handle parsing errors — model may not follow instructions
| Method | Reliability | Flexibility | Complexity |
|---|---|---|---|
| Prompt Instructions | ⭐⭐ | ⭐⭐⭐ | Low |
| JSON Mode | ⭐⭐⭐ | ⭐⭐ | Low |
| Function Calling | ⭐⭐⭐ | ⭐⭐⭐ | Medium |
| Structured Outputs API | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | Medium |
Try it yourself
Interactive demo of this technique
Extract contact information from text and return it as JSON
Contact information:
- Name: Alex Johnson
- Company: DataFlow Inc.
- Email: a.johnson@dataflow.com
- Phone: +1 (415) 555-0123
- Address: 42 Main Street
{
"name": "Alex Johnson",
"company": "DataFlow Inc.",
"email": "a.johnson@dataflow.com",
"phone": "+1 (415) 555-0123",
"address": "42 Main Street"
}
Without specifying a schema, the model returns a dashed list — readable for humans but not machine-parsable. A JSON schema + "only JSON" instruction produce machine-readable output that can be used directly in code.
Create a free account to solve challenges
3 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path