Skip to content

[RLlib] Convert rllib.models, execution, offline, and callbacks docstring examples to stock doctests - #67761

Merged
pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-models-execution-doctests
Nov 4, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-models-execution-doctests

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Description

Converts the .. testcode:: examples in the python/ray/rllib/models, execution, offline, and callbacks docstrings to >>> examples under a plural Examples: header. Sphinx renders .. testcode::, but griffe and MkDocs don't, and stock >>> examples render in both. This follows the rllib.utils conversion in #67753. The four subpackages are small, so they share one PR.

This PR covers 17 of the 18 testcode blocks in these subpackages, in 9 files:

  • 3 runnable blocks become runnable doctests. They're synchronous_parallel_sample in execution/rollout_ops.py and the two RLlibCallback examples in callbacks/callbacks.py. None of these ran before, because the RLlib doctest() target doesn't execute testcode. All three pass now.
  • 7 never-run blocks stay never-run. These were :skipif: True. Each now sits in a docstring that carries the # doctest: +SKIP_EXAMPLE flag ([core] Add a docstring-level +SKIP_EXAMPLE doctest flag for render-only examples #66753). They're in models/catalog.py, models/modelv2.py (2), models/preprocessors.py (2, in one docstring), offline/input_reader.py, and offline/mixed_input.py.
  • 7 blocks that don't parse as Python become .. code-block:: python, and their outputs become .. code-block:: text. The five RepeatedValues blocks use <Tensor shape=...> placeholders. The two train_ops.py examples have an extra closing parenthesis.

This is a syntax-only change. The example code and the claims in each docstring are unchanged. No format-forced changes were needed, and pydoclint passes without new Args or Returns entries.

Left for a separate correctness PR

In execution/buffers/mixin_replay_buffer.py, the MixInMultiAgentReplayBuffer example's testoutput is :hide: with a body of only ..., which matches any output. Converting it would change what the example checks.

Related issues

Related to #66749, #66753, and #67753.

Additional information

Not a duplicate. I searched open PRs for RLlib testcode and doctest conversions, and none touches these docstrings.

Tests run

  • ci/ray_ci/doc/run_docstring_examples.py on all 9 files, in a Python 3.12 virtualenv with ray[rllib]==2.59.0, torch, and tensorflow 2.21: "3 docstring(s) run, 0 failed, 6 skipped, in 9 file(s)." All six skips are +SKIP_EXAMPLE.
  • 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 3 runnable docstrings pass.
  • ci/ray_ci/doc/check_docstring_only.py --base upstream/master: "9 file(s), docstrings only."
  • pre-commit run --files on all 9 files: all hooks pass, including pydoclint.

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

…ib.callbacks docstring examples to stock doctests

Convert the `.. testcode::` examples in these docstrings to `>>>` examples under an `Examples:` header, so they render in griffe and MkDocs as well as Sphinx. Runnable blocks become runnable doctests, `:skipif: True` blocks carry `+SKIP_EXAMPLE`, and blocks that don't parse become `.. code-block::`. The example code and the docstring claims are unchanged.

@rtd-build

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman dstrodtman added rllib RLlib related issues docstring-only Skip heavy library tests for Ray API docstring edits. labels Nov 3, 2026
@dstrodtman dstrodtman added the go add ONLY when ready to merge, run all tests label Nov 3, 2026
@dstrodtman
dstrodtman marked this pull request as ready for review November 3, 2026 20:57
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 20:57

@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 refactors documentation across multiple RLlib files, converting obsolete .. testcode:: and .. testoutput:: blocks into standard Examples: sections with doctest formatting or .. code-block:: directives. The review feedback identifies a few issues in the updated examples: a NameError in on_offline_eval_runners_recreated due to referencing an undefined is_evaluation variable, and syntax errors (extra closing parentheses) in the example print statements for train_one_step and multi_gpu_train_one_step.

Comment on lines +269 to +272
... if is_evaluation:
... offline_eval_runner._custom_property_for_evaluation = 1
... else:
... offline_eval_runner._custom_property_for_training = 1

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.

high

The method on_offline_eval_runners_recreated does not have an is_evaluation parameter in its signature (unlike on_env_runners_recreated). Referencing is_evaluation here will raise a NameError at runtime. Since these are offline evaluation runners, they are always used for evaluation, so you can set the property directly without checking is_evaluation.

Suggested change
... if is_evaluation:
... offline_eval_runner._custom_property_for_evaluation = 1
... else:
... offline_eval_runner._custom_property_for_training = 1
... offline_eval_runner._custom_property_for_evaluation = 1

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, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.

algo = [...]
train_batch = synchronous_parallel_sample(algo.env_runner_group)
# This trains the policy on one batch.
print(train_one_step(algo, train_batch)))

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 an extra closing parenthesis at the end of the print statement, which is a syntax error.

Suggested change
print(train_one_step(algo, train_batch)))
print(train_one_step(algo, train_batch))

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, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.

algo = [...]
train_batch = synchronous_parallel_sample(algo.env_runner_group)
# This trains the policy on one batch.
print(multi_gpu_train_one_step(algo, train_batch)))

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 an extra closing parenthesis at the end of the print statement, which is a syntax error.

