# openai/gym architecture

> Architecture diagram of the GitHub repository openai/gym, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openai/gym
- Repository: https://github.com/openai/gym

## Overview

Gym provides a standard reinforcement-learning environment API and a catalog of environments. The principal workflow is to request a versioned environment with `gym.make`, then reset it and repeatedly step it using actions drawn from its action space, receiving observations and rewards. Environments span classic control, Box2D, MuJoCo, and toy-text domains. Core API wrappers transform environment interactions; spaces describe action and observation values, while vector environments support batched interaction and wrappers add rollout features such as video recording. Some subsystem links are documented rather than verified in sampled implementation excerpts.

Main entry points: [`gym/core.py`](https://github.com/openai/gym/blob/master/gym/core.py) (Environment contract), [`gym/spaces/space.py`](https://github.com/openai/gym/blob/master/gym/spaces/space.py) (Action and observation spaces), [`gym/envs/registration.py`](https://github.com/openai/gym/blob/master/gym/envs/registration.py) (Environment registry), [`gym/wrappers/record_video.py`](https://github.com/openai/gym/blob/master/gym/wrappers/record_video.py) (Video recording wrapper)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Environment API"]
  node_public_api["Gym API<br/>[__init__.py]"]
  node_registry["Environment registry<br/>[registration.py]"]
  node_env_contract["Environment contract<br/>[core.py]"]
  node_seeding["Random seeding<br/>[seeding.py]"]
end

subgraph group_domains["Environment Families"]
  node_classic["Classic control"]
  node_box2d["Box2D tasks"]
  node_mujoco["MuJoCo tasks"]
  node_toy_text["Toy-text tasks"]
end

subgraph group_spaces["Spaces and Batching"]
  node_spaces["Action and observation spaces<br/>[space.py]"]
  node_box_space["Continuous Box space<br/>[box.py]"]
  node_structured_spaces["Structured spaces<br/>[dict.py]"]
  node_vector_env["Vector environments<br/>[vector_env.py]"]
  node_async_vector["Async vector execution"]
  node_shared_memory["Shared observations<br/>[shared_memory.py]"]
end

subgraph group_rollouts["Rollout Utilities"]
  node_wrappers["Environment wrappers<br/>[core.py]"]
  node_compat["Step API compatibility"]
  node_checker["Passive environment checks"]
  node_video_wrapper["Video recording wrapper<br/>[record_video.py]"]
  node_video_recorder["Video recorder<br/>[video_recorder.py]"]
  node_logger["Logging<br/>[logger.py]"]
  node_colorize["Message colorization<br/>[colorize.py]"]
end

node_user(("RL user"))

node_user -->|"requests environment"| node_public_api
node_public_api -->|"exposes make"| node_registry
node_user -->|"selects environment"| node_registry
node_registry -->|"creates CartPole"| node_classic
node_classic -.->|"implements API"| node_env_contract
node_box2d -.->|"implements API"| node_env_contract
node_mujoco -.->|"implements API"| node_env_contract
node_toy_text -.->|"implements API"| node_env_contract
node_user -->|"resets and steps"| node_env_contract
node_env_contract -->|"declares spaces"| node_spaces
node_spaces -->|"provides Box"| node_box_space
node_spaces -->|"provides Dict"| node_structured_spaces
node_env_contract -->|"uses seeding"| node_seeding
node_env_contract -->|"defines wrapper types"| node_wrappers
node_vector_env -->|"batches spaces"| node_spaces
node_async_vector -->|"allocates observations"| node_shared_memory
node_shared_memory -->|"handles space types"| node_spaces
node_video_wrapper -->|"starts recorder"| node_video_recorder
node_video_wrapper -->|"wraps environment"| node_env_contract
node_logger -->|"colors messages"| node_colorize
node_env_contract -->|"imports compatibility helper"| node_compat
node_checker -->|"checks spaces"| node_spaces

click node_public_api "https://github.com/openai/gym/blob/master/gym/__init__.py"
click node_registry "https://github.com/openai/gym/blob/master/gym/envs/registration.py"
click node_env_contract "https://github.com/openai/gym/blob/master/gym/core.py"
click node_classic "https://github.com/openai/gym/tree/master/gym/envs/classic_control"
click node_box2d "https://github.com/openai/gym/tree/master/gym/envs/box2d"
click node_mujoco "https://github.com/openai/gym/tree/master/gym/envs/mujoco"
click node_toy_text "https://github.com/openai/gym/tree/master/gym/envs/toy_text"
click node_spaces "https://github.com/openai/gym/blob/master/gym/spaces/space.py"
click node_box_space "https://github.com/openai/gym/blob/master/gym/spaces/box.py"
click node_structured_spaces "https://github.com/openai/gym/blob/master/gym/spaces/dict.py"
click node_vector_env "https://github.com/openai/gym/blob/master/gym/vector/vector_env.py"
click node_async_vector "https://github.com/openai/gym/blob/master/gym/vector/async_vector_env.py"
click node_shared_memory "https://github.com/openai/gym/blob/master/gym/vector/utils/shared_memory.py"
click node_wrappers "https://github.com/openai/gym/blob/master/gym/core.py"
click node_compat "https://github.com/openai/gym/blob/master/gym/utils/step_api_compatibility.py"
click node_checker "https://github.com/openai/gym/blob/master/gym/utils/passive_env_checker.py"
click node_video_wrapper "https://github.com/openai/gym/blob/master/gym/wrappers/record_video.py"
click node_video_recorder "https://github.com/openai/gym/blob/master/gym/wrappers/monitoring/video_recorder.py"
click node_logger "https://github.com/openai/gym/blob/master/gym/logger.py"
click node_colorize "https://github.com/openai/gym/blob/master/gym/utils/colorize.py"
click node_seeding "https://github.com/openai/gym/blob/master/gym/utils/seeding.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_registry,node_env_contract,node_seeding,node_user toneBlue
class node_classic,node_box2d,node_mujoco,node_toy_text toneAmber
class node_spaces,node_box_space,node_structured_spaces,node_vector_env,node_async_vector,node_shared_memory toneMint
class node_wrappers,node_compat,node_checker,node_video_wrapper,node_video_recorder,node_logger,node_colorize toneRose
```

## Components

### Environment API

- [Gym API](https://github.com/openai/gym/blob/master/gym/__init__.py): `gym/__init__.py`
- [Environment registry](https://github.com/openai/gym/blob/master/gym/envs/registration.py): `gym/envs/registration.py`
- [Environment contract](https://github.com/openai/gym/blob/master/gym/core.py): `gym/core.py`
- [Random seeding](https://github.com/openai/gym/blob/master/gym/utils/seeding.py): `gym/utils/seeding.py`

### Environment Families

- [Classic control](https://github.com/openai/gym/tree/master/gym/envs/classic_control): `gym/envs/classic_control`
- [Box2D tasks](https://github.com/openai/gym/tree/master/gym/envs/box2d): `gym/envs/box2d`
- [MuJoCo tasks](https://github.com/openai/gym/tree/master/gym/envs/mujoco): `gym/envs/mujoco`
- [Toy-text tasks](https://github.com/openai/gym/tree/master/gym/envs/toy_text): `gym/envs/toy_text`

### Spaces and Batching

- [Action and observation spaces](https://github.com/openai/gym/blob/master/gym/spaces/space.py): `gym/spaces/space.py`
- [Continuous Box space](https://github.com/openai/gym/blob/master/gym/spaces/box.py): `gym/spaces/box.py`
- [Structured spaces](https://github.com/openai/gym/blob/master/gym/spaces/dict.py): `gym/spaces/dict.py`
- [Vector environments](https://github.com/openai/gym/blob/master/gym/vector/vector_env.py): `gym/vector/vector_env.py`
- [Async vector execution](https://github.com/openai/gym/blob/master/gym/vector/async_vector_env.py): `gym/vector/async_vector_env.py`
- [Shared observations](https://github.com/openai/gym/blob/master/gym/vector/utils/shared_memory.py): `gym/vector/utils/shared_memory.py`

### Rollout Utilities

- [Environment wrappers](https://github.com/openai/gym/blob/master/gym/core.py): `gym/core.py`
- [Step API compatibility](https://github.com/openai/gym/blob/master/gym/utils/step_api_compatibility.py): `gym/utils/step_api_compatibility.py`
- [Passive environment checks](https://github.com/openai/gym/blob/master/gym/utils/passive_env_checker.py): `gym/utils/passive_env_checker.py`
- [Video recording wrapper](https://github.com/openai/gym/blob/master/gym/wrappers/record_video.py): `gym/wrappers/record_video.py`
- [Video recorder](https://github.com/openai/gym/blob/master/gym/wrappers/monitoring/video_recorder.py): `gym/wrappers/monitoring/video_recorder.py`
- [Logging](https://github.com/openai/gym/blob/master/gym/logger.py): `gym/logger.py`
- [Message colorization](https://github.com/openai/gym/blob/master/gym/utils/colorize.py): `gym/utils/colorize.py`

### Other components

- **RL user**

## Connections

- RL user → Gym API: requests environment (evidence: `README.md`)
- Gym API → Environment registry: exposes make (evidence: `gym/__init__.py`)
- RL user → Environment registry: selects environment (evidence: `README.md`)
- Environment registry → Classic control: creates CartPole (evidence: `README.md`)
- Classic control → Environment contract: implements API (evidence: `gym/envs/classic_control/acrobot.py`)
- Box2D tasks → Environment contract: implements API
- MuJoCo tasks → Environment contract: implements API
- Toy-text tasks → Environment contract: implements API
- RL user → Environment contract: resets and steps (evidence: `README.md`)
- Environment contract → Action and observation spaces: declares spaces
- Action and observation spaces → Continuous Box space: provides Box (evidence: `gym/spaces/__init__.py`)
- Action and observation spaces → Structured spaces: provides Dict (evidence: `gym/spaces/__init__.py`)
- Environment contract → Random seeding: uses seeding (evidence: `gym/core.py`)
- Environment contract → Environment wrappers: defines wrapper types (evidence: `gym/core.py`)
- Vector environments → Action and observation spaces: batches spaces (evidence: `gym/vector/vector_env.py`)
- Async vector execution → Shared observations: allocates observations (evidence: `gym/vector/async_vector_env.py`)
- Shared observations → Action and observation spaces: handles space types (evidence: `gym/vector/utils/shared_memory.py`)
- Video recording wrapper → Video recorder: starts recorder (evidence: `gym/wrappers/record_video.py`)
- Video recording wrapper → Environment contract: wraps environment (evidence: `gym/wrappers/record_video.py`)
- Logging → Message colorization: colors messages (evidence: `gym/logger.py`)
- Environment contract → Step API compatibility: imports compatibility helper (evidence: `gym/core.py`)
- Passive environment checks → Action and observation spaces: checks spaces (evidence: `gym/utils/passive_env_checker.py`)
