Skip to content

[doc][tune][DOC-1131] Update deprecated spec.config sample_from examples to config-dict style - #63804

Merged
matthewdeng merged 1 commit into
ray-project:masterfrom
dstrodtman:doc-1131-spec-config-examples
Jun 2, 2026
Merged

matthewdeng merged 1 commit into
ray-project:masterfrom
dstrodtman:doc-1131-spec-config-examples

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Why are these changes needed?

tune.sample_from callables receive the trial config dict directly; the legacy spec.config access pattern has been deprecated since the placeholder-resolution mechanism landed in Feb 2023 (befad81). Every Tune doc example still written as lambda spec: spec.config.x logs this warning on each trial when users copy it:

sample_from functions that take a spec dict are deprecated. Please update your function to work with the config dict directly.

Follow-up to #63443, which fixed the tune.sample_from docstring itself.

What changed

Updated to config-dict style (lambda config: config["x"]):

  • doc/source/tune/tutorials/tune-search-spaces.rst — examples plus the prose describing the spec object and spec.config namespace
  • doc/source/tune/api/search_space.rst
  • doc/source/tune/doc_code/faq.py — conditional-spaces and grid-search examples
  • doc/source/tune/doc_code/key_concepts.py
  • doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb — 3 cells
  • python/ray/tune/examples/pbt_tune_cifar10_with_keras.py
  • python/ray/tune/tests/test_trial_scheduler_pbt.py — runs through Tuner, so it exercised the deprecated fallback on every run

One of these is a bug fix rather than deprecation cleanup: the tune-resources.rst with_resources example was broken as written. Resource callables receive the bare config dict via default_resource_request() with no spec fallback, so resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0} raises AttributeError: 'dict' object has no attribute 'config'.

Deliberately left unchanged

  • test_sample.py and test_var.py sample_from sites — these drive generate_variants / BasicVariantGenerator directly, the legacy full-spec path where spec.config is still the operative contract (config-dict style would break them).
  • test_placeholder.py::testFunction — deliberate coverage that both styles work.
  • test_controller_resume_integration.py — restore-path coverage for legacy-style functions.
  • variant_generator.py docstring — documents the DeveloperAPI legacy contract and the {"eval": ...} syntax.

Verification

Ran the updated patterns end-to-end on ray 2.55.1:

  • Config-dict conditional space (b sampled from a range depending on a): 4 trials, conditional held, zero deprecation warnings. Control with the old spec style logs one warning per trial.
  • Fixed with_resources callable resolves; control with the old spec style reproduces the AttributeError.

ruff==0.8.4 and black==22.10.0 (the pre-commit pins) pass on the edited Python files.

Related issue number

Internal tracking: DOC-1131.

Checks

  • I've signed off every commit (by using the -s flag, i.e., git commit -s) in this PR.
  • I've run pre-commit equivalent lint (ruff + black at pinned versions) on the changed files.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
  • I've added any new APIs to the API Reference (n/a — no API changes).
  • I've made sure the tests are passing (changed examples validated against ray 2.55.1; test_trial_scheduler_pbt.py change is parameter-name-only under the Tuner path).

🤖 Generated with Claude Code

…les to config-dict style

sample_from callables have received the trial config dict directly
since the placeholder resolution mechanism landed (befad81, Feb 2023);
the spec.config access pattern hits the deprecation fallback in
Function._CallSampler.__try_fn and logs a warning on every trial.
Update user-facing examples to the config-dict style.

The tune-resources.rst with_resources example was broken outright:
resource callables receive the bare config dict via
default_resource_request() with no spec fallback, so
`lambda spec: ... spec.config.use_gpu` raises AttributeError.

Deliberately left unchanged:
- test_sample.py / test_var.py sites that drive generate_variants /
  BasicVariantGenerator directly (legacy full-spec path, where
  spec.config is still the operative contract)
- test_placeholder.py testFunction (deliberate both-styles coverage)
- test_controller_resume_integration.py (legacy-style restore coverage)
- variant_generator.py docstring (documents the DeveloperAPI legacy
  contract and the {"eval": ...} syntax)

Verified on ray 2.55.1: the config-dict conditional space runs clean
with no deprecation warning (old style logs one per trial), and the
fixed with_resources lambda resolves while the old one reproduces the
AttributeError.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman requested a review from a team as a code owner June 2, 2026 19:32

@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 Ray Tune documentation, examples, and tests to reflect an API change where lambda functions in tune.sample_from and tune.with_resources receive a config dictionary directly instead of a spec object. Feedback is provided for tune-resources.rst suggesting the use of config.get("use_gpu", False) instead of direct dictionary access to prevent potential KeyErrors when the key is missing.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

