AIMessage without actually executing the tools. Useful for extraction and structured output use cases where you need to generate output that conforms to a complex schema without losing the original messages and tool IDs.
Defined in: langgraph/prebuilt/tool_validator.py:47
Class Definition
Parameters
Sequence[BaseTool | type[BaseModel] | Callable]
required
A list of schemas to validate the tool calls with. These can be any of the following:
- A pydantic BaseModel class
- A BaseTool instance (the args_schema will be used)
- A function (a schema will be created from the function signature)
Callable[[BaseException, ToolCall, type[BaseModel]], str] | None
default:"None"
A function that takes an exception, a ToolCall, and a schema and returns a formatted error string. By default, it returns the exception repr and a message to respond after fixing validation errors.
str
default:"'validation'"
The name of the node.
list[str] | None
default:"None"
A list of tags to add to the node.
Input/Output
list[AnyMessage] | dict[str, Any]
Can be used either in
StateGraph with a 'messages' key or with a list of messages.dict[str, list[ToolMessage]] | list[ToolMessage]
A list of
ToolMessage objects with the validated content or error messages.- If input is a dict: returns
{"messages": [ToolMessage(...)]} - If input is a list: returns
[ToolMessage(...)]
How It Works
TheValidationNode performs the following steps:
- Extracts the last
AIMessagefrom the input - Iterates through all tool calls in that message
- For each tool call, validates the arguments against the corresponding schema
- Returns
ToolMessageobjects with:- The validated content (as JSON) if validation succeeds
- An error message if validation fails (with
additional_kwargs={"is_error": True})
This node does not actually run the tools, it only validates the tool calls. This is useful for extraction and other use cases where you need to generate structured output that conforms to a complex schema without losing the original messages and tool IDs (for use in multi-turn conversations).
Usage Example
Re-prompting for Valid Response
Custom Error Formatting
With Multiple Schemas
Using Functions as Schemas
Integration with StateGraph
Properties
dict[str, type[BaseModel]]
Mapping from schema name to BaseModel class. This is populated during initialization and contains all the schemas that can be validated.
Validation Behavior
Successful Validation
When validation succeeds:- Returns a
ToolMessagewith the validated content serialized as JSON - The
tool_call_idmatches the original tool call ID - No
additional_kwargsare set
Failed Validation
When validation fails:- Returns a
ToolMessagewith the error message fromformat_error - The
tool_call_idmatches the original tool call ID - Sets
additional_kwargs={"is_error": True}to indicate an error
Common Use Cases
1. Structured Data Extraction
Validate that extracted information matches the expected schema before processing:2. Form Input Validation
Ensure user inputs conform to required formats:3. Multi-Step Extraction with Retry
Validate extraction and re-prompt on errors:See Also
- ToolNode - For actually executing tools (not just validating)
- create_react_agent - Factory function for ReAct agents
- Pydantic BaseModel - Schema validation framework used by ValidationNode