Overview
MultiTurnEnv implements the core rollout loop used by all Verifiers environments (even SingleTurnEnv is just a MultiTurnEnv with max_turns=1). Each rollout follows this pattern:
- Initialize state —
setup_state()prepares per-rollout resources - Loop until done:
- Get prompt messages (initial prompt or previous conversation + environment response)
- Get model response
- Check stop conditions — exit if any
@vf.stopmethod returnsTrue
- Render completion — assemble final conversation into
state["completion"] - Cleanup — run all
@vf.cleanupmethods
The Rollout Loop
Here’s the core structure of a multi-turn rollout:env_response()— Required. Define how the environment responds after each model turnsetup_state()— Optional. Initialize per-rollout resources@vf.stopmethods — Optional. Define custom stop conditions@vf.cleanupmethods — Optional. Cleanup resources after each rollout
Building a Custom Environment
Let’s build a simple number guessing game:Real Example: Wordle
Let’s examine thewordle environment from the repository:
environments/wordle/wordle.py
- Wraps a TextArena game environment
- Uses
XMLParserto extract guesses from structured output - Custom
feedback_fncleans up the game state for the model - Multiple reward functions: correctness + efficiency bonus
Advanced Patterns
Custom Stop Conditions
Control when rollouts end with@vf.stop decorators:
Early Termination from env_response
Signal completion directly from the environment response:state["final_env_response"] bypasses the model response loop and terminates immediately.
Cleanup and Resource Management
Use decorators for proper resource cleanup:Custom Message Assembly
Overrideget_prompt_messages() for non-linear conversations:
Trajectory Tracking
Add metadata to each turn:Error Handling
Verifiers provides a hierarchy of error types undervf.Error:
vf.Error is raised during a rollout:
- It’s caught automatically
- Stored in
state["error"] - The built-in
has_errorstop condition triggers - The rollout terminates gracefully
Monitor Rubrics
Track environment-specific metrics automatically:MultiTurnEnv automatically tracks num_turns for all multi-turn environments.
Testing Your Environment
Common Pitfalls
Next Steps
- Add tools: Give your environment access to external functions → Tool Environments Guide
- Custom patterns: Advanced multi-turn patterns → Custom Environments Guide
- Training: Use your environment for RL training → Training Guide