Skip to main content
PufferLib’s emulation API provides unified wrappers for Gymnasium and PettingZoo environments, converting complex observation and action spaces into flat arrays for efficient neural network processing.

GymnasiumPufferEnv

Wraps Gymnasium environments to work with PufferLib’s training infrastructure.

Constructor

GymnasiumPufferEnv(env=None, env_creator=None, env_args=[], env_kwargs={}, buf=None, seed=0)
env
gymnasium.Env
default:"None"
An existing Gymnasium environment instance. Mutually exclusive with env_creator.
env_creator
callable
default:"None"
A callable that creates a Gymnasium environment. Mutually exclusive with env.
env_args
list
default:"[]"
Positional arguments to pass to env_creator.
env_kwargs
dict
default:"{}"
Keyword arguments to pass to env_creator.
buf
dict
default:"None"
Optional buffer dictionary containing pre-allocated arrays for observations, rewards, terminals, truncations, masks, and actions.
seed
int
default:"0"
Random seed for the environment.

Properties

observation_space
gymnasium.Space
The emulated observation space (flattened to Box if needed).
action_space
gymnasium.Space
The emulated action space (converted to MultiDiscrete if needed).
single_observation_space
gymnasium.Space
Same as observation_space for single-agent environments.
single_action_space
gymnasium.Space
Same as action_space for single-agent environments.
num_agents
int
Always 1 for Gymnasium environments.
render_mode
str
The render mode of the wrapped environment.
emulated
dict
Dictionary containing emulation metadata:
  • observation_dtype: The flattened observation dtype
  • emulated_observation_dtype: The structured observation dtype

Methods

reset

reset(seed=None) -> tuple[np.ndarray, dict]
Resets the environment and returns the initial observation.
seed
int
default:"None"
Optional random seed for the reset.
Returns: Tuple of (observation, info)
observation
np.ndarray
The initial observation in emulated format.
info
dict
Additional information from the environment.

step

step(action) -> tuple[np.ndarray, float, bool, bool, dict]
Executes an action and returns the result.
action
np.ndarray | int
The action to execute in emulated format.
Returns: Tuple of (observation, reward, terminal, truncated, info)
observation
np.ndarray
The observation after taking the action.
reward
float
The reward received.
terminal
bool
Whether the episode terminated naturally.
truncated
bool
Whether the episode was truncated (e.g., time limit).
info
dict
Additional information from the environment.

render

render() -> np.ndarray | None
Renders the environment. Returns: RGB array if render_mode is ‘rgb_array’, None otherwise.

close

close()
Closes the environment and releases resources.

seed

seed(seed)
Sets the random seed for the environment.
seed
int
The random seed.

PettingZooPufferEnv

Wraps PettingZoo parallel environments to work with PufferLib’s training infrastructure.

Constructor

PettingZooPufferEnv(env=None, env_creator=None, env_args=[], env_kwargs={}, buf=None, seed=0)
env
pettingzoo.ParallelEnv
default:"None"
An existing PettingZoo parallel environment instance. Mutually exclusive with env_creator.
env_creator
callable
default:"None"
A callable that creates a PettingZoo parallel environment. Mutually exclusive with env.
env_args
list
default:"[]"
Positional arguments to pass to env_creator.
env_kwargs
dict
default:"{}"
Keyword arguments to pass to env_creator.
buf
dict
default:"None"
Optional buffer dictionary containing pre-allocated arrays for observations, rewards, terminals, truncations, masks, and actions.
seed
int
default:"0"
Random seed for the environment.

Properties

single_observation_space
gymnasium.Space
The emulated observation space for a single agent.
single_action_space
gymnasium.Space
The emulated action space for a single agent.
num_agents
int
Number of agents in the environment.
agents
list
List of currently active agent IDs.
possible_agents
list
List of all possible agent IDs.
render_mode
str
The render mode of the wrapped environment.
done
bool
Whether all agents are done.
emulated
dict
Dictionary containing emulation metadata:
  • observation_dtype: The flattened observation dtype
  • emulated_observation_dtype: The structured observation dtype

