Skill Tracer
Skills › Scientific Agent Skills › pufferlib › integration.md

integration.md

198 lines · skills/pufferlib/references/integration.md at 5f2bc03

Open on GitHub
1# Integration, Security, and Migration Guide
2
3Review: **2026-10-01**. Native 5.0 guidance is in [native-5.md](native-5.md);
43.0 and the pinned 4.0 snapshot below are historical profiles.
5

Integration, Security, and Migration Guide

Review: 2026-10-01. Native 5.0 guidance is in native-5.md; 3.0 and the pinned 4.0 snapshot below are historical profiles.

Raw lines 1 to 5
6## Compatibility matrix
7
8| Need | Published `pufferlib==3.0.0` | Historical `4.0` source |
9|---|---|---|
10| Gymnasium instance adaptation | `pufferlib.emulation.GymnasiumPufferEnv` | Removed from reviewed 4.0 source |
11| PettingZoo Parallel adaptation | `pufferlib.emulation.PettingZooPufferEnv` | Removed from reviewed 4.0 source |
12| Python vector backends | `pufferlib.vector` | Removed from reviewed 4.0 source |
13| Native Python `PufferEnv` | Supported | Replaced by historical C/Ocean interface |
14| Trainer | `pufferlib.pufferl.PuffeRL` | Native backend or `pufferlib.torch_pufferl.PuffeRL` |
15| External logging | W&B; Neptune historical/shut down | W&B in historical CLI |
16| Primary config | merged INI sections | different INI schema |
17| Checkpoints | Torch state dict plus trainer state | native `.bin`; Torch fallback state dict |
19Pin a profile. Do not import from a floating branch or blend examples across
20columns.

Compatibility matrix

NeedPublished pufferlib==3.0.0Historical 4.0 source
Gymnasium instance adaptationpufferlib.emulation.GymnasiumPufferEnvRemoved from reviewed 4.0 source
PettingZoo Parallel adaptationpufferlib.emulation.PettingZooPufferEnvRemoved from reviewed 4.0 source
Python vector backendspufferlib.vectorRemoved from reviewed 4.0 source
Native Python PufferEnvSupportedReplaced by historical C/Ocean interface
Trainerpufferlib.pufferl.PuffeRLNative backend or pufferlib.torch_pufferl.PuffeRL
External loggingW&B; Neptune historical/shut downW&B in historical CLI
Primary configmerged INI sectionsdifferent INI schema
CheckpointsTorch state dict plus trainer statenative .bin; Torch fallback state dict

Pin a profile. Do not import from a floating branch or blend examples across columns.

Raw lines 6 to 21
22## Correct stable adaptation patterns

Correct stable adaptation patterns

Raw lines 22 to 23
24### Gymnasium
26```python
27import gymnasium
28import pufferlib.emulation
29import pufferlib.vector
32def make_env(buf=None, seed=0):
33 raw = gymnasium.make("CartPole-v1")
34 return pufferlib.emulation.GymnasiumPufferEnv(raw, buf=buf, seed=seed)
37vecenv = pufferlib.vector.make(
38 make_env,
39 backend=pufferlib.vector.Serial,
40 num_envs=2,
41 seed=42,
43try:
44 observations, infos = vecenv.reset(seed=42)
45 vecenv.action_space.seed(43)
46 actions = vecenv.action_space.sample()
47 observations, rewards, terminals, truncations, infos = vecenv.step(actions)
48finally:
49 vecenv.close()
50```
52This source-reviewed pattern is illustrative, not a native PufferLib runtime
53test. The vectorizer injects `buf` and `seed` into creators; a zero-argument
54factory fails. Use Gymnasium 0.29.1/NumPy <2 with the published package, not
55the current Gymnasium 1.3 vector API.

Gymnasium

import gymnasium
import pufferlib.emulation
import pufferlib.vector


def make_env(buf=None, seed=0):
    raw = gymnasium.make("CartPole-v1")
    return pufferlib.emulation.GymnasiumPufferEnv(raw, buf=buf, seed=seed)


vecenv = pufferlib.vector.make(
    make_env,
    backend=pufferlib.vector.Serial,
    num_envs=2,
    seed=42,
)
try:
    observations, infos = vecenv.reset(seed=42)
    vecenv.action_space.seed(43)
    actions = vecenv.action_space.sample()
    observations, rewards, terminals, truncations, infos = vecenv.step(actions)
finally:
    vecenv.close()

