liveBook · Manning

Validation

As explained in AI Agents in Action, Second Edition

Output validation is the process of checking generated or returned data against defined conditions before it is accepted or delivered.

Output validation checks generated or returned data against defined conditions before it is accepted or used. In agent workflows, checking tool results against expected schemas helps prevent one bad result from steering later actions. section 3.2.4 section 4.4.1

A guardrail can perform these checks with code or an agent. Simple rules, such as string comparisons or length checks, can mark invalid data by triggering a tripwire; more complex validation logic is also possible. section 4.4.1

A simple guardrail

An input guardrail checks whether a request contains a forbidden research title and triggers a tripwire if it does. The same pattern can guard an agent's output. section 4.4.1

Listing 4.8 07_input_output_guardrails.py
class ResearchOutputModel(BaseModel):    #1
    """Output model for the research agent."""
    research_plan: str
    """The final research plan as text."""
    research_plan_file: str
    """The path to the research plan file."""

@input_guardrail    #2
async def research_guardrail(    #2
    ctx: RunContextWrapper[None], 
    agent: Agent, 
    input: str | list[TResponseInputItem]
) -> GuardrailFunctionOutput:
    forbidden_research = "The Hitchhiker's Guide to the Galaxy"
    if forbidden_research in input:
        research_forbidden = True
    else:
        research_forbidden = False

    return GuardrailFunctionOutput(
        output_info=f"user asked: {input}",
        tripwire_triggered=research_forbidden,    #3
    )

@output_guardrail    #4
async def research_output_guardrail(     #4
    ctx: RunContextWrapper, 
    agent: Agent, 
    output: ResearchOutputModel    #1
) -> GuardrailFunctionOutput:
    if len(output.research_plan) < 100:
        insufficient_research = True
    else:
        insufficient_research = False

    return GuardrailFunctionOutput(    
        output_info="research plan length: {len(output.research_plan)}",
        tripwire_triggered=insufficient_research,    #3
    )
# code omitted…
agent = Agent(
    name="Assistant",
    instructions=instructions,
    mcp_servers=[research_srv, thinking_srv, fs_srv],
    output_type=ResearchOutputModel,    #1
    input_guardrails=[research_guardrail],    #2
    output_guardrails=[research_output_guardrail],    #4
)
goal = """
Produce a research plan to find the book 'The Hitchhiker's Guide to the Galaxy'
"""
try:
    print("Running...", goal)
    result = await Runner.run(agent, goal)
    print(result.final_output)
except InputGuardrailTripwireTriggered as input_tripped:
    print(f"""
Input guardrail tripwire triggered:    #1
{input_tripped.guardrail_result.output.output_info}
""")
except OutputGuardrailTripwireTriggered as output_tripped:
    print(f"""
Output guardrail tripwire triggered:    #2
{output_tripped.guardrail_result.output.output_info} 
""")

Inspect an Agents SDK listing that defines input and output guardrails and an output model. section 4.4.1

How AI Agents in Action, Second Edition covers it

How deep it goes
Demonstrates string and length checks with tripwires, while allowing more complex logic without detailing it.
Examples
Uses a forbidden research title, string comparisons, length checks, and a typed research output model.
What you can do after
Apply guardrails to check agent inputs and outputs, trigger failures, and validate tool results before further actions.
How it connects
Ties output validation to guardrails, AI agents, and tool execution.

Also explained in

Compare how 30 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.