Methods

observation_space

observation_space(agent) -> gymnasium.Space
Returns the observation space for a specific agent.
agent
str
The agent ID.
Returns: The emulated observation space for the agent.

action_space

action_space(agent) -> gymnasium.Space
Returns the action space for a specific agent.
agent
str
The agent ID.
Returns: The emulated action space for the agent.

reset

reset(seed=None) -> tuple[dict, dict]
Resets the environment and returns initial observations for all agents.
seed
int
default:"None"
Optional random seed for the reset.
Returns: Tuple of (observations, infos)
observations
dict
Dictionary mapping agent IDs to their observations in emulated format.
infos
dict
Dictionary mapping agent IDs to their info dicts.

step

step(actions) -> tuple[dict, dict, dict, dict, dict]
Executes actions for all agents and returns the results.
actions
dict | np.ndarray
Either a dictionary mapping agent IDs to actions, or a numpy array with actions for all agents in order.
Returns: Tuple of (observations, rewards, terminals, truncations, infos)
observations
dict
Dictionary mapping agent IDs to their observations.
rewards
dict
Dictionary mapping agent IDs to their rewards.
terminals
dict
Dictionary mapping agent IDs to terminal flags.
truncations
dict
Dictionary mapping agent IDs to truncation flags.
infos
dict
Dictionary mapping agent IDs to their info dicts.

render

render() -> np.ndarray | None
Renders the environment. Returns: RGB array if render_mode is ‘rgb_array’, None otherwise.

close

close()
Closes the environment and releases resources.

Utility functions

emulate_observation_space

emulate_observation_space(space) -> tuple[gymnasium.Space, np.dtype]
Converts a complex observation space to a flat Box space.
space
gymnasium.Space
The original observation space (can be Box, Tuple, Dict, etc.).
Returns: Tuple of (emulated_space, struct_dtype)
emulated_space
gymnasium.spaces.Box
A flat Box space that can hold the original observations.
struct_dtype
np.dtype
Structured dtype for converting between flat and structured representations.

emulate_action_space

emulate_action_space(space) -> tuple[gymnasium.Space, np.dtype]
Converts a complex action space to MultiDiscrete or keeps Box/Discrete.
space
gymnasium.Space
The original action space.
Returns: Tuple of (emulated_space, struct_dtype)
emulated_space
gymnasium.Space
MultiDiscrete space for complex spaces, or the original for Box/Discrete.
struct_dtype
np.dtype
Structured dtype for converting between representations.

dtype_from_space

dtype_from_space(space) -> np.dtype
Creates a structured numpy dtype from a Gymnasium space.
space
gymnasium.Space
The space to convert.
Returns: Structured numpy dtype matching the space structure.

flatten_space

flatten_space(space) -> list[gymnasium.Space]
Flattens a nested space into a list of leaf spaces.
space
gymnasium.Space
The space to flatten.
Returns: List of leaf spaces (Box, Discrete, etc.).

emulate

emulate(struct, sample)
Copies a sample from a complex space into a structured array.
struct
np.ndarray
Structured numpy array view to write into.
sample
any
Sample from the original space (dict, tuple, or array).

nativize

nativize(arr, space, struct_dtype) -> any
Converts a flat array back to the original space format.
arr
np.ndarray
Flat array in emulated format.
space
gymnasium.Space
The original space.
struct_dtype
np.dtype
Structured dtype for conversion.
Returns: Sample in the original space format.

make_object

make_object(object_instance=None, object_creator=None, creator_args=[], creator_kwargs={}) -> any
Creates or validates an object instance.
object_instance
any
default:"None"
Existing object instance. Mutually exclusive with object_creator.
object_creator
callable
default:"None"
Callable that creates the object. Mutually exclusive with object_instance.
creator_args
list
default:"[]"
Positional arguments for object_creator.
creator_kwargs
dict
default:"{}"
Keyword arguments for object_creator.
Returns: The object instance.

Build docs developers (and LLMs) love