This source-reviewed pattern is illustrative, not a native PufferLib runtime test. The vectorizer injects buf and seed into creators; a zero-argument factory fails. Use Gymnasium 0.29.1/NumPy <2 with the published package, not the current Gymnasium 1.3 vector API.

Raw lines 24 to 56
57### PettingZoo
59Use a reviewed Parallel environment instance:
61```python
62wrapped = pufferlib.emulation.PettingZooPufferEnv(reviewed_parallel_env)
63```
65The stable source does not document automatic AEC-to-Parallel conversion in
66this adapter. Convert explicitly with PettingZoo's supported utilities only
67when the environment's turn semantics permit it, then test action ordering,
68dead-agent handling, masks, and termination/truncation dictionaries.

PettingZoo

Use a reviewed Parallel environment instance:

wrapped = pufferlib.emulation.PettingZooPufferEnv(reviewed_parallel_env)

The stable source does not document automatic AEC-to-Parallel conversion in this adapter. Convert explicitly with PettingZoo's supported utilities only when the environment's turn semantics permit it, then test action ordering, dead-agent handling, masks, and termination/truncation dictionaries.

Raw lines 57 to 69
70### Native stable environment
72Subclass `pufferlib.PufferEnv`, define `single_observation_space`,
73`single_action_space`, and `num_agents` before `super().__init__`, then update
74the provided arrays in place. Native Puffer environments are already vector
75interfaces; do not return Gym's scalar four-tuple.

Native stable environment

Subclass pufferlib.PufferEnv, define single_observation_space, single_action_space, and num_agents before super().__init__, then update the provided arrays in place. Native Puffer environments are already vector interfaces; do not return Gym's scalar four-tuple.

Raw lines 70 to 76
77## Unsupported shortcuts from the old skill
79Remove or migrate these historical patterns:
81| Historical pattern | Current guidance |
82|---|---|
83| `pufferlib.make("name", ...)` | Stable: import an audited creator and use `pufferlib.vector.make`; 4.0: build/configure a named native environment |
84| `pufferlib.emulate(...)` | Stable: instantiate `GymnasiumPufferEnv` or `PettingZooPufferEnv` explicitly |
85| `pufferlib.vectorization.Serial` | Stable module is `pufferlib.vector.Serial` |
86| `from pufferlib import PuffeRL` | Stable trainer is `pufferlib.pufferl.PuffeRL`; 4.0 fallback is in `torch_pufferl` |
87| define native `observation_space`/`action_space` | Stable native class requires `single_observation_space`/`single_action_space` before `super()` |
88| return `(obs, reward, done, info)` | Return separate termination and truncation values |
89| native multi-agent dictionaries and `dones["__all__"]` | Use stable vector buffers or a reviewed PettingZoo Parallel adapter |
90| arbitrary dotted `entry_point` registration | Import an audited callable directly; bundled tools reject dotted paths |
91| top-level `WandbLogger`/`NeptuneLogger` | Stable logger classes live in `pufferlib.pufferl`; prefer the CLI and sanitized config |
92| assume Atari/Procgen/NetHack names exist everywhere | Verify the chosen version's config/source and install the separately reviewed environment |

Unsupported shortcuts from the old skill

Remove or migrate these historical patterns:

Historical patternCurrent guidance
pufferlib.make("name", ...)Stable: import an audited creator and use pufferlib.vector.make; 4.0: build/configure a named native environment
pufferlib.emulate(...)Stable: instantiate GymnasiumPufferEnv or PettingZooPufferEnv explicitly
pufferlib.vectorization.SerialStable module is pufferlib.vector.Serial
from pufferlib import PuffeRLStable trainer is pufferlib.pufferl.PuffeRL; 4.0 fallback is in torch_pufferl
define native observation_space/action_spaceStable native class requires single_observation_space/single_action_space before super()
return (obs, reward, done, info)Return separate termination and truncation values
native multi-agent dictionaries and dones["__all__"]Use stable vector buffers or a reviewed PettingZoo Parallel adapter
arbitrary dotted entry_point registrationImport an audited callable directly; bundled tools reject dotted paths
top-level WandbLogger/NeptuneLoggerStable logger classes live in pufferlib.pufferl; prefer the CLI and sanitized config
assume Atari/Procgen/NetHack names exist everywhereVerify the chosen version's config/source and install the separately reviewed environment
Raw lines 77 to 93
94## Migrating 3.0 to 4.0
96This is a redesign, not a drop-in upgrade:
981. Preserve the 3.0 lock, source digest, config, checkpoint hashes, and baseline
99 evaluation before changing anything.
1002. Inventory use of `emulation`, `vector`, `PufferEnv`, third-party environments,
101 policy wrappers, INI keys, logger flags, and Torch checkpoints.
1023. Decide whether the application should stay on published 3.0.0 or port to a
103 historical 4.0 C environment. The pinned 4.0 package tree has no Python
104 emulation/vector modules. For a new native port, use the separate 5.0 guide.
1054. Port environment logic to the reviewed Squared/Target C binding contract.
1065. Recreate configuration using 4.0 `[vec]`, `[policy]`, `[torch]`, and `[train]`
107 keys. Do not mechanically rename old keys.
1086. Rebuild policy composition around 4.0 encoder/decoder/network modules or the
109 native backend.
1107. Treat old `.pt` and new `.bin` files as incompatible unless an official,
111 tested converter says otherwise. Do not improvise binary conversion.
1128. Re-run contract, same-seed trace, throughput, and held-out learning
113 baselines. Attribute behavior changes; do not compare headline SPS alone.
115The historical 4.0 snapshot contains some stale 3.0-style examples even though the
116corresponding modules are absent. Prefer current implementation and docs over
117those copied examples.

