Recommended resources
The freely available resources this lesson will direct you to.
Introduction
In our last lesson, we focused on designing fallback and escalation behaviors to manage agent failures. We established that a robust system must gracefully handle unexpected events, including when an agent produces a malformed or syntactically incorrect output. The self-correction loop we discussed is a powerful reactive pattern for this.
Today, we shift from reaction to prevention. Why build complex loops to handle broken JSON when you can architect the system to guarantee the JSON is always valid? This lesson directly addresses that question. Our learning outcome is to apply structured output formatting (e.g., JSON schemas) to enforce consistent agent responses.
This is a cornerstone of building reliable agentic systems. When an agent's output is guaranteed to be machine-readable and conform to a predefined data contract, you unlock the ability to:
- Reliably call tools and APIs without parsing errors.
- Chain agents together in predictable workflows.
- Store agent outputs in structured databases for analysis.
- Build user interfaces that can confidently bind to agent data.
We will explore the underlying theory of how this enforcement works and then translate that theory into practical design patterns for building your agentic solutions.
1. From Unpredictable Text to Guaranteed Structure
At their core, Large Language Models are text generators. Without constraints, they will often wrap their output in helpful but programmatically useless explanatory text. For an agentic system that needs to parse a response, this is a critical failure point.
Structured Output from LLMs: Grammars, Regex, and State Machines
To see a simple example of this problem, and the solution offered by modern LLM APIs, let's watch the beginning of the video 'Structured Output from LLMs' from the Efficient NLP channel.
Watch from the start to 02:26. Notice how the unconstrained model adds extra text and how using a Pydantic schema with the OpenAI API forces the output into the desired format.
The technique that makes this possible is called constrained decoding. As a computer scientist, you can think of it as changing the model's token generation process from a free-form selection to one that is filtered by a set of rules.
Constrained Decoding and Structured Output for Agent Reliability
The article 'Constrained Decoding and Structured Output for Agent Reliability' provides a clear, high-level definition of this concept.
Read the 'Concept Introduction' section. It provides a simple analogy and a technical overview of how constrained decoding works by masking invalid tokens.
This is more than just a convenience. As you design systems for clients, the distinction between hoping for valid JSON and guaranteeing it is the difference between a fragile prototype and a production-ready service.
Structured Output, Function Calling, or JSON Mode?
Modern APIs offer several ways to get structured data. A key architectural decision is choosing the right tool for the job.
OpenAI Structured Output - All You Need to Know
Dave Ebbelaar's video 'OpenAI Structured Output - All You Need to Know' offers excellent guidelines on when to use different features.
Please watch the following two segments: 01:18 - 04:57: This section explains the key benefit of the new structured output mode: it guarantees content validity (e.g., respecting enums) within the schema, not just syntactic validity. 05:07 - 07:12: This part provides crucial decision-making criteria for when to use structured output versus function calling. Pay close attention to the recommendation for multi-agent systems.
To summarize the key design choice:
- Use Function Calling when you need the model to select one or more tools and generate the specific arguments to call them. This is for when the agent is acting.
- Use Structured Output when you need to enforce the format of the final response to a user, or, critically for our purposes, when defining the data contract for internal routing and communication between agents in a multi-agent system.
2. The Core Mechanism: Automata-Guided Generation
How does an LLM, a probabilistic model, provide a deterministic guarantee of structure? The answer lies in applying principles from formal language theory directly to the token generation process. Given your background, the underlying mechanics will be particularly interesting.
The process generally involves converting the desired schema (e.g., a JSON schema) into a regular expression, which is then used to construct a Finite State Machine (FSM). This FSM guides the LLM at every step.
Structured Output from LLMs: Grammars, Regex, and State Machines
The 'Structured Output from LLMs' video provides one of the best explanations of this FSM-guided process.
Watch the section from 03:01 to 08:10. This is the theoretical core of the lesson. Focus on: How a JSON schema can be converted into a regular expression and then a Finite State Machine (FSM). The mechanism of using the FSM's current state to create a 'mask' of valid next tokens. How this mask is applied to the LLM's output probabilities (logits) before sampling the next token.
The following diagram provides a fantastic visual summary of this token masking process. The FSM state dictates which tokens are "allowed," their probabilities are renormalized, and the model samples only from that valid subset.

