Skip to content

[RLlib] Fix broken rllib.algorithms docstring examples and make them run - #67775

Merged
pseudo-rnd-thoughts merged 2 commits into
masterfrom
doc-1926-rllib-algorithms-example-fixes
Nov 4, 2026
Merged

pseudo-rnd-thoughts merged 2 commits into
masterfrom
doc-1926-rllib-algorithms-example-fixes

Conversation

@dstrodtman

@dstrodtman dstrodtman commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

This is a correctness change for RLlib review. It fixes rllib.algorithms docstring examples that were wrong or couldn't run, and it converts the last testcode blocks in rllib/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, no testcode or testoutput remains in rllib/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, and APPOConfig runs tune.Tuner(...).fit(). Tune always prints its experiment table to stdout, even with verbose=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 shows results.num_errors as 0, so a failed trial fails the doctest. In MARWILConfig, print(f"beta: {config.beta}") now shows beta: 1.0, and the bare config calls discard their return value.

Examples that were broken and now run

BCConfig had these problems:

  • The data path ./rllib/offline/tests/data/cartpole/large.json no longer exists, because rllib/ moved to python/ray/rllib. The example now reads cartpole-v1_large through Path(__file__).parents[2], as MARWILConfig does. The doctest() target already ships that data.
  • build() failed, because an offline config without evaluation needs observation_space and action_space. Both now come from gym.make("CartPole-v1").
  • The second example passed beta=0.75, which BCConfig.validate rejects, because BC requires beta to be 0.0. That argument is gone, and print(config.beta) shows 0.0.
  • The Tuner had no stop condition. It now stops after one iteration.

+SKIP_EXAMPLE is gone.

Fixed but still render-only

These examples read offline/tests/data/pendulum/pendulum-v1_enormous, which the doctest() target doesn't ship, so they keep +SKIP_EXAMPLE. Adding that data to the target's data would let them run.

  • IQLConfig: the same path, spaces, and stop-condition fixes as BCConfig, with Pendulum data and spaces. print(config.beta) shows 0.1. It passes the strict check with +SKIP_EXAMPLE removed.
  • 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 uses Pendulum-v1 and reads the Pendulum data. lr=0.01 became lr=None, because CQL sets a learning rate per optimizer, and its validation rejects a base lr. print(config.to_dict()) stays as-is.

Possible RLlib bug, not fixed here

In the CQLConfig example, dataset_num_iters_per_learner=1 makes algo.train() fail every time, with KeyError: 0 in Learner._create_iterator_if_necessary. It works with 2 or 5, and BC, IQL, and MARWIL work with 1. The example uses 5, as examples/algorithms/cql/pendulum_cql.py does. 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_EXAMPLE keeps any of them render-only.

Review fixes

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, tensorflow 2.21, and pillow. For these runs, the cartpole-v1_large data was linked into the install, where the doctest() target's data puts it.

  • ci/ray_ci/doc/run_docstring_examples.py on all 8 changed files: "6 docstring(s) run, 0 failed, 2 skipped, in 8 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 6 runnable docstrings pass, and IQLConfig and CQLConfig carry +SKIP_EXAMPLE.
  • With +SKIP_EXAMPLE removed, IQLConfig passes the strict check. CQLConfig runs every statement, and only the to_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 --files on all 8 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. go add ONLY when ready to merge, run all tests labels Nov 3, 2026
@dstrodtman
dstrodtman marked this pull request as ready for review November 3, 2026 21:01
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 21:01

@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 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.

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.

medium

There is a typo in the comment: 'evalaution' should be 'evaluation'.

Suggested change
>>> # Set the config's environment for evalaution.
>>> # Set the config's environment for evaluation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed in 38b0d33.

Base automatically changed from doc-1926-rllib-algorithms-doctests to master November 4, 2026 15:51
dstrodtman and others added 2 commits November 4, 2026 11:17
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]>
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit da868b4 into master Nov 4, 2026
6 checks passed
@pseudo-rnd-thoughts
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]>
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