Skip to content

[RLlib] Fix broken rllib.models, execution, offline, and callbacks docstring examples - #67762

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

pseudo-rnd-thoughts merged 2 commits into
masterfrom
doc-1926-rllib-models-execution-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 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

@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:59
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 20:59
@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 updates, corrects, and standardizes docstring examples across several RLlib modules to ensure they are fully runnable doctests. It also resolves minor syntax errors, updates outdated API references, and aligns parameter names in the examples. The review feedback points out an opportunity to clean up the MixInMultiAgentReplayBuffer example by removing an unused variable C from a tuple assignment.

Comment on lines +35 to +36
>>> A, B, C = (SampleBatch({"obs": [1]}), SampleBatch({"obs": [2]}),
... SampleBatch({"obs": [3]}))

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

The variable 'C' is defined in the tuple assignment but is never used or added to the buffer in the example. To keep the example clean and avoid confusing the reader, it is better to remove it.

Suggested change
>>> A, B, C = (SampleBatch({"obs": [1]}), SampleBatch({"obs": [2]}),
... SampleBatch({"obs": [3]}))
>>> A, B = SampleBatch({"obs": [1]}), SampleBatch({"obs": [2]})

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 08b2986.

Base automatically changed from doc-1926-rllib-models-execution-doctests to master November 4, 2026 15:18
dstrodtman and others added 2 commits November 4, 2026 15:18
…rllib.callbacks docstring examples and make them run

Fix the examples that the stock-doctest conversion left as `testcode` or `code-block`, and the never-run examples that fail when run. Pseudocode in `ModelCatalog`, `OneHotPreprocessor`, and `RepeatedValues` becomes working code with real output, the mix-in replay buffer example checks deterministic output instead of a hidden `...`, and wrong API names in the `train_ops`, `InputReader`, and callback examples now match the source.

@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 force-pushed the doc-1926-rllib-models-execution-example-fixes branch from 08b2986 to 004765e Compare November 4, 2026 15:18
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit c7297fb into master Nov 4, 2026
6 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-models-execution-example-fixes branch November 4, 2026 16:44
pseudo-rnd-thoughts pushed a commit that referenced this pull request Nov 6, 2026
…_recreated (#67770)

## Description

`Algorithm._run_one_offline_evaluation` fires the
`on_offline_eval_runners_recreated` callback after it restores offline
evaluation runners. It passes `env_runner_group=` and
`env_runner_indices=`. But
`RLlibCallback.on_offline_eval_runners_recreated` declares
`offline_eval_runner_group` and `offline_eval_runner_indices` as
required keyword-only arguments. The `AlgorithmConfig.callbacks()` docs
point callable callbacks to that signature too. So any callback that
follows the documented signature raises `TypeError` when offline
evaluation runners are restored:

```text
TypeError: OfflineEvalRunnersRecreatedCallback.on_offline_eval_runners_recreated() missing 2 required keyword-only arguments: 'offline_eval_runner_group' and 'offline_eval_runner_indices'
```

This PR passes the documented argument names. This call site is the only
place RLlib fires the callback, and the only other implementation, the
`_MultiCallbacks` container in `callbacks/utils.py`, takes `**kwargs`.
Ray 2.59.0 has the same bug.

This PR also adds `test_on_offline_eval_runners_recreated_callback` to
`callbacks/tests/test_callbacks_on_algorithm.py`. The test calls
`_run_one_offline_evaluation` on a stand-in algorithm whose offline
evaluation runners report two restored runners. It checks that both an
`RLlibCallback` subclass and a callable, each written against the
documented signature, receive the runner group and indices. It doesn't
need offline data or crashing runners, so it runs in seconds.

I found this while fixing the `on_offline_eval_runners_recreated`
docstring example in #67762.

## Related issues

Related to #67762.

## Additional information

**Not a duplicate.** I searched open PRs for
`on_offline_eval_runners_recreated` and found only #67762, which changes
that hook's docstring example, not this call site.

**Tests run**

These ran in a Python 3.12 virtualenv with `ray[rllib]==2.59.0` and
`torch`, with `ray/rllib` linked to this branch's source:

- The new test, before the fix: it fails with the `TypeError` above.
- `pytest
python/ray/rllib/callbacks/tests/test_callbacks_on_algorithm.py`, after
the fix: "3 passed in 68.47s." That includes the existing
`test_on_env_runners_recreated_callback` and
`test_on_init_and_checkpoint_loaded`.
- `pre-commit run --files` on both changed files: all hooks pass.

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