For a more formal treatment, we can express this mathematically.
Constrained Decoding and Structured Output for Agent Reliability
The article on 'Constrained Decoding' provides the pseudocode and mathematical formulation for this process.
Read section 3, 'Algorithms & Math'. The pseudocode for constrained_decode and the mathematical formulation for the token mask \mathbf{m}_i formalize the process we saw in the video. This connects formal language theory directly to LLM inference.
This mechanism is now being integrated into high-performance serving frameworks like vLLM, demonstrating its importance for production systems.

It's worth noting that while FSMs (recognizing regular languages) can handle most practical JSON needs, more complex nested structures theoretically require context-free grammars (CFGs). This introduces challenges like the "token-terminal mismatch problem," where LLM tokens don't align with the grammar's terminal symbols. This is an active area of research, but for most agentic design, FSM-based approaches are sufficient.
3. Application and System Design Patterns
With a solid grasp of the theory, let's turn to practical application. How do you, as a system architect, use these capabilities to build robust agentic workflows?
Pydantic: The Industry Standard for Schema Definition
While you can write raw JSON schemas, the overwhelming best practice in the Python ecosystem is to use Pydantic. Pydantic models are Python classes that provide data validation and schema definition in a clean, readable, and maintainable way.
OpenAI Structured Output - All You Need to Know
Let's return to the 'OpenAI Structured Output' video to see a side-by-side comparison of using a raw JSON schema versus a Pydantic model.
Watch from 07:12 to 20:51. As you watch, consider these points from a system design perspective: Readability and Maintainability: Compare the verbose JSON schema dictionary with the concise Pydantic class definitions. Type Safety: Pydantic leverages Python's type hints, making your code self-documenting and checkable by static analysis tools. Extensibility: Notice how easily a new field (like confidence_score) is added to the Pydantic model. This is crucial for iterative development.
Architectural Patterns for Structured Output
Adopting structured output is not just an implementation detail; it enables new architectural patterns for building reliable systems.
Constrained Decoding and Structured Output for Agent Reliability
The 'Constrained Decoding' article outlines several key design patterns that you should incorporate into your architectural toolkit.
Please read section 4, 'Design Patterns & Architectures'. Pay special attention to: Schema-First Agent Design: The principle of defining data contracts before writing prompts, treating your agent's interface like a software API. Layered Validation: The idea that constrained decoding is only the first layer. It should be combined with type validation (Pydantic) and semantic business logic validation.
Case Study: Inter-Agent Handoffs
Perhaps the most powerful application of structured output in agentic systems is in orchestrating teams of specialized agents. By defining a strict JSON schema for the "handoff" of information from one agent to the next, you create a reliable communication bus.
The 'Inter-Agent Handoff Schema' from Promptz.dev is an excellent, real-world example of this pattern in action for a product development workflow.
Take a few minutes to browse this document. You don't need to memorize it, but observe the design: Read the 'Design Principles' and the structure of the 'Master Handoff Envelope'. Skim through the 'Phase-Specific Payloads' (e.g., Orchestrator to Market Research Agent, Market Research Agent to Orchestrator). Notice how each handoff is a precisely defined JSON object. This is the orchestration piece that many tutorials miss: agent collaboration is not a vague conversation, but a sequence of structured data transformations.
This example demonstrates how the principles of API design and data contracts, familiar from traditional software engineering, are directly applicable and essential for building scalable and maintainable multi-agent systems.
Conclusion
Today, we've moved from hoping for correct formats to guaranteeing them. Enforcing structured output via constrained decoding is a fundamental technique for building production-quality agentic systems.
Key Takeaways:
- Guarantee, Don't Guess: Use structured output features to ensure agent responses are always machine-readable and conform to your data contracts.
- Mechanism Matters: The underlying process involves converting schemas into Finite State Machines that guide the LLM's token generation by masking invalid tokens at each step.
- Pydantic is the Standard: For defining schemas in Python, Pydantic offers superior readability, maintainability, and validation compared to raw JSON schemas.
- Adopt a Schema-First Mindset: Design your data contracts before you write your prompts. Treat agent interfaces with the same rigor as you would a software API.
- Structure Enables Orchestration: Strict, schema-defined handoffs are the backbone of reliable multi-agent systems, enabling predictable communication and workflow execution.
Preview of the Next Lesson
We now have a powerful tool to control the structure of an agent's output. However, a perfectly formatted JSON response can still contain incorrect or low-quality information. The next step is to improve the content within that structure.
In our next lesson, we will focus on the iterative process of improvement. We will learn how to iteratively refine system instructions using failure case analysis, a systematic method for diagnosing why an agent produces a suboptimal response and updating its core programming to prevent that failure in the future.