Skip to main content

State

The State type is a specialized dict subclass that holds all runtime information for a rollout, including inputs, outputs, trajectory, and metadata.

Overview

State provides a unified interface for accessing both:
  • Input fields: From the dataset row (prompt, answer, task, info, example_id)
  • Runtime fields: Created during rollout execution (trajectory, reward, completion, etc.)
Fields are automatically forwarded to/from the nested input dict for seamless access.

Type Definition

Input Fields

These fields come from the dataset and are stored in state["input"]:
Messages
The input prompt as a list of messages.
str | Any
Ground truth answer or reference data for scoring.
str
Task identifier (e.g., “math”, “coding”, “gsm8k”).
Info
Additional metadata from the dataset (arbitrary dict).
int
Unique integer ID for this example.

Runtime Fields

Client
The API client instance for model calls.
str
Model identifier (e.g., “gpt-4”, “claude-3-5-sonnet-20241022”).
SamplingArgs | None
Sampling parameters (temperature, top_p, etc.).
bool
Whether the rollout completed successfully.
bool
Whether the rollout was truncated (max turns, length limit, etc.).
str | None
Name of the stop condition that ended the rollout.
list[Tool]
Tool definitions available during this rollout.
list[TrajectoryStep]
Complete turn-by-turn trajectory (prompts, completions, rewards).
Messages | None
Final completion (last assistant message or concatenated messages).
float | None
Total reward for this rollout.
float | None
Advantage value (for group scoring).
dict[str, float] | None
Named metric scores (e.g., {"correctness": 1.0, "length": 0.8}).
RolloutTiming | None
Timing information (start_time, generation_ms, scoring_ms, total_ms).
Error | None
Error object if rollout failed.
TokenUsage | None
Token usage (input_tokens, output_tokens).

Special Behavior

Input Field Forwarding

Accessing input fields automatically looks in state["input"]:

get() Method

Safe access with default fallback:

Example Usage

Basic Access

Trajectory Inspection

Custom State Keys

Environments can add custom keys:

Safe Error Access

Serialization

State can be converted to RolloutOutput for serialization:

Type Annotations

Common Patterns

Checking Completion

Accessing Metadata

Iterating Trajectory

See Also