Repository navigation
[RLlib] Fix broken rllib.env docstring examples and make them run - #67764
Merged
Merged
Conversation
dstrodtman
marked this pull request as ready for review
November 3, 2026 20:59
Contributor
There was a problem hiding this comment.
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.
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
force-pushed
the
doc-1926-rllib-env-example-fixes
branch
from
November 4, 2026 16:26
c712f82 to
df6ff8d
Compare
pseudo-rnd-thoughts
approved these changes
Nov 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This is a correctness change for RLlib review. It fixes
rllib.envdocstring 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: Trueand failed when run. Each now runs, and the+SKIP_EXAMPLEflag is gone, so the doctest target checks it.EnvContext.set_defaults:print(env_ctx)prints the worker fields too, such asworker=0/None, becauseEnvContext.__str__appends them. The example now printsdict(env_ctx), which gives the dict the example showed.make_multi_agent:reset()returns(obs, infos), soprint(obs)printed a tuple. The example also never reset the three-agent env, so its secondprint(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
SingleAgentEpisodegetter examples calledget_infos,get_actions,get_rewards, andget_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)returns2, not0.In the
SingleAgentEpisodeclass example, the comment expects[0.0, 0.0, 0.0, r0]when looking back four rewards from ts=1. Butslice(-4, 0)withneg_index_as_lookback=Truereturns four fill values. The example now usesslice(-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 passedenvas the first argument, but the method's first parameter isgroups. It now passes only the groups dict. The example uses a placeholder env, so it stays render-only.doc/source/rllib/multi-agent-envs.mdincludes this method's source, so the fix shows there too.PettingZooEnv: the example usedpettingzoo.butterfly.prison_v3. That env isn't in PettingZoo 1.24.3, the versionpython/requirements/ml/rllib-test-requirements.txtpins, or in 1.27.0. The example now usespistonball_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, andBaseEnvare unchanged and stay render-only. TheExternalEnvexample still passes the deprecatedenvargument toDQN.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, andtensorflow2.21:ci/ray_ci/doc/run_docstring_examples.pyon all 4 changed files: "10 docstring(s) run, 0 failed, 3 skipped, in 4 file(s)."doctestpass over the same docstrings withELLIPSIS. Unlike the runner above, it fails on unexpected output, the way the RLlibdoctest()target does. All 10 runnable docstrings pass, and the rest carry+SKIP_EXAMPLE.PettingZooEnvexample with+SKIP_EXAMPLEremoved, 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 --fileson all 4 files: all hooks pass.The PR carries the
docstring-onlylabel. Its commit carries an@rtd-buildline for the Read the Docs preview.AI assistance (Claude Code) was used for this PR.
🤖 Generated with Claude Code