StatefulToolEnv
Environment for tasks where tools require per-rollout state (e.g., sandbox IDs, database connections, sessions).Overview
StatefulToolEnv extends ToolEnv to support tools that need persistent state across multiple calls within a rollout:
- Hidden arguments: Some tool arguments can be hidden from the model and injected automatically
- State injection: Use
update_tool_args()to inject state into tool calls - Per-rollout isolation: Each rollout maintains its own state
Inheritance
Constructor
Do NOT pass tools in the constructor. Use
add_tool() instead to specify hidden arguments.Core Methods
update_tool_args
str
Name of the tool being called.
dict
Arguments from the model’s tool call.
vf.Messages
Conversation history.
vf.State
Current rollout state (can be modified in-place).
dict - Updated tool arguments (including hidden args).
add_tool
Callable
Python function to add as a tool.
list[str]
default:"[]"
List of argument names to hide from the model’s view. These arguments will be removed from the tool schema but can be injected via
update_tool_args().remove_tool
env_response
update_tool_args() before each tool execution. Implemented by StatefulToolEnv - do not override.
Example Usage
Sandbox Environment
Database Session
API Session with Authentication
Multi-Argument State Injection
Conditional State Injection
Schema Filtering
Hidden arguments are removed from the tool schema shown to the model:Common Patterns
Setup and Cleanup
Usesetup_state() and @vf.cleanup for resource management:
Per-Tool State Injection
Inject different state for different tools:Dynamic State Updates
Modify state based on tool calls:When to Use
UseStatefulToolEnv for:
- Sandbox environments (code execution, file operations)
- Database transactions
- Authenticated API sessions
- Stateful workflows
- Tools requiring persistent connections
- Stateless, idempotent tools
- Read-only operations
- Tools with no shared state
See Also
- ToolEnv - Stateless tool environments
- MultiTurnEnv - Multi-turn base class
- Environment - Base environment reference