Skip to content

[RLlib] Fix broken rllib.evaluation and rllib.policy docstring examples and make them run - #67768

Merged
pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-evaluation-policy-example-fixes
Nov 4, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
masterfrom
doc-1926-rllib-evaluation-policy-example-fixes

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Description

This is a correctness change for RLlib review. It fixes rllib.evaluation and rllib.policy docstring examples that were wrong or couldn't run. It also turns the two code-block fallbacks from #67767 back into doctests. After this PR, 17 of these docstrings run in the doctest target, and no testcode or testoutput remains in either subpackage.

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

  • get_learner_stats: the example called get_stats, which doesn't exist, on the result of worker.learn_on_batch(samples), where worker and samples were undefined. It now passes literal grad_info dicts in the shape the old comment showed, covering both the single-agent and the multi-agent case.
  • discount_cumsum: the output was a symbolic formula. It's now the real array([4.707, 5.23 , 4.7 , 3. ]), and the formula is a comment.
  • RolloutWorker.sample, sample_with_count, learn_on_batch, compute_gradients, and apply_gradients:
    • PPOTF1Policy fails to build with the default config, so each example now passes a PPOConfig with the old API stack enabled and framework("tf").
    • sample_with_count used PPOTFPolicy instead of PPOTF1Policy.
    • sample used AlgorithmConfig without importing it.
    • learn_on_batch and compute_gradients passed an undefined samples instead of batch.
    • sample() returns a MultiAgentBatch, not a SampleBatch, so the outputs now show that.
  • chop_into_sequences: the outputs now show the list of arrays and the array that the function returns. The values are the same as before.
  • ViewRequirement: ModelV2(...) raised TypeError. The example now builds a ModelV2 with real spaces. It shows that the model's only default view requirement is obs, with shift=0.
  • SampleBatch.concat, rows, columns, shuffle, split_by_episode, and right_zero_pad:
    • The outputs were dict literals, but print(batch) shows a summary such as SampleBatch(5: ['a']). The examples now print columns, and the outputs are real.
    • split_by_episode set a "dones" column, which SampleBatch rejects with KeyError. It now sets "terminateds", which the splitter reads.
    • shuffle now seeds NumPy, so its output is fixed.
  • concat_samples and concat_samples_into_ma_batch:
    • Both passed plain dicts to MultiAgentBatch, which asserts on SampleBatch values.
    • The second concat_samples call concatenated b1 and b2 instead of c1 and c2.
    • The concat_samples_into_ma_batch example called concat_samples.
    • The outputs now show the real MultiAgentBatch repr and the concatenated columns.

The five RolloutWorker examples build old-API-stack TF1 PPO policies. They take about 9 seconds together on CPU. Their outputs show 2,000 sampled steps, which comes from PPO's default config. I ran them locally, but not under Bazel. If RLlib would rather not run old-API-stack TF code in the doctest target, re-adding # doctest: +SKIP_EXAMPLE keeps the corrected examples render-only.

Fixed but still render-only

  • RolloutWorker class docstring: the single-agent half gets the same config fix and runs. The multi-agent half uses placeholders and PGTFPolicy, which RLlib no longer has, so the docstring stays skipped.
  • RolloutWorker.set_global_vars: the stray ... is gone, and so is the assignment of the method's None return value. It's a doctest again.
  • SampleCollector.add_init_obs, add_action_reward_next_obs, and get_inference_input_dict:
    • The add_action_reward_next_obs calls now pass env_id.
    • add_init_obs is called with keywords, since its arguments are keyword-only.
    • get_inference_input_dict gets a policy ID, not policy.model, and its dict syntax error is fixed.
    • The examples still use placeholder env and collector objects.

ObservationFunction.__call__, Policy.learn_on_batch, Policy.export_checkpoint, TFPolicy, and RolloutWorker.get_weights, set_weights, and get_global_vars use placeholder objects. They're unchanged and stay render-only.

Found, not fixed here

These need RLlib's call, so this PR leaves them alone:

  • The abstract SampleCollector.add_init_obs has no env_id parameter, but SimpleListCollector.add_init_obs, its only implementation, requires one. The example follows the abstract signature.
  • The render-only get_global_vars output, {"timestep": 424242}, leaves out num_grad_updates_per_policy, which the real dict contains.

Related issues

Stacked on #67767. 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 7 changed files: "17 docstring(s) run, 0 failed, 8 skipped, in 7 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 17 runnable docstrings pass, and the rest carry +SKIP_EXAMPLE.
  • ci/ray_ci/doc/check_docstring_only.py --base doc-1926-rllib-evaluation-policy-doctests: "7 file(s), docstrings only."
  • pre-commit run --files on all 7 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 21:00
@dstrodtman
dstrodtman requested a review from a team as a code owner November 3, 2026 21:00
@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 systematically updates and corrects docstring examples across several RLlib modules, including sample collectors, metrics, postprocessing, rollout workers, RNN sequencing, sample batches, and view requirements. The changes ensure that these examples are valid, executable doctests by importing necessary dependencies, using up-to-date configurations (such as PPOConfig), correcting API signatures, and aligning the expected outputs with actual runtime representations. I have no feedback to provide as there are no review comments to assess.

Base automatically changed from doc-1926-rllib-evaluation-policy-doctests to master November 4, 2026 15:20
…es and make them run

Fix rllib/evaluation and rllib/policy docstring examples that were wrong or
couldn't run, and drop `+SKIP_EXAMPLE` from the ones that now run, so the
RLlib doctest target checks them. The fixes cover undefined names, removed
`dones` writes, plain dicts where `MultiAgentBatch` needs `SampleBatch`
values, wrong function names, a missing `PPOConfig` for `RolloutWorker`,
call signatures, and pseudo-reprs that didn't match real output. Examples
that need placeholder objects stay render-only.

@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-evaluation-policy-example-fixes branch from 14a40e2 to 0065b54 Compare November 4, 2026 16:22
@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit a1020a3 into master Nov 4, 2026
6 checks passed
@pseudo-rnd-thoughts
pseudo-rnd-thoughts deleted the doc-1926-rllib-evaluation-policy-example-fixes branch November 4, 2026 16:43
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