0Pricing
AI Agents · Lesson

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.

Output Formatting (JSON, XML, Markdown) is a free AI Agents lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Structured Outputs (Strict Schema)

OpenAI Structured Outputs takes a JSON Schema and guarantees the output matches:

schema = {
    'name': 'extract_person',
    'schema': {
        'type': 'object',
        'properties': {
            'name': {'type': 'string'},
            'age': {'type': 'integer'}
        },
        'required': ['name', 'age'],
        'additionalProperties': False
    },
    'strict': True
}

response = client.chat.completions.create(
    model='gpt-4o-2024-08-06',
    messages=messages,
    response_format={'type': 'json_schema', 'json_schema': schema}
)

XML Tags (Anthropic-Style)

Anthropic recommends XML tags as the most reliable formatting for Claude:

system = '''
Return your answer wrapped in XML tags:

<reasoning>Your step-by-step thinking</reasoning>
<answer>The final answer</answer>
'''

# Parse with a simple regex or BeautifulSoup
import re
ans = re.search(r'<answer>(.*?)</answer>', text, re.S).group(1)

Markdown for Humans

Use Markdown when the output goes directly to a human (chat UI, docs).

Avoid Markdown when piping to code — Markdown is hard to parse robustly.

Pick the Right Format Per Consumer

  • JSON — going into your code
  • XML — going into your code, especially Claude
  • Markdown — going to a human UI
  • Plain text — going to another LLM

Show, Do Not Just Tell

Always include a literal example of the desired output in your prompt:

system = '''
Return a JSON object like:
{
  "action": "reply",
  "content": "Hi there!",
  "confidence": 0.95
}
'''
print(system.strip())

Force the First Token

For Anthropic, pre-fill the assistant turn with { or [ to force JSON output:

messages = [
    {'role': 'user', 'content': 'Return JSON with name and age.'},
    {'role': 'assistant', 'content': '{'}
]
# Output will start at '{ "name": ...' guaranteed.
for m in messages:
    print(f"{m['role']}: {m['content']}")
print('Output will start at \'{ "name": ...\' guaranteed.')

Handle Trailing Junk

Even with JSON mode, sometimes models add a "Sure! Here is the JSON:" prefix. Robust parsers:

  1. Look for first { or [
  2. Bracket-count to find the matching close
  3. Parse only that slice

Repair Prompts

If parsing fails, send the broken output back to the model and ask for a fix:

repair_prompt = f'''
The previous output failed to parse with error: {error}.
Return ONLY a valid JSON object matching the schema. No prose.

Previous output:
{bad_output}
'''

Pydantic for Validation

Validate the parsed dict with a Pydantic model — type-check fields, raise on missing.

from pydantic import BaseModel

class Person(BaseModel):
    name: str
    age: int

person = Person.model_validate_json(response_text)
print(person.name, person.age)

Best Format for Code

You are piping output into Python. Which format is most reliable?

Recap

Three rules:

  1. Pick a format that matches the consumer
  2. Show an example, do not just describe it
  3. Validate every response and have a repair path

Frequently asked questions

Is the “Output Formatting (JSON, XML, Markdown)” lesson free?

Yes — the full text of “Output Formatting (JSON, XML, Markdown)” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “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. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Output Formatting (JSON, XML, Markdown)” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Zero-shot, Few-shot and Chain-of-Thought
  2. System vs User vs Assistant Roles
  3. Output Formatting (JSON, XML, Markdown)
  4. Avoiding Prompt Injection in Inputs
← Back to AI Agents