MultiTurnEnv
Environment for multi-turn tasks involving back-and-forth interaction between the model and environment.Overview
MultiTurnEnv enables interactive tasks where:
- The model generates a response
- The environment provides feedback via
env_response() - This continues until a stop condition is met
- Common use cases: games, simulations, tool use, agent interactions
Inheritance
Constructor
Parameters
int
default:"-1"
Maximum number of turns before stopping. -1 for unlimited turns.
Core Methods
env_response
Messages
Conversation history including the model’s latest response.
State
Current rollout state.
Messages | str - Environment’s response as messages or string.
setup_state
State
Initialized state from
init_state().State - Modified state.
get_prompt_messages
State
Current rollout state.
Messages - Prompt messages for the model.
Default behavior:
- Turn 0: Returns
state["prompt"] - Turn N: Concatenates previous turn’s prompt + completion +
env_response()
render_completion
state["completion"] after rollout completes. Override for custom completion formatting.
State
Completed rollout state.
env_response if present.
add_trajectory_step
state["trajectory"]. Override to set intermediate rewards, advantages, or extra metadata.
State
Current rollout state.
TrajectoryStep
Step containing prompt, completion, response, tokens, etc.
Stop Conditions
Stop conditions are methods decorated with@vf.stop that return bool. The rollout continues until any stop condition returns True.
Built-in Stop Conditions
has_error
state["error"] is set. Highest priority (checked first).
prompt_too_long
state["prompt_too_long"] is True (set when OverlongPromptError occurs).
max_turns_reached
max_turns (if > 0).
has_final_env_response
state["final_env_response"] is set. Use this to signal termination from env_response():
Custom Stop Conditions
Add custom stop conditions by decorating methods with@vf.stop:
Rollout Loop
The rollout loop is implemented in the finalrollout() method:
- Initialize state via
init_state() - Call
setup_state() - Loop:
- Check stop conditions via
is_completed() - Get prompt via
get_prompt_messages() - Get model response via
get_model_response() - Add to trajectory via
add_model_response()→add_trajectory_step() env_response()is called in nextget_prompt_messages()
- Check stop conditions via
- Call
render_completion() - Return final state
Do NOT override
rollout(). Use the provided hooks: setup_state(), env_response(), add_trajectory_step(), and stop conditions.Example Usage
Simple Game Environment
Text-Based Adventure
With Intermediate Rewards
Common Patterns
Signal Termination from env_response
Setstate["final_env_response"] to stop the rollout:
Access Dataset Fields in env_response
Dataset fields are available instate["input"] or directly in state:
Stateful Simulations
Usesetup_state() to initialize and env_response() to update:
Built-in Rubric
MultiTurnEnv includes MultiTurnMonitorRubric which adds:
num_turnsmetric: Number of turns in the trajectory
When to Use
UseMultiTurnEnv for:
- Games and simulations
- Multi-step reasoning tasks
- Environments requiring feedback loops
- Agent interactions
- Tool use (or use ToolEnv for structured tool calling)
See Also
- Environment - Base class reference
- SingleTurnEnv - Single-turn environments
- ToolEnv - Structured tool-calling environments
- StatefulToolEnv - Tool environments with per-rollout state