Output Formatting (JSON, XML, Markdown)
Force the model to return parseable structures using JSON mode, XML tags, or strict Markdown to make outputs machine-readable.
Why Format Matters
Agents pipe model output into code. Free-form prose breaks parsers. Structured output (JSON, XML, etc.) is mandatory for any production agent.
JSON Mode
OpenAI and most providers offer a JSON mode that guarantees parseable output:
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=messages,
response_format={'type': 'json_object'},
)
import json
data = json.loads(response.choices[0].message.content)All lessons in this course
- Zero-shot, Few-shot and Chain-of-Thought
- System vs User vs Assistant Roles
- Output Formatting (JSON, XML, Markdown)
- Avoiding Prompt Injection in Inputs