Skip to content

[RLlib] Fix broken rllib.utils docstring examples and make them run - #67755

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

pseudo-rnd-thoughts merged 2 commits into
masterfrom
doc-1926-rllib-utils-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.utils docstring examples that were wrong or couldn't run, and it converts the last testcode blocks in rllib/utils. Those blocks needed a change to code, output, or prose, so they stayed out of the syntax-only conversion in #67753. After this PR, no testcode or testoutput remains in rllib/utils.

Stacked on #67753. 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.

  • numpy.flatten_inputs_to_1d_tensor: the example imported the function from tf_utils, not from numpy. It built Box(shape=...) without the low and high arguments Gymnasium requires, passed a ragged list for "b", and passed plain lists where the function needs NumPy arrays. The outputs now show what the function returns. The values are the same as before.
  • MeanStdFilter.apply_changes and MeanStdFilter.sync: running_stats.mean is a 0-d array, so the output reads array(1.5), not 1.5. a(1) returns the filtered value, which a doctest would echo, so the example now discards it.
  • _NotProvided: the example passed preprocessor_pref to .training(). AlgorithmConfig.env_runners() is the method that sets it. The printed default is deepmind, without quotes.
  • space_utils.get_base_struct_from_space and flatten_to_single_ndarray: the first example now imports the Gymnasium spaces and gives Box bounds. Both outputs are the real reprs. Before, they were pseudo-reprs such as dict(a=Box(), ...).
  • RemoteCallResults: the example used actors without defining it. It now defines the same three-actor setup as FaultTolerantActorManager.

testcode blocks this PR converts

  • FaultTolerantActorManager: the async half slept for two seconds and printed whatever was ready, so its output depended on timing. It now calls fetch_ready_async_reqs(timeout_seconds=None), which waits for every in-flight request, so it prints True three times. foreach_actor_async returns a count, which the example now discards.
  • ReplayBuffer: the prose said the True output was a Sphinx artifact. print(buffer._eviction_started) prints it, so the output now sits under that line, and the prose is gone. The buffer.sample(1) calls discard their result.
  • remove_last_ts_from_data, in postprocessing/episodes.py: the directive was ..testcode:: without a space, so it rendered as an rST comment. The example called PPOLearner._remove_last_ts_from_data, which doesn't exist. The function is remove_last_ts_from_data in the same module. The function returns a bare array when it gets one data item, not a tuple, so the first assertion now compares unpadded, not unpadded[0]. Returns now says so too.
  • unpad_data_if_necessary, in postprocessing/zero_padding.py: the directive had the same missing space. The example was correct and runs unchanged.
  • _find_offending_sub_space, in pre_checks/env.py: the example didn't parse. It's rebuilt from what was legible, a Box(-2.0, 1.5, (2,), np.int8) and the value np.array([-1.5, 3.0]), wrapped in a Dict space so there's a path to report. The function is private and isn't in the API reference.
  • MultiAgentMixInReplayBuffer: the five blocks are pseudocode (buffer.add(<A>)) with descriptive outputs, so they become python and text code blocks. The fourth block constructed MixInReplay, which doesn't exist. It now constructs MultiAgentMixInReplayBuffer.

Fixed but still render-only

  • tf_utils.flatten_inputs_to_1d_tensor: the same input fixes as the numpy version. The calls ran into TypeError before and run now. They stay +SKIP_EXAMPLE. Under RLlib's TensorFlow setup the tensors are graph-mode, so print(out) shows a tensor name, not the values the example shows.
  • annotations.py: two examples decorated with @overrides. The decorator is override.

tf_utils.one_hot, make_action_immutable, and the other annotations.py examples are unchanged and stay render-only.

Review fixes

These address gemini-code-assist comments on #67753:

  • torch_utils.flatten_inputs_to_1d_tensor: the "c" key of the example dict is now indented like "a" and "b".
  • MultiAgentEpisodeReplayBuffer and MultiAgentPrioritizedEpisodeReplayBuffer: the examples appended "__all__" to env.agents, which is the env's own list, so the env then listed "__all__" as an agent. They now build a new list.
  • The three episode replay buffer examples dropped the final episode if it terminated on the loop's last step, because the after-loop append only ran for unfinished episodes. The loop already appends every episode that ends inside it, so the last episode is now appended unconditionally.

Related issues

Stacked on #67753. 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 16 changed files: "18 docstring(s) run, 0 failed, 8 skipped, in 16 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 18 runnable docstrings pass, and the rest carry +SKIP_EXAMPLE. The seven runnable docstrings from [RLlib] Convert rllib.utils docstring examples to stock doctests #67753 pass this check too.
  • The two tf_utils examples run with +SKIP_EXAMPLE removed. Every call succeeds, and only the graph-mode output differs.
  • ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-utils-doctests: "16 file(s), docstrings only."
  • pre-commit run --files on all 16 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:56
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 20:56
@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 standardizing docstring examples and doctests across various RLlib utility modules, including actor manager, filters, replay buffers, and space utilities. It replaces outdated test blocks with standard doctest formats, fixes incorrect class and decorator references, and updates expected outputs to match actual runtime behavior. No review comments were provided, so there is no additional feedback to address.

Base automatically changed from doc-1926-rllib-utils-doctests to master November 4, 2026 15:05
dstrodtman and others added 2 commits November 4, 2026 11:24
Fixes the rllib.utils docstring examples that were wrong or couldn't run,
and converts the last `testcode` blocks in rllib/utils that needed a code,
output, or prose change. Fixed examples that run now drop
`+SKIP_EXAMPLE`, so the doctest target checks them.

@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
dstrodtman force-pushed the doc-1926-rllib-utils-example-fixes branch from 4e40202 to 9af43eb Compare November 4, 2026 16:26
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 520fff0 into master Nov 4, 2026
6 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-utils-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