Implementation:Farama Foundation Gymnasium Vector ArrayConversion
| Knowledge Sources | |
|---|---|
| Domains | Reinforcement_Learning, Wrappers |
| Last Updated | 2026-02-15 03:00 GMT |
Overview
A vector environment wrapper for converting between Array API compatible frameworks (numpy, torch, jax.numpy, cupy, etc.), serving as the vectorized counterpart of the single-environment ArrayConversion wrapper.
Description
The ArrayConversion vector wrapper converts all inputs and outputs of a vector environment between two Array API compatible frameworks. Unlike the single-environment version, this wrapper also converts rewards, terminations, and truncations (which are arrays in vector environments) rather than casting them to Python scalars.
The wrapper:
- Converts actions from the target framework to the environment framework before stepping.
- Converts observations, rewards, terminations, truncations, and info from the environment framework to the target framework after stepping and resetting.
- Converts options to the environment framework during reset.
- Supports pickling via
__getstate__/__setstate__with string-based module name serialization.
Uses the array_conversion singledispatch function from gymnasium.wrappers.array_conversion for the actual conversions.
Usage
Use this wrapper when you have a vectorized environment using one Array API framework but your training code uses another. This is the vector equivalent of gymnasium.wrappers.ArrayConversion.
Code Reference
Source Location
- Repository: Farama_Foundation_Gymnasium
- File:
gymnasium/wrappers/vector/array_conversion.py
Signature
class ArrayConversion(VectorWrapper, gym.utils.RecordConstructorArgs):
def __init__(
self,
env: VectorEnv,
env_xp: ModuleType | str,
target_xp: ModuleType | str,
env_device: Device | None = None,
target_device: Device | None = None,
): ...
Import
from gymnasium.wrappers.vector import ArrayConversion
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| env | VectorEnv | Yes | The Array API compatible vector environment to wrap |
| env_xp | ModuleType or str | Yes | The Array API framework the environment uses |
| target_xp | ModuleType or str | Yes | The Array API framework to convert outputs to |
| env_device | Device or None | No | The device the environment is on (default None) |
| target_device | Device or None | No | The device for output arrays (default None) |
Outputs
| Name | Type | Description |
|---|---|---|
| observations | target_xp array | Observations converted to target framework |
| rewards | target_xp array | Rewards converted to target framework array |
| terminations | target_xp array | Termination flags converted to target framework array |
| truncations | target_xp array | Truncation flags converted to target framework array |
| info | dict | Info dict with values converted to target framework |
Usage Examples
import numpy as np
import gymnasium as gym
from gymnasium.wrappers.vector import ArrayConversion
# Convert a JAX vector environment to return NumPy arrays
envs = gym.make_vec("JaxEnv-vx", num_envs=3)
envs = ArrayConversion(envs, env_xp="jax.numpy", target_xp="numpy")
obs, info = envs.reset(seed=123)
type(obs) # <class 'numpy.ndarray'>