Migrating 3.0 to 4.0

This is a redesign, not a drop-in upgrade:

  1. Preserve the 3.0 lock, source digest, config, checkpoint hashes, and baseline evaluation before changing anything.
  2. Inventory use of emulation, vector, PufferEnv, third-party environments, policy wrappers, INI keys, logger flags, and Torch checkpoints.
  3. Decide whether the application should stay on published 3.0.0 or port to a historical 4.0 C environment. The pinned 4.0 package tree has no Python emulation/vector modules. For a new native port, use the separate 5.0 guide.
  4. Port environment logic to the reviewed Squared/Target C binding contract.
  5. Recreate configuration using 4.0 [vec], [policy], [torch], and [train] keys. Do not mechanically rename old keys.
  6. Rebuild policy composition around 4.0 encoder/decoder/network modules or the native backend.
  7. Treat old .pt and new .bin files as incompatible unless an official, tested converter says otherwise. Do not improvise binary conversion.
  8. Re-run contract, same-seed trace, throughput, and held-out learning baselines. Attribute behavior changes; do not compare headline SPS alone.

The historical 4.0 snapshot contains some stale 3.0-style examples even though the corresponding modules are absent. Prefer current implementation and docs over those copied examples.

Raw lines 94 to 118
119## Third-party environments and native code
121Environment extras can pull old Gym versions, native libraries, renderers,
122emulators, datasets, model opponents, and ROM tooling. A package name in a
123PufferLib optional extra is not a security or license endorsement.
125Before install/import/build:
1271. Identify the official repository and immutable revision.
1282. Read build/install hooks and all network downloads.
1293. Verify licenses for code and assets separately.
1304. Verify hashes/attestations; record missing provenance.
1315. Use a disposable sandbox without credentials, home-directory mounts, or
132 network after required artifacts are staged.
1336. Cap processes, threads, memory, disk, render resolution, agents, and steps.
1347. Do not execute bundled native extensions, ROMs, checkpoints, or pickle files
135 until separately trusted.
137For Atari and similar systems, the user must supply legally obtained assets.
138Never download ROM sets or auto-accept a license on the user's behalf.

Third-party environments and native code

Environment extras can pull old Gym versions, native libraries, renderers, emulators, datasets, model opponents, and ROM tooling. A package name in a PufferLib optional extra is not a security or license endorsement.

Before install/import/build:

  1. Identify the official repository and immutable revision.
  2. Read build/install hooks and all network downloads.
  3. Verify licenses for code and assets separately.
  4. Verify hashes/attestations; record missing provenance.
  5. Use a disposable sandbox without credentials, home-directory mounts, or network after required artifacts are staged.
  6. Cap processes, threads, memory, disk, render resolution, agents, and steps.
  7. Do not execute bundled native extensions, ROMs, checkpoints, or pickle files until separately trusted.

For Atari and similar systems, the user must supply legally obtained assets. Never download ROM sets or auto-accept a license on the user's behalf.

