ToolEnv
Environment for tasks where the model can call Python functions as tools.Overview
ToolEnv enables LLMs to call Python functions with all arguments exposed to the model. Key features:
- Stateless tools: Each tool call is independent and idempotent
- Automatic schema generation: Function signatures are converted to tool definitions
- Error handling: Configurable error formatting and stop-on-error behavior
- Tool metrics: Automatic tracking of tool call counts
Inheritance
Constructor
Parameters
list[Callable] | None
List of Python functions to expose as tools. Function signatures and docstrings are used to generate tool schemas.
int
default:"10"
Maximum number of turns before stopping.
Callable[[Exception], str]
default:"lambda e: f'{e}'"
Function to format exceptions into error messages shown to the model.
list[type[Exception]] | None
List of exception types that should stop the rollout (raise
ToolParseError or ToolCallError).Core Methods
call_tool
ToolMessage. Override to customize tool execution.
str
Name of the tool to call.
dict
Arguments parsed from the model’s tool call.
str
Unique ID for this tool call.
ToolMessage - Message containing tool result or error.
env_response
vf.Messages
Conversation history including model’s tool calls.
vf.State
Current rollout state.
vf.Messages - List of ToolMessage objects with results.
add_tool
Callable
Python function to add as a tool.
remove_tool
Callable
Python function to remove.
Stop Conditions
no_tools_called
Built-in Rubric
ToolEnv includes ToolMonitorRubric which tracks:
total_tool_calls: Total number of tool calls made{tool_name}_calls: Number of calls to each specific tool
Example Usage
Basic Calculator
With Error Handling
With Stop Errors
Database Query Tools
API Client Tools
Dynamic Tool Addition
Tool Schema Generation
Tools are automatically converted to schema using function signatures and docstrings:Common Patterns
Stateless Tools Only
All tool calls should be independent:Custom Error Messages
Format errors to guide the model:Reward Based on Tool Usage
When to Use
UseToolEnv for:
- Stateless function calling (calculators, converters, queries)
- API clients (each call is independent)
- Read-only database queries
- File reading operations
- Any idempotent tool
- Tools requiring per-rollout state (sandbox IDs, sessions)
- Database transactions
- File writing in isolated environments
- Any tool where state must persist across calls
See Also
- StatefulToolEnv - Tools with per-rollout state
- MultiTurnEnv - Multi-turn base class
- Environment - Base environment reference