Repository navigation
[RLlib] Fix broken rllib.algorithms docstring examples and make them run - #67775
Merged
pseudo-rnd-thoughts merged 2 commits intoNov 4, 2026
Merged
Conversation
dstrodtman
marked this pull request as ready for review
November 3, 2026 21:01
Contributor
There was a problem hiding this comment.
Code Review
This pull request converts Sphinx .. testcode:: and .. testoutput:: blocks into standard Python doctests across several RLlib algorithm configuration files, including APPO, BC, CQL, DQN, IMPALA, IQL, MARWIL, and PPO. It also updates paths, environments, and configurations within these examples to ensure they are fully runnable. The feedback points out a minor typo ('evalaution' instead of 'evaluation') in a comment within marwil.py.
| ... # per training step. | ||
| ... dataset_num_iters_per_learner=1, | ||
| ... ) | ||
| >>> # Set the config's environment for evalaution. |
Contributor
Fix the rllib/algorithms config class examples that never ran and were broken, and convert the last `testcode` blocks in rllib/algorithms. The Tune examples now stop after one iteration and check `results.num_errors`, the offline examples read data that exists and set the spaces RLlib requires, and BC and its Tune example now pass validation. IQL and CQL stay render-only because the doctest target doesn't ship the Pendulum data they read. @rtd-build Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Douglas Strodtman <[email protected]>
Fixes issues gemini-code-assist found in the converted examples: unused or duplicated imports and variables, example code that mutates shared state or drops data, and a typo. @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-algorithms-example-fixes
branch
from
November 4, 2026 16:24
38b0d33 to
c0cd843
Compare
pseudo-rnd-thoughts
approved these changes
Nov 4, 2026
pseudo-rnd-thoughts
deleted the
doc-1926-rllib-algorithms-example-fixes
branch
November 4, 2026 22:51
dstrodtman
added a commit
that referenced
this pull request
Nov 5, 2026
…67834) ## Description `//python/ray/rllib:doctest` fails on master in the `APPOConfig` and `IMPALAConfig` examples. #67774 made these examples runnable. Each one builds an algorithm with `learners(num_learners=1)`, and the learner worker sets up a Gloo process group and prints this line: ``` (_WrappedExecutable pid=5360) [Gloo] Rank 0 is connected to 0 peer ranks. Expected number of connected peer ranks is : 0 ``` Ray forwards worker output to the driver's stdout asynchronously. Stock doctest compares the driver's stdout against each example's expected output, so the line fails `algo = config.build()`, which expects nothing. Both bazel attempts failed this way in [premerge build 76224](https://buildkite.com/ray-project/premerge/builds/76224) on #67627, which doesn't touch these files. Any RLlib PR that runs the full doctest target without the `docstring-only` label hits the same failure. #67774 passed premerge because it ran with `docstring-only`, which runs docstring examples in a light image without torch and skips these examples by name. This PR sets `RAY_LOG_TO_DRIVER=0` in the target's `env`. `ray.init()` reads that constant when it isn't passed `log_to_driver`, and these examples don't pass it. #67831 sets the same variable on the Train V2 doctest target for the same Gloo output from `TorchTrainer` workers. No RLlib docstring example expects worker output: a search of `python/ray/rllib` outside `examples/` and `tests/` for `pid=` and worker-actor prefixes finds none. The examples themselves don't change. **Not a duplicate.** I searched open PRs for `APPOConfig`, `IMPALAConfig`, doctest, and Gloo. #67775 is the only match. It changes the second, Tune-based example in each of these docstrings and doesn't touch the first example or `BUILD.bazel`. ## Testing - `pre-commit run --files python/ray/rllib/BUILD.bazel`: all hooks pass, including buildifier and buildifier-lint. - On macOS arm64 with a Ray nightly wheel, master's `appo.py` and `impala.py`, and Ray's default doctest plugin: `pytest -p default_doctest_pytest_plugin --doctest-modules --noconftest ray/rllib/algorithms/appo/appo.py ray/rllib/algorithms/impala/impala.py` passes with and without `RAY_LOG_TO_DRIVER=0` (2 passed each time). The Gloo line doesn't appear on macOS, so this run shows the change doesn't break the examples but doesn't reproduce the failure. This PR's premerge, which runs the full RLlib doctest target on Linux, is the real check. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Douglas Strodtman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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.algorithmsdocstring examples that were wrong or couldn't run, and it converts the lasttestcodeblocks inrllib/algorithms. Those blocks needed a change to code, output, or prose, so they stayed out of the syntax-only conversion in #67774. After this PR, notestcodeortestoutputremains inrllib/algorithms.Stacked on #67774. Review that PR first. This diff is against its branch.
Tune examples that now check their result
The second example in each of
IMPALAConfig,MARWILConfig,DQNConfig,PPOConfig, andAPPOConfigrunstune.Tuner(...).fit(). Tune always prints its experiment table to stdout, even withverbose=0, so a doctest can't check the exact output.fit()doesn't raise when a trial fails, so the old:hide:output of...checked nothing.Each example now uses the convention from Ray Tune's own docstrings, such as
tune/stopper/stopper.py:print("[ignore]"); results = tune.Tuner(...).fit() # doctest: +ELLIPSIS, with[ignore]...as the output. Each then showsresults.num_errorsas0, so a failed trial fails the doctest. InMARWILConfig,print(f"beta: {config.beta}")now showsbeta: 1.0, and the bare config calls discard their return value.Examples that were broken and now run
BCConfighad these problems:./rllib/offline/tests/data/cartpole/large.jsonno longer exists, becauserllib/moved topython/ray/rllib. The example now readscartpole-v1_largethroughPath(__file__).parents[2], asMARWILConfigdoes. Thedoctest()target already ships that data.build()failed, because an offline config without evaluation needsobservation_spaceandaction_space. Both now come fromgym.make("CartPole-v1").beta=0.75, whichBCConfig.validaterejects, because BC requiresbetato be 0.0. That argument is gone, andprint(config.beta)shows0.0.Tunerhad no stop condition. It now stops after one iteration.+SKIP_EXAMPLEis gone.Fixed but still render-only
These examples read
offline/tests/data/pendulum/pendulum-v1_enormous, which thedoctest()target doesn't ship, so they keep+SKIP_EXAMPLE. Adding that data to the target'sdatawould let them run.IQLConfig: the same path, spaces, and stop-condition fixes asBCConfig, with Pendulum data and spaces.print(config.beta)shows0.1. It passes the strict check with+SKIP_EXAMPLEremoved.CQLConfig: the example built on CartPole with no offline data, and it failed on the replay buffer type. CQL learns continuous actions, so the example now usesPendulum-v1and reads the Pendulum data.lr=0.01becamelr=None, because CQL sets a learning rate per optimizer, and its validation rejects a baselr.print(config.to_dict())stays as-is.Possible RLlib bug, not fixed here
In the
CQLConfigexample,dataset_num_iters_per_learner=1makesalgo.train()fail every time, withKeyError: 0inLearner._create_iterator_if_necessary. It works with 2 or 5, and BC, IQL, and MARWIL work with 1. The example uses 5, asexamples/algorithms/cql/pendulum_cql.pydoes. I didn't look into the cause.CI time. The six runnable docstrings build an algorithm and train it, and the five Tune examples each take 15 to 28 seconds on CPU. Together they add about 150 seconds to the RLlib
doctest()target. If that's too much, a docstring-level+SKIP_EXAMPLEkeeps any of them render-only.Review fixes
BCConfigandIQLConfig: each docstring holds two examples, and they ran together. A blank line now separates them. From gemini-code-assist comments on [RLlib] Convert rllib.algorithms docstring examples to stock doctests #67774.MARWILConfig: fixed the typo "evalaution". From a gemini-code-assist comment.Related issues
Stacked on #67774. 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,tensorflow2.21, andpillow. For these runs, thecartpole-v1_largedata was linked into the install, where thedoctest()target'sdataputs it.ci/ray_ci/doc/run_docstring_examples.pyon all 8 changed files: "6 docstring(s) run, 0 failed, 2 skipped, in 8 file(s)."doctestpass over the same docstrings withELLIPSIS. Unlike the runner above, it fails on unexpected output, the way the RLlibdoctest()target does. All 6 runnable docstrings pass, andIQLConfigandCQLConfigcarry+SKIP_EXAMPLE.+SKIP_EXAMPLEremoved,IQLConfigpasses the strict check.CQLConfigruns every statement, and only theto_dict()print has no expected output.ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-algorithms-doctests: "8 file(s), docstrings only."pre-commit run --fileson all 8 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