Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Output Validation for AI Agents

Hello! Welcome to the next lesson in our journey to build production-grade agentic systems.

In our last lesson, we focused on reactive resilience—designing robust error-handling and retry patterns for when tool calls fail. We learned how to make an agent that doesn't simply crash when an external service is down or a request is malformed.

However, a tool call can succeed perfectly from a technical standpoint (e.g., return a 200 OK status) yet still provide an output that is useless or, even worse, dangerously incorrect. This brings us to today's crucial topic and learning outcome: Apply output validation techniques to verify an agent's work before it reaches the end user.

This lesson moves us from reactive error handling to proactive quality control. It's about building a "quality assurance" layer into your agentic workflow. For the high-stakes client services you intend to build, simply getting an answer isn't enough; you must be able to trust that the answer is well-structured, coherent, and correct. This is how you move from a "vibes-based" prototype to an evidence-based, reliable system.

1. The First Line of Defense: Syntactic & Schema Validation

The most fundamental problem with LLM outputs is their native format: text. While we prompt for structured formats like JSON, the model can easily make mistakes—missing commas, unclosed brackets, inconsistent field names—leading to parsing errors in downstream code.

The first step in output validation is to enforce a strict data contract or schema. Instead of "prompting and praying," we define the exact structure we expect and validate the output against it.

LLM to Pydantic to Application Pipeline: Turning Unpredictable Text into Reliable Data
This diagram illustrates the core principle of schema validation: creating a dedicated layer to transform unpredictable LLM text into reliable, structured data conforming to a predefined schema.

For this, libraries that pair data definition with validation are indispensable. Pydantic is a widely adopted standard in the Python ecosystem for this exact purpose. Given your computer science background, you can think of this as analogous to using an XSD to validate an XML document or applying type-checking in a statically-typed language—it's a fundamental discipline applied to the non-deterministic world of LLMs.

The following video provides an excellent overview of this problem and introduces Pydantic and the Instructor library, which operationalizes this concept with LLM APIs.

Pydantic is all you need: Jason Liu

This talk, 'Pydantic is all you need,' directly addresses the problem of unreliable LLM outputs and demonstrates a powerful solution for enforcing structure.

Please watch the first six minutes of the video (00:00 - 06:00). Pay close attention to: The initial problem description: why parsing LLM-generated JSON is brittle. How Pydantic uses Python classes to define a data schema. How the Instructor library integrates Pydantic directly into the OpenAI API call, guaranteeing a type-safe, validated object as the output, not just a string or dictionary.

This technique of defining a Pydantic model and using a library like Instructor (or the typed output features in frameworks like DSPy) is your first and most important validation step. It solves the entire class of problems related to malformed or structurally inconsistent outputs.

For a deeper architectural perspective, it's worth noting that this validation can even be pushed down into the model serving layer itself for greater efficiency, a concept known as structured decoding.

vLLM Architecture for Structured Decoding
The vLLM architecture shows how schema constraints can be integrated directly into the token generation process, ensuring the output is syntactically correct by construction.

2. Beyond Structure: Semantic Validation with "Graders"

A perfectly structured output can still be wrong. A weather API might return a valid JSON object with {"temperature": -500, "unit": "celsius"}, or a summarization agent might produce a beautifully formatted summary that completely misrepresents the source text. This is where we move from syntactic to semantic validation—checking if the output makes sense.

A useful mental model for this is the concept of "graders," as detailed in a post by Anthropic on agent evaluations. A grader is a piece of logic that scores some aspect of an agent's performance.

Demystifying evals for AI agents - Anthropic

To formalize our approach to validation, let's borrow from the domain of agent evaluation. This article from Anthropic provides a clear taxonomy of 'graders' that we can adapt for real-time validation.

Please read the section 'Types of graders for agents.' Focus on understanding the distinct characteristics, strengths, and weaknesses of the three types: Code-based graders Model-based graders Human graders We will be focusing on implementing the first two as automated validation techniques.

Let's explore how to implement these two primary types of automated graders.

Technique 1: Code-Based Graders (Rules and Logic)

This is the most reliable and cheapest form of semantic validation. You write deterministic code to check the properties of the output. Pydantic's validator functions are a perfect place to implement these checks.

For example, you can write validators that:

  • Check if a numerical value is within a plausible range (e.g., age must be between 0 and 120).
  • Verify that a string matches a specific regular expression.
  • Confirm that a start date occurs before an end date.

The Pydantic is all you need video demonstrates this concept. The presenter defines a custom validator and then, more powerfully, shows how to create an LLM-based validator, which is a hybrid approach where a code-based check triggers a model-based judgment.

Pydantic is all you need: Jason Liu

Let's return to the Pydantic video to see how custom validators are implemented for semantic checks.

Watch the segment from 07:22 to 08:07. First, notice the simple custom validator that enforces a string to be lowercase. Then, observe the llm_validator that checks for 'mean things'. This is a powerful pattern: a Pydantic validator that calls another LLM with a specific, narrow validation task.

Another powerful framework for this is DSPy, which uses assert and suggest to enforce constraints. An assert is a hard constraint that will cause a failure if violated, while a suggest is a soft constraint that nudges the model toward a correct output without causing a hard stop.