Suggested change
print(multi_gpu_train_one_step(algo, train_batch)))
print(multi_gpu_train_one_step(algo, train_batch))

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, that's right. This PR is syntax-only on purpose, so it leaves example code unchanged. #67762, stacked on this PR, fixes this.

@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 364db4a into master Nov 4, 2026
7 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-models-execution-doctests branch November 4, 2026 15:18
pseudo-rnd-thoughts pushed a commit that referenced this pull request Nov 4, 2026
…cstring examples (#67762)

## Description

This is a correctness change for RLlib review. It fixes docstring
examples in `rllib/models`, `execution`, `offline`, and `callbacks` that
were wrong or couldn't run, and it converts the last `testcode` block in
those subpackages. Where a fix is verified, the example now runs in the
doctest target. After this PR, no `testcode` or `testoutput` remains in
these four subpackages.

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

### Examples that now run

- `models/catalog.py`, `ModelCatalog`: the example used undefined
placeholders, such as `env` and `raw_observation`. It also read
`model.outputs`, which `ModelV2` doesn't have. It's now a working
CartPole example with `framework="torch"`. It gets logits by calling the
model and passes them to the distribution class.
- `models/preprocessors.py`, `OneHotPreprocessor`: the example called
`self.transform` on a random sample, and it wrote the output as
`np.array(...)`. It now builds the preprocessor and transforms fixed
values. The output is the real `array(..., dtype=float32)`, with the
same one-hot values as before.
- `models/repeated_values.py`, `unbatch_all` and `unbatch_repeat_dim`:
these were `<Tensor shape=...>` pseudocode, and the second one called
`batch.unbatch()`, which doesn't exist. Both now use a small torch
tensor and print lengths and shapes. Both docstrings gain a `Returns`
entry, which pydoclint requires once a docstring has an `Examples:`
section. Each entry describes what the method body returns.
- `execution/buffers/mixin_replay_buffer.py`,
`MixInMultiAgentReplayBuffer`: the hidden `...` output matched anything.
The full output varies, because `replay()` appends randomly replayed
samples after the newly added batches. The example now prints the first
three entries, `[1 2 2]`, which are always the newly added batches.
- `offline/input_reader.py`, `InputReader.tf_input_ops`: the example
called `self._build_layers_v2`, which doesn't exist in RLlib. It also
read `input_ops["action"]`, but the column is `"actions"`. It now calls
`self.forward(...)`, as
`examples/_old_api_stack/models/custom_loss_model.py` does. The prose
pointed to `examples/custom_loss.py`, which doesn't exist, and now
points to that file.
- `models/modelv2.py`, `get_initial_state` and `forward`: these
class-definition sketches pass unchanged, so they drop `+SKIP_EXAMPLE`.
- `callbacks/callbacks.py`, `on_offline_eval_runners_recreated`: the
example read `is_evaluation`, which isn't a parameter of this hook. It
also passed `remote_runner_ids=` to `foreach_runner`, which takes
`remote_worker_ids`. Both are fixed.
- `callbacks/callbacks.py`, `on_env_runners_recreated`: a comment named
`local_worker=False`, but the code passes `local_env_runner=False`.

### Examples that stay render-only

- `execution/train_ops.py`, `train_one_step` and
`multi_gpu_train_one_step`: these need a built old-API-stack
`Algorithm`. This PR removes the stray parenthesis and passes
`worker_set=` to `synchronous_parallel_sample`, whose arguments are
keyword-only. It also prints the output as a Python dict. They stay code
blocks, because an `Examples:` section makes pydoclint require type
hints in the function signatures, and that's a code change.
- `offline/mixed_input.py`, `MixedInput`: this needs an `IOContext`,
local JSON files, and an S3 path. It keeps `+SKIP_EXAMPLE`.

### Found, not fixed here

This is a code issue, not a docstring one, so it's out of scope.
`Algorithm._run_one_offline_evaluation` fires the
`on_offline_eval_runners_recreated` callback with `env_runner_group=`
and `env_runner_indices=`. The hook's signature in
`callbacks/callbacks.py` declares `offline_eval_runner_group` and
`offline_eval_runner_indices` as keyword-only arguments with no
defaults. So an override that follows the documented signature would
raise `TypeError` when the callback fires.

### Review fixes

- `MixInMultiAgentReplayBuffer`: the example defined a batch `C` that it
never used, so it's gone. From a gemini-code-assist comment.

## Related issues

Stacked on #67761. Related to #66753.

## Additional information

**Not a duplicate.** I searched open PRs for fixes to these docstring
examples and found none.

**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 the 8 changed files: "10
docstring(s) run, 0 failed, 0 skipped, in 8 file(s)."
- A strict stdlib `doctest` pass over the same docstrings with
`ELLIPSIS`, which fails on unexpected output the way the RLlib
`doctest()` target does: all 10 pass.
- `ci/ray_ci/doc/check_docstring_only.py --base
doc-1926-rllib-models-execution-doctests`: "8 file(s), docstrings only."
- `pre-commit run --files` on all 8 files: all hooks pass, including
pydoclint.

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](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