Skip to main content

GymEnv

A universal adapter for running OpenAI Gym-compatible environments with language models.
GymEnv is experimental and subject to breaking changes. The API may change in future releases.

Overview

GymEnv bridges the gap between Gym’s step-based API and Verifiers’ message-based rollout system. It:
  • Converts Gym observations to text prompts
  • Parses model completions into actions
  • Manages episode lifecycle (reset/step/done)
  • Computes episodic rewards

Inheritance

Constructor

type[StepResetEnv]
required
Gym environment class with reset(seed) and step(action) methods.
dict[str, Any] | None
default:"None"
Keyword arguments passed to env_cls() constructor.
Callable[[str], Any] | None
default:"None"
Function to parse model output into an action. Defaults to identity (string actions).
Callable[[Any], str] | None
default:"None"
Function to convert observations to text. Defaults to str(obs).
int
default:"1000"
Number of episodes in training dataset.
int
default:"20"
Number of episodes in eval dataset.
int | None
default:"None"
Maximum steps per episode. If None, uses 1000.
int
default:"0"
Random seed for episode generation.
str | None
System prompt explaining the task.
Rubric | None
Custom rubric for scoring. Defaults to EpisodicSumRubric().

StepResetEnv Protocol

Gym environments must implement:
Supports both old (4-tuple) and new (5-tuple) Gym APIs.

Example Usage

CartPole Example

Custom Text Game

With Custom Parser

Built-in Rubric

EpisodicSumRubric

Default rubric that sums step rewards:
Accesses state["trajectory"] to sum per-step rewards from the environment.

Key Methods

gym_to_hf

Generates HuggingFace datasets by running reset() on each episode:

obs_to_text

Converts observation to text. Override for custom formatting:

env_response

Executes env.step(action) and returns observation as user message.

State Keys

GymEnv adds:
StepResetEnv
Active Gym environment instance (created per rollout).
bool
Whether episode has terminated.
float
Step-level reward from env.step().
dict
Info dict returned by env.step().

Error Handling

Action parsing errors:
  • Set state["gym_done"] = True
  • Return error message to model
  • Assign 0.0 reward to that step

Stop Conditions

Episode ends when:
  1. Gym returns done=True or truncated=True
  2. max_episode_steps is reached
  3. Action parsing fails

Advanced: Custom Reward

Limitations

  • Text-only: Observation must be convertible to text (no vision support)
  • Synchronous: Gym envs are not async
  • Single episode per rollout: Each rollout is one episode

When to Use

Use GymEnv for:
  • Existing Gym environments
  • Sequential decision-making tasks
  • Reinforcement learning benchmarks
  • Text-based games
For tool-based tasks, use ToolEnv instead.

See Also