Cover of Structured Output and Tool Schemas
From Keyword to Code

Structured Output and Tool Schemas

Getting Reliable JSON Out of a Model That Wants to Chat

Learn how to work towards reliable structured responses from language models. Explore JSON output and tool schemas, and the practical challenges of connecting conversational models to software that expects a defined format.

View on Amazon ↗

Opens Amazon in a new tab. Check the listing for formats and availability.

A closer look

About this book.

Ask a language model for JSON and it will happily hand you a paragraph instead — or JSON with the wrong fields, a missing value, or a type your code wasn't expecting. If you've ever wrapped an API call in a fragile try/except and hoped, you already know the problem this book solves.

Structured Output and Tool Schemas is a hands-on, code-first guide to getting reliable, schema-conformant output from OpenAI and Anthropic models, every time — not just on your three test examples. You'll build one real pipeline, start to finish: a document-extraction system that turns messy invoice text into validated, typed data.

Inside, you'll learn to

  • Design a Pydantic schema that actually enforces what "correct" means
  • Compare JSON mode, function calling, and native structured outputs — and know which to pick
  • Validate every response independently, and turn failures into specific, repairable errors
  • Build a repair-and-retry loop with a hard ceiling, plus a quarantine path for the rest
  • Measure real reliability against a labeled test set, stream partial results, and version your schema without breaking callers already depending on it

By the end, you'll have a working extraction service — and the reliability numbers to back up calling it that.

Beyond the book

Reader resources.

Download chapter code (ZIP)

Includes a README with setup notes and a chapter-by-chapter file index.

Official documentation and guides for topics covered in this book.

← Back to the books