Gymnasium Spaces and Data Contracts
Use this sub-skill when a task involves choosing Gymnasium action or observation spaces, validating samples with contains, seeding or sampling spaces, flattening nested data for model inputs, unflattening model vectors back to structured observations/actions, or serializing space samples.
Start Here
- For constructor choices and data contracts, use
references/space-design.mdbefore modifying an environment'saction_spaceorobservation_space. - For
flatdim,flatten_space,flatten,unflatten,to_jsonable, andfrom_jsonable, usereferences/flattening-and-json.md. - For
Box.containssurprises, integer starts, nested flatten shape mismatches, dynamic spaces, and wrapper space updates, usereferences/troubleshooting.md. - To smoke-test the local Gymnasium install and demonstrate the core contract, run
python sub-skills/spaces-data/scripts/space_contract_smoke.py --helpand thenpython sub-skills/spaces-data/scripts/space_contract_smoke.pyfrom the root Gymnasium skill directory.
Scope Boundaries
- Use
../environment-api/SKILL.mdfor customEnvlifecycle,reset/step, registration,check_env, and seeding an environment throughreset(seed=...). - Use
../wrappers-recording/SKILL.mdwhen a wrapper changesobservation_space,action_space, observations, actions, rewards, rendering, or recording behavior. - Use
../vectorization/SKILL.mdfor batched/vector spaces,gym.vectorutilities,single_observation_space,single_action_space, and vector wrapper space transforms.
Minimum Correct Patterns
import numpy as np
from gymnasium import spaces
observation_space = spaces.Dict({
"position": spaces.Box(low=-1.0, high=1.0, shape=(2,), dtype=np.float32),
"inventory": spaces.Tuple((spaces.Discrete(5), spaces.MultiBinary(3))),
})
observation = observation_space.sample()
assert observation_space.contains(observation)
from gymnasium.spaces import utils as space_utils
flat = space_utils.flatten(observation_space, observation)
restored = space_utils.unflatten(observation_space, flat)
assert observation_space.contains(restored)
Keep runtime guidance self-contained. Do not depend on Gymnasium source, tests, examples, or documentation paths at runtime.