Skip to main content

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

Core Methods

env_response

Abstract method - Must be implemented by subclasses. Generate environment’s response to the model’s latest message.
Messages
Conversation history including the model’s latest response.
State
Current rollout state.
Returns: Messages | str - Environment’s response as messages or string.

setup_state

Override to add environment-specific state fields before the rollout begins.
State
Initialized state from init_state().
Returns: State - Modified state.

get_prompt_messages

Construct the prompt for the next model turn. Override for non-linear message sequences.
State
Current rollout state.
Returns: 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

Render the final state["completion"] after rollout completes. Override for custom completion formatting.
State
Completed rollout state.
Default behavior: Extracts all messages after the initial prompt, including the final env_response if present.

add_trajectory_step

Add a trajectory step to 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

Stops if state["error"] is set. Highest priority (checked first).

prompt_too_long

Stops if state["prompt_too_long"] is True (set when OverlongPromptError occurs).

max_turns_reached

Stops when trajectory length reaches max_turns (if > 0).

has_final_env_response

Stops if 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 final rollout() method:
Flow:
  1. Initialize state via init_state()
  2. Call setup_state()
  3. 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 next get_prompt_messages()
  4. Call render_completion()
  5. 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

Set state["final_env_response"] to stop the rollout:

Access Dataset Fields in env_response

Dataset fields are available in state["input"] or directly in state:

Stateful Simulations

Use setup_state() to initialize and env_response() to update:

Built-in Rubric

MultiTurnEnv includes MultiTurnMonitorRubric which adds:
  • num_turns metric: Number of turns in the trajectory

When to Use

Use MultiTurnEnv for:
  • Games and simulations
  • Multi-step reasoning tasks
  • Environments requiring feedback loops
  • Agent interactions
  • Tool use (or use ToolEnv for structured tool calling)
For single-response tasks, use SingleTurnEnv instead.

See Also