GeekHub Learn
Module
Lesson 5.66 of 8 in this module2 min read Module 5: Using AI APIs (OpenAI, Gemini, Anthropic)

Structured outputs across providers

Module 4.5 taught the why. This lesson covers the how across all three major providers.

Same shipping label format, three different printers. You need to know which buttons make each printer behave.

All three providers support enforced JSON outputs:

  • OpenAI: response_format={"type": "json_schema", "json_schema": {...}}
  • Gemini: response_mime_type="application/json" + response_schema=
  • Anthropic: tool use with a tool that takes a typed input matching your schema

Anthropic example (tool-as-schema pattern):

tool = {
    "name": "save_resume",
    "description": "Save extracted resume fields",
    "input_schema": {
        "type": "object",
        "properties": {
            "name": {"type": "string"},
            "skills": {"type": "array", "items": {"type": "string"}},
        },
        "required": ["name", "skills"],
    },
}

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=400,
    tools=[tool],
    tool_choice={"type": "tool", "name": "save_resume"},
    messages=[{"role": "user", "content": "Aman, Python and React."}],
)
print(message.content[0].input)

The model is forced to call save_resume with structured args matching the schema.

Visualize it

3-column comparison: same schema, three SDK calls, identical resulting JSON.

Try it now

Use any provider's playground to set a JSON schema and watch it enforce.

Hands-on lab

Write a extract_resume(text, provider) function that produces the same JSON shape from all three providers.

Try it now

Why does Anthropic use tools to enforce schemas rather than a response_format flag?

Common mistakes

  • Forgetting strict: true (OpenAI) or required fields
  • Letting the model add freestyle commentary alongside JSON
  • Mixing schemas across versions

Debugging tip

If you still see invalid JSON, you are either not in structured mode or your schema has loopholes (missing required, extra properties).

Challenge

Build a multi-provider structured extractor that works for both invoices and resumes via the same extract(schema, text) function.

Where this shows up

  • Resume parsing (the Module 7 project)
  • Invoice extraction
  • Form auto-fill from chat
  • Agent planning that emits structured plans

From the field

Structured outputs collapse the gap between LLMs and traditional systems. Mastering them is the move from "AI demo" to "AI in production".

Recap

All three providers enforce structured outputs, with slightly different ergonomics. Same schema, three buttons. Master all three.


Quick recall

3 prompts · think before you flip

Prompt 1 of 3

How does OpenAI enforce JSON schemas?

Quiz time

1 question · tap an answer to check it

  1. 1. The most reliable structured output mode in Anthropic is

Finished lesson 5.6?

Mark complete to update your module progress and unlock the streak.

Loading