# Gym Migration Guide

## Who Should Read This Guide?

**If you’re new to Gymnasium**: You can probably skip this page! This guide is for users migrating from older versions of OpenAI Gym. If you’re just starting with RL, head to [Basic Usage](basic_usage.md) instead.

**If you’re migrating from OpenAI Gym**: This guide will help you update your code to work with Gymnasium. The changes are significant but straightforward once you understand the reasoning behind them.

**If you’re updating old tutorials**: Many online RL tutorials use the old v0.21 API. This guide shows you how to modernize that code.

**If you’re upgrading from Gym v0.26 or Gymnasium v0.26-v0.29**: Your code already uses the new step API, so jump to [Migrating from v0.26-v0.29 to v1.0](#migration-v1), which covers the smaller changes made in Gymnasium v1.0.

## Why Did the API Change?

Gymnasium is a fork of [OpenAI Gym v0.26](https://github.com/openai/gym/releases/tag/0.26.2), which introduced breaking changes from [Gym v0.21](https://github.com/openai/gym/releases/tag/v0.21.0). These changes weren’t made lightly - they solved important problems that made RL research and development more difficult.

The main issues with the old API were:

- **Ambiguous episode endings**: The single `done` flag couldn’t distinguish between “task completed” and “time limit reached”
- **Inconsistent seeding**: Random number generation was unreliable and hard to reproduce
- **Rendering complexity**: Switching between visual modes was unnecessarily complicated
- **Reproducibility problems**: Subtle bugs made it difficult to reproduce research results

For environments that can’t be updated, see [Compatibility Helpers](#migration-compatibility-helpers) below.

## Quick Reference: Complete Changes Table

| **Component**            | **v0.21 (Old)**                                   | **v0.26+ (New)**                                              | **Impact**      |
|--------------------------|---------------------------------------------------|---------------------------------------------------------------|-----------------|
| **Package Import**       | `import gym`                                      | `import gymnasium as gym`                                     | All code        |
| **Environment Reset**    | `obs = env.reset()`                               | `obs, info = env.reset()`                                     | Training loops  |
| **Random Seeding**       | `env.seed(42)`                                    | `env.reset(seed=42)`                                          | Reproducibility |
| **Step Function**        | `obs, reward, done, info = env.step(action)`      | `obs, reward, terminated, truncated, info = env.step(action)` | RL algorithms   |
| **Episode Ending**       | `while not done:`                                 | `while not (terminated or truncated):`                        | Training loops  |
| **Render Mode**          | `env.render(mode="human")`                        | `gym.make(env_id, render_mode="human")`                       | Visualization   |
| **Time Limit Detection** | `info.get('TimeLimit.truncated')`                 | `truncated` return value                                      | RL algorithms   |
| **Value Bootstrapping**  | `target = reward + (1-done) * gamma * next_value` | `target = reward + (1-terminated) * gamma * next_value`       | RL correctness  |

## Side-by-Side Code Comparison

### Old v0.21 Code

<!-- test: skip -->
```python
import gym

# Environment creation and seeding
env = gym.make("LunarLander-v2")
env.seed(123)
observation = env.reset()

# Training loop
done = False
while not done:
    action = env.action_space.sample()
    observation, reward, done, info = env.step(action)
    env.render(mode="human")

env.close()
```

### New v1.0.0+ Code

The same code works on v0.26 to v0.29 if `LunarLander-v3` is replaced with `LunarLander-v2`, as `LunarLander-v3` was added in v1.0.0.

```python
import gymnasium as gym  # Note: 'gymnasium' not 'gym'

# Environment creation with render mode specified upfront
env = gym.make("LunarLander-v3", render_mode="human")

# Reset with seed parameter
observation, info = env.reset(seed=123)

# Training loop with terminated/truncated distinction
done = False
while not done:
    action = env.action_space.sample()
    observation, reward, terminated, truncated, info = env.step(action)

    # Episode ends if either terminated OR truncated
    done = terminated or truncated

env.close()
```

## Key Changes Breakdown

### 1. Package Name Change

**Old**: `import gym`
**New**: `import gymnasium as gym`

Why: Gymnasium is a separate project that maintains and improves upon the original Gym codebase.

<!-- test: skip -->
```python
# OLD
import gym

# NEW
import gymnasium as gym
```

### 2. Seeding and Random Number Generation

The biggest conceptual change is how randomness is handled.

**Old v0.21**: Separate `seed()` method

<!-- test: skip -->
```python
env = gym.make("CartPole-v1")
env.seed(42)  # Set random seed
obs = env.reset()  # Reset environment
```

**New v0.26+**: Seed passed to `reset()`

```python
env = gym.make("CartPole-v1")
obs, info = env.reset(seed=42)  # Seed and reset together
```

**Why this changed**: Some environments (especially emulated games) can only set their random state at the beginning of an episode, not mid-episode. The old approach could lead to inconsistent behavior.

**Practical impact**:

<!-- test: skip -->
```python
# OLD: Seeding applied to all future episodes
env.seed(42)
for episode in range(10):
    obs = env.reset()

# NEW: Seed the first reset only; later resets continue from the seeded
# random number generator, so the whole run is still reproducible
obs, info = env.reset(seed=42)
for episode in range(10):
    # ... episode code ...
    obs, info = env.reset()
```

You can pass a seed to any reset (e.g. `seed=42 + episode`), but that is rarely needed: seeding once is enough for reproducibility.

### 3. Environment Reset Changes

**Old v0.21**: Returns only observation

<!-- test: skip -->
```python
observation = env.reset()
```

**New v0.26+**: Returns observation AND info

```python
observation, info = env.reset()
```

**Why this changed**:

- `info` provides consistent access to debugging information
- `seed` parameter enables reproducible episodes
- `options` parameter allows episode-specific configuration

**Common migration pattern**:

```python
# If you don't need the new features, just unpack the tuple
obs, _ = env.reset()  # Ignore info with underscore

# If you want to maintain the same random behavior as v0.21
env.reset(seed=42)  # Set seed once
# Then for subsequent resets:
obs, info = env.reset()  # Uses internal random state
```

### 4. Step Function: The `done` → `terminated`/`truncated` Split

This is the most important change for training algorithms.

**Old v0.21**: Single `done` flag

<!-- test: skip -->
```python
obs, reward, done, info = env.step(action)
```

**New v0.26+**: Separate `terminated` and `truncated` flags

```python
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
```

**Why this matters**:

- **`terminated`**: Episode ended because the task was completed or failed (agent reached goal, died, etc.)
- **`truncated`**: Episode ended due to external constraints (time limit, step limit, etc.)

This distinction is crucial for value function bootstrapping in RL algorithms:

<!-- test: skip -->
```python
# OLD (ambiguous)
if done:
    # Should we bootstrap? We don't know if this was natural termination or time limit!
    next_value = 0  # Assumption that may be wrong

# NEW (clear)
if terminated:
    next_value = 0      # Natural ending - no future value
elif truncated:
    next_value = value_function(next_obs)  # Time limit - estimate future value
```

**Migration strategy**:

<!-- test: skip -->
```python
# Simple migration (works for many cases)
next_obs, reward, terminated, truncated, info = env.step(action)
done = terminated or truncated  # use `done` only to decide when to reset

# Better migration (preserves RL algorithm correctness)
next_obs, reward, terminated, truncated, info = env.step(action)
# Only bootstrap from the next state if the episode didn't terminate.
# A truncated episode (e.g. time limit) still has future value.
target = reward + gamma * (1 - terminated) * value_function(next_obs)
```

For more information, see our [blog post](https://farama.org/Gymnasium-Terminated-Truncated-Step-API) about it.

### 5. Render Mode Changes

**Old v0.21**: Render mode specified each time

<!-- test: skip -->
```python
env = gym.make("CartPole-v1")
env.render(mode="human")     # Visual window
env.render(mode="rgb_array") # Get pixel array
```

**New v0.26+**: Render mode fixed at creation

```python
env = gym.make("CartPole-v1", render_mode="human")     # For visual display
env = gym.make("CartPole-v1", render_mode="rgb_array") # For recording
observation, info = env.reset()
frame = env.render()  # Uses the mode specified at creation
```

**Why this changed**: Some environments can’t switch render modes on-the-fly. Fixing the mode at creation enables better optimization and prevents bugs.

**Practical implications**:

<!-- test: skip -->
```python
# OLD: Could switch modes dynamically
env = gym.make("CartPole-v1")
for episode in range(10):
    # ... episode code ...
    if episode % 10 == 0:
        env.render(mode="human")  # Show every 10th episode

# NEW: Create separate environments for different purposes
training_env = gym.make("CartPole-v1")  # No rendering for speed
eval_env = gym.make("CartPole-v1", render_mode="human")  # Visual for evaluation

# Or use None for no rendering, then create visual env when needed
env = gym.make("CartPole-v1", render_mode=None)  # Fast training
if need_visualization:
    visual_env = gym.make("CartPole-v1", render_mode="human")
```

## TimeLimit Wrapper Changes

The [`TimeLimit`](../api/wrappers/misc_wrappers.md#gymnasium.wrappers.TimeLimit) wrapper behavior also changed to align with the new termination model.

**Old v0.21**: Added `TimeLimit.truncated` to info dict

<!-- test: skip -->
```python
obs, reward, done, info = env.step(action)
if done and info.get('TimeLimit.truncated', False):
    # Episode ended due to time limit
    pass
```

**New v0.26+**: Uses the `truncated` return value

<!-- test: skip -->
```python
obs, reward, terminated, truncated, info = env.step(action)
if truncated:
    # Episode ended due to time limit (or other truncation)
    pass
if terminated:
    # Episode ended naturally (success/failure)
    pass
```

This makes time limit detection much cleaner and more explicit.

## Environment-Specific Changes

### Removed Environments

Some environments were moved or removed:

<!-- test: skip -->
```python
# OLD: Robotics and Atari environments in main gym
import gym
env = gym.make("FetchReach-v1")  # No longer available
env = gym.make("Pong-v0")

# NEW: Moved to separate packages, which register their environments on import
import gymnasium

import ale_py
import gymnasium_robotics

# `register_envs` is a no-op, it only stops IDEs and linters removing the "unused" imports
gymnasium.register_envs(ale_py)
gymnasium.register_envs(gymnasium_robotics)

env = gymnasium.make("FetchReach-v4")
env = gymnasium.make("ALE/Pong-v5")
```

<a id="migration-v1"></a>

## Migrating from v0.26-v0.29 to v1.0

Gymnasium v1.0 kept the `reset` / `step` API from v0.26, so most code written for Gym v0.26 or Gymnasium v0.26-v0.29 only needs a few changes. The full list is in the [v1.0.0 release notes](https://github.com/Farama-Foundation/Gymnasium/releases/tag/v1.0.0).

### Third-party environments must be imported

Previously, installed packages such as `ale-py` registered their environments behind the scenes, so `gym.make("ALE/Pong-v5")` worked without importing them. This plugin system was removed, so import the package first:

<!-- test: skip -->
```python
import gymnasium as gym
import ale_py

gym.register_envs(ale_py)  # optional, stops IDEs and linters removing the import

env = gym.make("ALE/Pong-v5")

# or let `make` import the module for you with "module:env_id"
env = gym.make("ale_py:ALE/Pong-v5")
```

### Vector environments

- `gym.vector.make` was replaced by [`make_vec()`](../api/registry.md#gymnasium.make_vec), which uses the synchronous vectorizer by default (`vector.make` used the asynchronous one). Choose with `vectorization_mode="sync"` or `"async"`.
- [`vector.VectorEnv`](../api/vector.md#gymnasium.vector.VectorEnv) no longer inherits from [`Env`](../api/env.md#gymnasium.Env), so wrappers for single environments (`gymnasium.wrappers`) and vector environments (`gymnasium.wrappers.vector`) are now separate. The base vector wrapper `VectorEnvWrapper` was renamed to [`vector.VectorWrapper`](../api/vector/wrappers.md#gymnasium.vector.VectorWrapper).
- Autoreset changed. In v0.29, a sub-environment was reset in the same `step` that its episode ended, and the last observation was stored in `info["final_observation"]`. In v1.0 the default is `AutoresetMode.NEXT_STEP`: the sub-environment is reset during the *next* `step` call, which ignores that sub-environment’s action and returns its reset observation with a reward of 0. Skip that step’s data when training, as in the example below.

```python
import numpy as np
import gymnasium as gym

envs = gym.make_vec("CartPole-v1", num_envs=3, vectorization_mode="sync")

replay_buffer = []
obs, info = envs.reset(seed=42)
autoreset = np.zeros(envs.num_envs, dtype=bool)
for _ in range(200):
    actions = envs.action_space.sample()
    next_obs, rewards, terminations, truncations, infos = envs.step(actions)

    for j in range(envs.num_envs):
        # Skip the reset step of sub-environments whose episode ended last step
        if not autoreset[j]:
            replay_buffer.append(
                (obs[j], actions[j], rewards[j], terminations[j], truncations[j], next_obs[j])
            )

    obs = next_obs
    autoreset = np.logical_or(terminations, truncations)

envs.close()
```

To keep the v0.29 behaviour, where the reset happens in the same step and the last observation is in `infos["final_obs"]` (renamed from `"final_observation"`), pass `vector_kwargs={"autoreset_mode": gym.vector.AutoresetMode.SAME_STEP}` to `gym.make_vec`.

### Renamed and removed wrappers

| **v0.29**                                     | **v1.0**                                                                        |
|-----------------------------------------------|---------------------------------------------------------------------------------|
| `AutoResetWrapper`                            | `Autoreset`                                                                     |
| `FrameStack`                                  | `FrameStackObservation`                                                         |
| `PixelObservationWrapper`                     | `AddRenderObservation`                                                          |
| `VectorListInfo`                              | `vector.DictInfoToList`                                                         |
| `monitoring.VideoRecorder`                    | Removed, use `RecordVideo`                                                      |
| `StepAPICompatibility`, `EnvCompatibility`    | Removed, use [Shimmy](https://shimmy.farama.org/) for old Gym environments      |
| `gym.make(..., apply_api_compatibility=True)` | Removed, the argument is now passed to the environment and raises a `TypeError` |

### Accessing attributes through wrappers

Wrappers no longer forward unknown attributes to the environment they wrap (this was deprecated in v0.29), so `env.gravity` raises an `AttributeError` on a wrapped environment. Use `get_wrapper_attr` (or `env.unwrapped`) instead, and `set_wrapper_attr` to change a value:

```python
>>> import gymnasium as gym
>>> env = gym.make("CartPole-v1")
>>> env.get_wrapper_attr("gravity")
9.8
>>> env.unwrapped.gravity
9.8
>>> env.set_wrapper_attr("gravity", 5.0)
True
>>> env.unwrapped.gravity
5.0
```

### Environment versions

Some environments were updated with new version numbers, so update their IDs: `LunarLander-v2` → `LunarLander-v3`, `CarRacing-v2` → `CarRacing-v3`, and the MuJoCo environments gained `v5` versions (e.g. `Ant-v5`).

<a id="migration-compatibility-helpers"></a>

## Compatibility Helpers

### Loading OpenAI Gym environments

For environments that can’t be updated to Gymnasium, [Shimmy](https://shimmy.farama.org/) provides compatibility environments for v0.21 and v0.26 style environments, where either the environment name or the environment itself can be passed.

<!-- test: skip -->
```python
import gymnasium
import shimmy  # install shimmy with `pip install shimmy`

gymnasium.register_envs(shimmy)


# Gym v0.21 style environments
env = gymnasium.make("GymV21Environment-v0", env_id="CartPole-v1")
# or
env = gymnasium.make("GymV21Environment-v0", env=OldV21Env())

# Gym v0.26 style environments
env = gymnasium.make("GymV26Environment-v0", env_id="CartPole-v1")
# or
env = gymnasium.make("GymV26Environment-v0", env=OldV26Env())
```

### Step API Compatibility

If environments implement the (old) done step API, Gymnasium provides functions ([`convert_to_terminated_truncated_step_api()`](../api/utils.md#gymnasium.utils.step_api_compatibility.convert_to_terminated_truncated_step_api) and [`convert_to_done_step_api()`](../api/utils.md#gymnasium.utils.step_api_compatibility.convert_to_done_step_api)) that will convert a step’s return values from the old step API (using `done`) to the new step API (using `terminated` and `truncated`), and vice versa. [`step_api_compatibility()`](../api/utils.md#gymnasium.utils.step_api_compatibility.step_api_compatibility) can be used to convert to either API.

## Testing Your Migration

After migrating, verify that:

- [ ] **Import statements** use `gymnasium` instead of `gym`
- [ ] **Reset calls** handle the `(obs, info)` return format
- [ ] **Step calls** handle `terminated` and `truncated` separately
- [ ] **Render mode** is specified during environment creation
- [ ] **Random seeding** uses the `seed` parameter in `reset()`
- [ ] **Training algorithms** properly distinguish termination types

Use the [`check_env()`](../api/utils.md#gymnasium.utils.env_checker.check_env) to verify their implementation.

## Getting Help

**If you encounter issues during migration**:

1. **Check the [compatibility helpers](#migration-compatibility-helpers)**: Some old environments can be used with Shimmy’s compatibility environments
2. **Look at the environment documentation**: Each environment may have specific migration notes
3. **Test with simple environments first**: Start with CartPole before moving to complex environments
4. **Compare old vs new behavior**: Run the same code with both APIs to understand differences

**Common resources**:

- [Gymnasium API documentation](https://gymnasium.farama.org/api/env)
- [GitHub issues](https://github.com/Farama-Foundation/Gymnasium/issues) for bug reports
- [Discord community](https://discord.gg/bnJ6kubTg6) for questions