Raw lines 119 to 139
140## Logging integration
142External tracking is disabled by default. The stable logger implementations can
143log the full argument mapping and upload model artifacts at close. The PyPI
1443.0.0 sdist has no CLI opt-out for that upload; use local logging or a reviewed
145custom logger for metrics-only tracking. Neptune hosted service is retired
146(2026-03-05); its token/integration names below are historical. Therefore:
148- sanitize arguments before logger construction;
149- keep `WANDB_API_KEY` and `NEPTUNE_API_TOKEN` only in an approved environment
150 injection or secret manager;
151- never add credential keys to nested INI/JSON/config objects;
152- do not pass a token on the command line;
153- disable model/source upload unless explicitly approved;
154- review project visibility, retention, residency, access controls, and cost;
155- use vendor offline/disabled modes only after confirming what is written
156 locally and how later sync behaves.
158Do not print all environment variables or recursively discover `.env` files.
159Checking whether one explicitly named credential variable exists can be
160acceptable; reading or logging its value is not.

Logging integration

External tracking is disabled by default. The stable logger implementations can log the full argument mapping and upload model artifacts at close. The PyPI 3.0.0 sdist has no CLI opt-out for that upload; use local logging or a reviewed custom logger for metrics-only tracking. Neptune hosted service is retired (2026-03-05); its token/integration names below are historical. Therefore:

  • sanitize arguments before logger construction;
  • keep WANDB_API_KEY and NEPTUNE_API_TOKEN only in an approved environment injection or secret manager;
  • never add credential keys to nested INI/JSON/config objects;
  • do not pass a token on the command line;
  • disable model/source upload unless explicitly approved;
  • review project visibility, retention, residency, access controls, and cost;
  • use vendor offline/disabled modes only after confirming what is written locally and how later sync behaves.

Do not print all environment variables or recursively discover .env files. Checking whether one explicitly named credential variable exists can be acceptable; reading or logging its value is not.

Raw lines 140 to 161
162## Integration acceptance test
164For each reviewed environment/profile:
1661. Create one instance without network or GPU.
1672. Validate spaces and reset return.
1683. Step a fixed action trace until both ordinary and episode-end paths run.
1694. Verify terminated/truncated semantics and final observation behavior.
1705. Close and confirm no child processes/resources remain.
1716. Run stable Serial or one 4.0 local native instance.
1727. Compare a same-seed trace.
1738. Scale to two workers/threads with small caps.
1749. Run policy shape and finite-value checks.
17510. Run held-out evaluation with logging still disabled.
177Only then consider GPU training, external logging, or larger parallelism.

Integration acceptance test

For each reviewed environment/profile:

  1. Create one instance without network or GPU.
  2. Validate spaces and reset return.
  3. Step a fixed action trace until both ordinary and episode-end paths run.
  4. Verify terminated/truncated semantics and final observation behavior.
  5. Close and confirm no child processes/resources remain.
  6. Run stable Serial or one 4.0 local native instance.
  7. Compare a same-seed trace.
  8. Scale to two workers/threads with small caps.
  9. Run policy shape and finite-value checks.
  10. Run held-out evaluation with logging still disabled.

Only then consider GPU training, external logging, or larger parallelism.

Raw lines 162 to 178
179## Sources
181- [PufferLib PyPI 3.0.0](https://pypi.org/project/pufferlib/3.0.0/) —
182 published 2025-06-23; accessed 2026-07-23.
183- [PufferLib 3.0 emulation source](https://github.com/PufferAI/PufferLib/blob/3.0/pufferlib/emulation.py)
184 — stable adapters; accessed 2026-07-23.
185- [PufferLib 3.0 vector source](https://github.com/PufferAI/PufferLib/blob/3.0/pufferlib/vector.py)
186 — stable vector API; accessed 2026-07-23.
187- [PufferLib 3.0 trainer source](https://github.com/PufferAI/PufferLib/blob/3.0/pufferlib/pufferl.py)
188 — stable logger/checkpoint behavior; accessed 2026-07-23.
189- [PufferLib 4.0 package tree](https://github.com/PufferAI/PufferLib/tree/25647630e1b15330bb3153a5a0d3ff8d234c3acf/pufferlib)
190 — historical modules; accessed 2026-07-23.
191- [PufferLib live 5.0 docs](https://puffer.ai/docs.html) — current native architecture;
192 checked 2026-10-01. Historical 4.0 module removal is verified from pinned source.
193- [PufferLib releases](https://github.com/PufferAI/PufferLib/releases) —
194 checked for source releases on 2026-07-23.
195- [Gymnasium Env API](https://gymnasium.farama.org/api/env/) — current
196 single-agent contract; accessed 2026-07-23.
197- [PyTorch security policy](https://github.com/pytorch/pytorch/security) —
198 model/native-package safety; accessed 2026-07-23.

Sources

Raw lines 179 to 198