# If you want to allocate gpu resources to trials based on a setting in your config
trainable_with_resources = tune.with_resources(trainable,
resources=lambda spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0})
resources=lambda config: {"gpu": 1} if config["use_gpu"] else {"gpu": 0})

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

Using direct dictionary access like config["use_gpu"] can raise a KeyError if the use_gpu key is not explicitly provided in the configuration. It is safer and more robust to use config.get("use_gpu", False) to provide a default fallback value.

Suggested change
resources=lambda config: {"gpu": 1} if config["use_gpu"] else {"gpu": 0})
resources=lambda config: {"gpu": 1} if config.get("use_gpu", False) else {"gpu": 0})

@ray-gardener ray-gardener Bot added tune Tune-related issues docs An issue or change related to documentation labels Jun 2, 2026
@matthewdeng
matthewdeng enabled auto-merge (squash) June 2, 2026 20:15
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Jun 2, 2026
@matthewdeng
matthewdeng merged commit 5631977 into ray-project:master Jun 2, 2026
5 checks passed
rueian pushed a commit to rueian/ray that referenced this pull request Jun 4, 2026
…les to config-dict style (ray-project#63804)

## Why are these changes needed?

`tune.sample_from` callables receive the trial `config` dict directly;
the legacy `spec.config` access pattern has been deprecated since the
placeholder-resolution mechanism landed in Feb 2023 (befad81). Every
Tune doc example still written as `lambda spec: spec.config.x` logs this
warning on each trial when users copy it:

> sample_from functions that take a spec dict are deprecated. Please
update your function to work with the config dict directly.

Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring
itself.

### What changed

Updated to config-dict style (`lambda config: config["x"]`):

- `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the
prose describing the `spec` object and `spec.config` namespace
- `doc/source/tune/api/search_space.rst`
- `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search
examples
- `doc/source/tune/doc_code/key_concepts.py`
- `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` —
3 cells
- `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py`
- `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through
`Tuner`, so it exercised the deprecated fallback on every run

One of these is a bug fix rather than deprecation cleanup: the
`tune-resources.rst` `with_resources` example was broken as written.
Resource callables receive the bare config dict via
`default_resource_request()` with no spec fallback, so `resources=lambda
spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises
`AttributeError: 'dict' object has no attribute 'config'`.

### Deliberately left unchanged

- `test_sample.py` and `test_var.py` `sample_from` sites — these drive
`generate_variants` / `BasicVariantGenerator` directly, the legacy
full-spec path where `spec.config` is still the operative contract
(config-dict style would break them).
- `test_placeholder.py::testFunction` — deliberate coverage that both
styles work.
- `test_controller_resume_integration.py` — restore-path coverage for
legacy-style functions.
- `variant_generator.py` docstring — documents the DeveloperAPI legacy
contract and the `{"eval": ...}` syntax.

### Verification

Ran the updated patterns end-to-end on ray 2.55.1:

- Config-dict conditional space (`b` sampled from a range depending on
`a`): 4 trials, conditional held, zero deprecation warnings. Control
with the old spec style logs one warning per trial.
- Fixed `with_resources` callable resolves; control with the old spec
style reproduces the `AttributeError`.

`ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the
edited Python files.

## Related issue number

Internal tracking: DOC-1131.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit equivalent lint (ruff + black at pinned
versions) on the changed files.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference (n/a — no API
changes).
- [x] I've made sure the tests are passing (changed examples validated
against ray 2.55.1; `test_trial_scheduler_pbt.py` change is
parameter-name-only under the `Tuner` path).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
limarkdcunha pushed a commit to limarkdcunha/ray that referenced this pull request Jun 30, 2026
…les to config-dict style (ray-project#63804)

## Why are these changes needed?

`tune.sample_from` callables receive the trial `config` dict directly;
the legacy `spec.config` access pattern has been deprecated since the
placeholder-resolution mechanism landed in Feb 2023 (befad81). Every
Tune doc example still written as `lambda spec: spec.config.x` logs this
warning on each trial when users copy it:

> sample_from functions that take a spec dict are deprecated. Please
update your function to work with the config dict directly.

Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring
itself.

### What changed

Updated to config-dict style (`lambda config: config["x"]`):

- `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the
prose describing the `spec` object and `spec.config` namespace
- `doc/source/tune/api/search_space.rst`
- `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search
examples
- `doc/source/tune/doc_code/key_concepts.py`
- `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` —
3 cells
- `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py`
- `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through
`Tuner`, so it exercised the deprecated fallback on every run

One of these is a bug fix rather than deprecation cleanup: the
`tune-resources.rst` `with_resources` example was broken as written.
Resource callables receive the bare config dict via
`default_resource_request()` with no spec fallback, so `resources=lambda
spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises
`AttributeError: 'dict' object has no attribute 'config'`.

### Deliberately left unchanged

- `test_sample.py` and `test_var.py` `sample_from` sites — these drive
`generate_variants` / `BasicVariantGenerator` directly, the legacy
full-spec path where `spec.config` is still the operative contract
(config-dict style would break them).
- `test_placeholder.py::testFunction` — deliberate coverage that both
styles work.
- `test_controller_resume_integration.py` — restore-path coverage for
legacy-style functions.
- `variant_generator.py` docstring — documents the DeveloperAPI legacy
contract and the `{"eval": ...}` syntax.

### Verification

Ran the updated patterns end-to-end on ray 2.55.1:

- Config-dict conditional space (`b` sampled from a range depending on
`a`): 4 trials, conditional held, zero deprecation warnings. Control
with the old spec style logs one warning per trial.
- Fixed `with_resources` callable resolves; control with the old spec
style reproduces the `AttributeError`.

`ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the
edited Python files.

## Related issue number

Internal tracking: DOC-1131.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit equivalent lint (ruff + black at pinned
versions) on the changed files.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference (n/a — no API
changes).
- [x] I've made sure the tests are passing (changed examples validated
against ray 2.55.1; `test_trial_scheduler_pbt.py` change is
parameter-name-only under the `Tuner` path).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…les to config-dict style (ray-project#63804)

## Why are these changes needed?

`tune.sample_from` callables receive the trial `config` dict directly;
the legacy `spec.config` access pattern has been deprecated since the
placeholder-resolution mechanism landed in Feb 2023 (befad81). Every
Tune doc example still written as `lambda spec: spec.config.x` logs this
warning on each trial when users copy it:

> sample_from functions that take a spec dict are deprecated. Please
update your function to work with the config dict directly.

Follow-up to ray-project#63443, which fixed the `tune.sample_from` docstring
itself.

### What changed

Updated to config-dict style (`lambda config: config["x"]`):

- `doc/source/tune/tutorials/tune-search-spaces.rst` — examples plus the
prose describing the `spec` object and `spec.config` namespace
- `doc/source/tune/api/search_space.rst`
- `doc/source/tune/doc_code/faq.py` — conditional-spaces and grid-search
examples
- `doc/source/tune/doc_code/key_concepts.py`
- `doc/source/tune/examples/pbt_visualization/pbt_visualization.ipynb` —
3 cells
- `python/ray/tune/examples/pbt_tune_cifar10_with_keras.py`
- `python/ray/tune/tests/test_trial_scheduler_pbt.py` — runs through
`Tuner`, so it exercised the deprecated fallback on every run

One of these is a bug fix rather than deprecation cleanup: the
`tune-resources.rst` `with_resources` example was broken as written.
Resource callables receive the bare config dict via
`default_resource_request()` with no spec fallback, so `resources=lambda
spec: {"gpu": 1} if spec.config.use_gpu else {"gpu": 0}` raises
`AttributeError: 'dict' object has no attribute 'config'`.

### Deliberately left unchanged

- `test_sample.py` and `test_var.py` `sample_from` sites — these drive
`generate_variants` / `BasicVariantGenerator` directly, the legacy
full-spec path where `spec.config` is still the operative contract
(config-dict style would break them).
- `test_placeholder.py::testFunction` — deliberate coverage that both
styles work.
- `test_controller_resume_integration.py` — restore-path coverage for
legacy-style functions.
- `variant_generator.py` docstring — documents the DeveloperAPI legacy
contract and the `{"eval": ...}` syntax.

### Verification

Ran the updated patterns end-to-end on ray 2.55.1:

- Config-dict conditional space (`b` sampled from a range depending on
`a`): 4 trials, conditional held, zero deprecation warnings. Control
with the old spec style logs one warning per trial.
- Fixed `with_resources` callable resolves; control with the old spec
style reproduces the `AttributeError`.

`ruff==0.8.4` and `black==22.10.0` (the pre-commit pins) pass on the
edited Python files.

## Related issue number

Internal tracking: DOC-1131.

## Checks

- [x] I've signed off every commit (by using the -s flag, i.e., `git
commit -s`) in this PR.
- [x] I've run pre-commit equivalent lint (ruff + black at pinned
versions) on the changed files.
- [x] I've included any doc changes needed for
https://docs.ray.io/en/master/.
- [ ] I've added any new APIs to the API Reference (n/a — no API
changes).
- [x] I've made sure the tests are passing (changed examples validated
against ray 2.55.1; `test_trial_scheduler_pbt.py` change is
parameter-name-only under the `Tuner` path).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs An issue or change related to documentation go add ONLY when ready to merge, run all tests tune Tune-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants