Implementation:Guardrails ai Guardrails ValidationOutcome Structured
| Knowledge Sources | |
|---|---|
| Domains | Structured_Output, Data_Conversion |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete pattern for consuming structured validated output from a Guard execution provided by the guardrails package.
Description
When a Guard created via Guard.for_pydantic() executes successfully, the ValidationOutcome.validated_output field contains a Python dict (for single model output) or list (for list model output) that conforms to the Pydantic model's schema. This is the same ValidationOutcome class used for plain text validation, but with OT typed as Dict or List instead of str.
Usage
Access .validated_output on the result of a structured Guard call. Optionally pass it to the Pydantic model constructor for type-safe access.
Code Reference
Source Location
- Repository: guardrails
- File: guardrails/classes/validation_outcome.py
- Lines: L45-51 (validated_output field)
Signature
class ValidationOutcome(IValidationOutcome, ArbitraryModel, Generic[OT]):
validated_output: Optional[OT] = Field(
description="The validated, and potentially fixed,"
" output from the LLM call after passing through validation.",
default=None,
)
# For structured output, OT is Dict or List
Import
from guardrails.classes.validation_outcome import ValidationOutcome
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ValidationOutcome | ValidationOutcome[Dict] or ValidationOutcome[List] | Yes | Result from Guard.__call__() with structured output Guard |
Outputs
| Name | Type | Description |
|---|---|---|
| .validated_output | Dict or List | Python dict/list matching Pydantic model schema |
| .validation_passed | bool | Whether all field validators passed |
| Pydantic model | BaseModel | Optional: MyModel(**validated_output) for typed access |
Usage Examples
Consuming Structured Output
from pydantic import BaseModel, Field
from guardrails import Guard
class Movie(BaseModel):
title: str = Field(description="Movie title")
year: int = Field(description="Release year")
guard = Guard.for_pydantic(output_class=Movie)
result = guard(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Name a classic movie."}],
)
if result.validation_passed:
# Access as dict
print(result.validated_output["title"])
print(result.validated_output["year"])
# Convert to typed Pydantic model
movie = Movie(**result.validated_output)
print(movie.title)
print(movie.year)