Implementation:Farama Foundation Gymnasium SyncVectorEnv
| Knowledge Sources | |
|---|---|
| Domains | Reinforcement_Learning, Parallelism |
| Last Updated | 2026-02-15 03:00 GMT |
Overview
Concrete tool for running multiple environments sequentially in a single process provided by the Gymnasium library.
Description
SyncVectorEnv creates and manages multiple environment instances in the main process. It instantiates environments from callables, batches their spaces using batch_space, and provides a unified VectorEnv interface. The copy parameter controls whether observations are copied (True, safe) or returned as views (False, faster but aliased).
Usage
Use SyncVectorEnv directly when you need custom environment factories with different configurations per sub-environment, or via gymnasium.make_vec with vectorization_mode="sync".
Code Reference
Source Location
- Repository: Gymnasium
- File: gymnasium/vector/sync_vector_env.py
- Lines: L26-120
Signature
class SyncVectorEnv(VectorEnv):
def __init__(
self,
env_fns: Iterator[Callable[[], Env]] | Sequence[Callable[[], Env]],
copy: bool = True,
observation_mode: str | Space = "same",
autoreset_mode: str | AutoresetMode = AutoresetMode.NEXT_STEP,
):
"""Vectorized environment that serially runs multiple environments.
Args:
env_fns: Iterable of callable functions that create the environments.
copy: If True, reset and step return copies of observations.
observation_mode: "same" (identical spaces) or "different" (varying bounds).
autoreset_mode: How to handle automatic resets on episode end.
"""
Import
from gymnasium.vector import SyncVectorEnv
import gymnasium as gym
envs = SyncVectorEnv([
lambda: gym.make("CartPole-v1"),
lambda: gym.make("CartPole-v1"),
])
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| env_fns | Sequence[Callable] | Yes | Factory functions that create environments |
| copy | bool | No | Copy observations (default True) |
| observation_mode | str | No | "same" or "different" (default "same") |
| autoreset_mode | AutoresetMode | No | Autoreset behavior (default NEXT_STEP) |
Outputs
| Name | Type | Description |
|---|---|---|
| num_envs | int | Number of sub-environments |
| single_observation_space | Space | Observation space of one sub-environment |
| single_action_space | Space | Action space of one sub-environment |
| observation_space | Space | Batched observation space |
| action_space | Space | Batched action space |
Usage Examples
Heterogeneous Environments
from gymnasium.vector import SyncVectorEnv
import gymnasium as gym
# Different gravity settings per environment
envs = SyncVectorEnv([
lambda: gym.make("Pendulum-v1", g=9.81),
lambda: gym.make("Pendulum-v1", g=1.62), # Moon gravity
lambda: gym.make("Pendulum-v1", g=3.72), # Mars gravity
])
obs, infos = envs.reset(seed=42)
print(obs.shape) # (3, 3) - 3 envs, 3-dim observation
envs.close()