Complete DSPy Tutorial - Master LLM Prompt Programming in 8 amazing examples!

The DSPy framework offers another elegant way to implement code-based validation directly into your agent's reasoning process.

Watch the segment from 23:28 to 25:20. Understand the difference between dspy.Suggest and dspy.Assert. Notice how a validation rule (the output must be in a predefined list of categories) is used to guide the LLM's output, and how the framework can show the history of the correction.

Technique 2: Advanced Semantic Validation

The most critical semantic failures often involve hallucinations or logical inconsistencies. We can design sophisticated code-based validators to catch these. One of the most powerful techniques is fact-checking against a source.

For any agent that performs retrieval-augmented generation (RAG) to answer questions or summarize documents, you can build a validator to ensure the generated output is grounded in the retrieved context.

Let's look at a brilliant example of this pattern.

Pydantic is all you need: Jason Liu

The Pydantic video concludes with an advanced example of a semantic validator designed to combat hallucinations. This is a crucial technique for building trustworthy RAG agents.

Watch the segment from 14:09 to 16:08. The speaker defines a data model where each 'fact' in the answer must be accompanied by 'substring quotes' from the original text. A validator then programmatically checks if those quotes actually exist in the source document. This forces the model to ground its claims, moving from 'I think the answer is X' to 'The answer is X, and here is the exact sentence from the source that proves it.'

This pattern of demanding citations from the model and then programmatically verifying them is a cornerstone of building reliable, evidence-based agents.

3. Architectural Pattern: AI Decision Circuits

A single validator can still be wrong. To achieve the high level of certainty required for enterprise clients, we can't rely on a single check. Instead, we need to design architectures that combine multiple, independent validation methods to reach a consensus.

This is where the concept of AI Decision Circuits comes in—a system design pattern that draws an analogy from fault-tolerant electronic circuits.

Attaining LLM Certainty with AI Decision Circuits

This article, 'Attaining LLM Certainty with AI Decision Circuits,' presents a powerful architectural pattern for building highly reliable agentic systems. It aligns perfectly with your interest in system design and moving beyond single-agent thinking.

Please read the following sections from the article: 'From Electronic Circuits to AI Decision Circuits': Understand the core principles of redundant processing and validator agents. The code snippets for primary_parser, backup_parser, negative_checker, and validate_call_type: See how different validation strategies are implemented as separate functions. The combine_results function and 'The Decision Logic, Step by Step' section: This is the heart of the pattern. Analyze how the outputs from the different validators are combined using deterministic Boolean logic to assign a final result and a confidence level (high, medium, low). This pattern demonstrates how to compose multiple simple validators into a system that is more robust than any of its individual parts.

By adopting this architectural pattern, you can design a system that not only produces an answer but also provides a quantifiable confidence score. This allows you to intelligently route outputs: high-confidence results can proceed automatically, while medium- or low-confidence results can be flagged for human review. This is a practical and powerful way to implement human-in-the-loop controls.

4. Closing the Loop: Validation and Self-Correction

What should happen when a validation check fails? As we saw in the previous lesson, an error isn't just a dead end; it's an opportunity for the agent to self-correct. A ValidationError is a type of structured feedback that is often more useful than a generic API error.

By feeding the validation error message back into the LLM's context, we can create a reflection loop that allows the agent to fix its own output.

Pydantic is all you need: Jason Liu

Let's revisit the Pydantic video one last time to see this self-correction loop in action. This connects today's lesson on validation directly back to our previous lesson on error handling.

Watch the segment on retries from 08:07 to 09:02. Notice how a validation error is caught, and max_retries is used. The framework automatically captures the validation error, appends it to the prompt, and asks the model to try again. This transforms validation from a simple pass/fail gate into an interactive correction mechanism.

This self-correction pattern, triggered by a validation failure, is a hallmark of a truly robust and adaptive agentic system.

Conclusion

Today, we've built a comprehensive quality assurance toolkit for our agents. We've established that verifying an agent's work is a multi-layered process that moves far beyond simple error checking.

Key Takeaways:

  • Validation is Layered: Effective validation progresses from syntactic (schema validation with tools like Pydantic) to semantic (checking for correctness and logic with custom code and model-based "graders").
  • A Taxonomy of Graders: We can implement validation using deterministic code-based graders (for rules, ranges, and fact-checking) and probabilistic model-based graders (for nuanced, rubric-based judgments).
  • Grounding is Key: For RAG systems, a critical validation technique is to force the agent to cite its sources and programmatically verify those citations against the original context.
  • Robustness Through Architecture: High-reliability systems can be built using architectural patterns like AI Decision Circuits, which combine redundant processing and multiple validators to achieve consensus and assign confidence scores.
  • Validation Enables Self-Correction: A validation failure is structured feedback. By feeding this feedback to the agent, we enable it to reflect and correct its own work, creating a more resilient system.

With the ability to handle errors and now validate outputs, we have the core components of a reliable single agent. In our next lesson, we will begin our exploration of Multi-Agent Architecture and Orchestration. We'll start by addressing a fundamental design question: When should you use a single, versatile agent versus a team of specialized agents for a given workflow? The validation techniques we learned today will prove essential for ensuring quality as work is handed off between these specialized agents.

Can't find a good explanation? Sign up and we'll make it for you

Sign up