Implementation:Guardrails ai Guardrails Guard Call Structured
| Knowledge Sources | |
|---|---|
| Domains | Validation, Structured_Output |
| Last Updated | 2026-02-14 00:00 GMT |
Overview
Concrete method for executing a Guard with structured JSON output parameters provided by the guardrails package.
Description
This is the same Guard.__call__ method used for plain text validation, but invoked with structured output parameters (tools or response_format) that guide the LLM to produce JSON. The Runner internally parses JSON output and validates each field against path-specific validators from the Pydantic schema. The validated_output is a Python dict matching the Pydantic model structure.
Usage
Use this after creating a Guard with Guard.for_pydantic() and generating strategy parameters. Pass tools= or response_format= as keyword arguments.
Code Reference
Source Location
- Repository: guardrails
- File: guardrails/guard.py
- Lines: L745-795
Signature
@trace(name="/guard_call", origin="Guard.__call__")
def __call__(
self,
llm_api: Optional[Callable] = None,
*args,
prompt_params: Optional[Dict] = None,
num_reasks: Optional[int] = 1,
messages: Optional[List[Dict]] = None,
metadata: Optional[Dict] = None,
full_schema_reask: Optional[bool] = None,
**kwargs, # tools=, response_format= passed here
) -> Union[ValidationOutcome[OT], Iterator[ValidationOutcome[OT]]]:
Import
from guardrails import Guard
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| llm_api | Optional[Callable] | No | LLM callable or model= kwarg |
| messages | Optional[List[Dict]] | Yes | Chat messages |
| tools | list | No | Function calling tools from guard.json_function_calling_tool() (via kwargs) |
| response_format | dict | No | JSON mode schema from guard.response_format_json_schema() (via kwargs) |
| num_reasks | Optional[int] | No | Max re-ask attempts (default 1) |
| full_schema_reask | Optional[bool] | No | Regenerate full schema vs just failed fields on reask |
Outputs
| Name | Type | Description |
|---|---|---|
| result | ValidationOutcome[Dict] or ValidationOutcome[List] | Validated output typed to match the Pydantic model structure |
Usage Examples
Complete Structured Output Flow
from pydantic import BaseModel, Field
from guardrails import Guard
from guardrails.hub import ValidLength
from guardrails.types.on_fail import OnFailAction
class Movie(BaseModel):
title: str = Field(
description="Movie title",
json_schema_extra={"validators": [
ValidLength(min=1, max=200, on_fail=OnFailAction.FIX)
]}
)
year: int = Field(description="Release year")
genre: str = Field(description="Primary genre")
guard = Guard.for_pydantic(output_class=Movie)
tools = guard.json_function_calling_tool()
result = guard(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Give me a sci-fi movie."}],
tools=tools,
)
# result.validated_output is a dict: {"title": "...", "year": ..., "genre": "..."}
movie = Movie(**result.validated_output)