Skip to main content

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
For tools requiring per-rollout state (e.g., sandbox IDs, database connections), use StatefulToolEnv instead.

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).
All other parameters are inherited from MultiTurnEnv.

Core Methods

call_tool

Execute a tool and return the result as a 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.
Returns: ToolMessage - Message containing tool result or error.

env_response

Process tool calls from the model’s response. Implemented by ToolEnv - do not override unless you need custom behavior.
vf.Messages
Conversation history including model’s tool calls.
vf.State
Current rollout state.
Returns: vf.Messages - List of ToolMessage objects with results.

add_tool

Dynamically add a tool to the environment.
Callable
Python function to add as a tool.

remove_tool

Remove a tool from the environment.
Callable
Python function to remove.

Stop Conditions

no_tools_called

Stops if the model’s last message was an assistant message with no tool calls. Inherits all stop conditions from MultiTurnEnv.

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:
Generates schema:

Common Patterns

Stateless Tools Only

All tool calls should be independent:
For stateful tools, use StatefulToolEnv.

Custom Error Messages

Format errors to guide the model:

Reward Based on Tool Usage

When to Use

Use ToolEnv for:
  • Stateless function calling (calculators, converters, queries)
  • API clients (each call is independent)
  • Read-only database queries
  • File reading operations
  • Any idempotent tool
Use StatefulToolEnv for:
  • 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