GymEnv
A universal adapter for running OpenAI Gym-compatible environments with language models.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:Example Usage
CartPole Example
Custom Text Game
With Custom Parser
Built-in Rubric
EpisodicSumRubric
Default rubric that sums step rewards:state["trajectory"] to sum per-step rewards from the environment.
Key Methods
gym_to_hf
reset() on each episode:
obs_to_text
env_response
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:- Gym returns
done=Trueortruncated=True max_episode_stepsis reached- 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
See Also
- MultiTurnEnv - Parent class
- Rubric - Custom reward functions
- Gymnasium Documentation - Gym API reference