Skip to content

[RLlib] Fix broken rllib.env docstring examples and make them run - #67764

Merged
pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-env-example-fixes
Nov 4, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-env-example-fixes

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Description

This is a correctness change for RLlib review. It fixes rllib.env docstring examples that were wrong or couldn't run.

Stacked on #67763. Review that PR first. This diff is against its branch.

Examples that were broken and now run

Before this PR, each of these was :skipif: True and failed when run. Each now runs, and the +SKIP_EXAMPLE flag is gone, so the doctest target checks it.

  • EnvContext.set_defaults: print(env_ctx) prints the worker fields too, such as worker=0/None, because EnvContext.__str__ appends them. The example now prints dict(env_ctx), which gives the dict the example showed.
  • make_multi_agent: reset() returns (obs, infos), so print(obs) printed a tuple. The example also never reset the three-agent env, so its second print(obs) reprinted the first env's observations. Both resets now unpack the tuple, and the outputs show each agent's observation array.

Getter examples now check their values

The SingleAgentEpisode getter examples called get_infos, get_actions, get_rewards, and get_extra_model_outputs, and each one gave its result in a comment. The calls now show their output, so the doctest checks it. Four comments were wrong:

  • get_actions(slice(-5, -2), fill=-9) returns three items, [array(-9), array(-9), 1], not four.
  • get_actions(slice(0, 2), one_hot_discrete=True) returns the one-hot vectors for actions 1 and 2, not 1 and 3.
  • get_rewards(slice(-5, -2), fill=0.0) returns three items, not four.
  • get_extra_model_outputs("mo", 1) returns 2, not 0.

In the SingleAgentEpisode class example, the comment expects [0.0, 0.0, 0.0, r0] when looking back four rewards from ts=1. But slice(-4, 0) with neg_index_as_lookback=True returns four fill values. The example now uses slice(-3, 1), which returns what the comment describes, and it shows that output.

In these outputs, fill values come back as 0-d NumPy arrays, such as array(-9), mixed with plain Python values. The examples show what the getters return today.

Fixed but still render-only

  • MultiAgentEnv.with_agent_groups: the example passed env as the first argument, but the method's first parameter is groups. It now passes only the groups dict. The example uses a placeholder env, so it stays render-only. doc/source/rllib/multi-agent-envs.md includes this method's source, so the fix shows there too.
  • PettingZooEnv: the example used pettingzoo.butterfly.prison_v3. That env isn't in PettingZoo 1.24.3, the version python/requirements/ml/rllib-test-requirements.txt pins, or in 1.27.0. The example now uses pistonball_v6, as the RLlib multi-agent envs page does. The outputs come from running the example with PettingZoo 1.27.0. It stays render-only, because PettingZoo isn't installed in the doctest environment.

ExternalEnv, MultiAgentEnv.step, MultiAgentEnv.reset, and BaseEnv are unchanged and stay render-only. The ExternalEnv example still passes the deprecated env argument to DQN.

Related issues

Stacked on #67763. Related to #66753.

Additional information

Not a duplicate. I searched open PRs for RLlib testcode, doctest, and docstring-example changes, and none touches these docstrings.

Tests run

These ran in a Python 3.12 virtualenv with ray[rllib]==2.59.0, torch, and tensorflow 2.21:

  • ci/ray_ci/doc/run_docstring_examples.py on all 4 changed files: "10 docstring(s) run, 0 failed, 3 skipped, in 4 file(s)."
  • A strict stdlib doctest pass over the same docstrings with ELLIPSIS. Unlike the runner above, it fails on unexpected output, the way the RLlib doctest() target does. All 10 runnable docstrings pass, and the rest carry +SKIP_EXAMPLE.
  • The PettingZooEnv example with +SKIP_EXAMPLE removed, in the same environment plus PettingZoo 1.27.0: all 11 examples pass. I didn't verify it end to end against the pinned 1.24.3.
  • ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-env-doctests: "4 file(s), docstrings only."
  • pre-commit run --files on all 4 files: all hooks pass.

The PR carries the docstring-only label. Its commit carries an @rtd-build line for the Read the Docs preview.

AI assistance (Claude Code) was used for this PR.

🤖 Generated with Claude Code

@dstrodtman dstrodtman added rllib RLlib related issues docstring-only Skip heavy library tests for Ray API docstring edits. labels Nov 3, 2026
@dstrodtman
dstrodtman marked this pull request as ready for review November 3, 2026 20:59
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 20:59
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Nov 3, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request focuses on updating, correcting, and enabling doctests across several RLlib environment-related files, including env_context.py, multi_agent_env.py, single_agent_episode.py, and pettingzoo_env.py. The changes ensure that the examples are executable and accurately reflect the return types and structures of the underlying methods. There are no review comments to address, and I have no additional feedback to provide.

Base automatically changed from doc-1926-rllib-env-doctests to master November 4, 2026 15:19
Fix `rllib/env` docstring examples that were wrong or couldn't run.
`EnvContext.set_defaults` and `make_multi_agent` now run as doctests.
The `SingleAgentEpisode` getter examples show checked output in place of
comments, several of which gave the wrong value. `with_agent_groups` no
longer passes the env twice, and the `PettingZooEnv` example uses
`pistonball_v6`, since PettingZoo no longer ships `prison_v3`.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman force-pushed the doc-1926-rllib-env-example-fixes branch from c712f82 to df6ff8d Compare November 4, 2026 16:26
@pseudo-rnd-thoughts
pseudo-rnd-thoughts enabled auto-merge (squash) November 4, 2026 16:38
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit e00326c into master Nov 4, 2026
7 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-env-example-fixes branch November 4, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docstring-only Skip heavy library tests for Ray API docstring edits. go add ONLY when ready to merge, run all tests rllib RLlib related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants