Skip to content

[Docs] Add conditional search-space example to tune.sample_from docstring (#33483) - #63443

Merged
elliot-barn merged 3 commits into
ray-project:masterfrom
dstrodtman:doc-984-sample-from-example
Jul 2, 2026
Merged

elliot-barn merged 3 commits into
ray-project:masterfrom
dstrodtman:doc-984-sample-from-example

Conversation

@dstrodtman

Copy link
Copy Markdown
Contributor

Description

The docstring for tune.sample_from did not show how to define a conditional search space, which is its canonical use case. Add a short Example block under python/ray/tune/search/sample.py that samples one parameter from a range derived from another via spec.config.

Related issues

Closes #33483

[DOC-984]

Additional information

Target file: python/ray/tune/search/sample.py. The example mirrors the existing conditional-search-space pattern from doc/source/tune/doc_code/faq.py (__conditional_spaces_start__ block), so users land on a consistent canonical form whether they read the API reference or the FAQ.

Generated with Claude Code

…ring (ray-project#33483)

The docstring for tune.sample_from did not show how to define a conditional
search space, which is its canonical use case. Add a short ``Example`` block
under python/ray/tune/search/sample.py that samples one parameter from a
range derived from another via ``spec.config``.

Closes ray-project#33483

[DOC-984]

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
@dstrodtman
dstrodtman requested a review from a team as a code owner May 18, 2026 16:00

@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 the sample_from function's docstring to include an explanation and example of conditional search spaces. The review feedback points out that the documentation uses a deprecated spec.config pattern and suggests updating it to use the config dictionary directly to prevent deprecation warnings.

Comment thread python/ray/tune/search/sample.py Resolved
Comment thread python/ray/tune/search/sample.py Resolved
@ray-gardener ray-gardener Bot added tune Tune-related issues docs An issue or change related to documentation labels May 18, 2026
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Jun 2, 2026
@dstrodtman dstrodtman removed the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Jun 2, 2026
dstrodtman and others added 2 commits June 2, 2026 14:42
…om-example

Signed-off-by: Douglas Strodtman <[email protected]>

# Conflicts:
#	python/ray/tune/search/sample.py
sample_from callables receive the trial config dict directly; the
spec.config access pattern hits the deprecation fallback in
Function._CallSampler.__try_fn and logs a warning at runtime. Also
rename the example dict to param_space to avoid shadowing the lambda
parameter, and fix a pre-existing typo (An callable -> A callable).

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Douglas Strodtman <[email protected]>
matthewdeng pushed a commit that referenced this pull request Jun 2, 2026
…les to config-dict style (#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 #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]>
@pseudo-rnd-thoughts pseudo-rnd-thoughts added the go add ONLY when ready to merge, run all tests label Jun 3, 2026
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]>
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Jun 17, 2026
@dstrodtman dstrodtman added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Jun 24, 2026
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]>
@elliot-barn
elliot-barn merged commit a1909f4 into ray-project:master Jul 2, 2026
5 checks passed
elliot-barn pushed a commit that referenced this pull request Jul 9, 2026
…ring (#33483) (#63443)

## Description

The docstring for `tune.sample_from` did not show how to define a
conditional search space, which is its canonical use case. Add a short
`Example` block under `python/ray/tune/search/sample.py` that samples
one parameter from a range derived from another via `spec.config`.

## Related issues

Closes #33483

[DOC-984]

## Additional information

Target file: `python/ray/tune/search/sample.py`. The example mirrors the
existing conditional-search-space pattern from
`doc/source/tune/doc_code/faq.py` (`__conditional_spaces_start__`
block), so users land on a consistent canonical form whether they read
the API reference or the FAQ.

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

---------

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Signed-off-by: elliot-barn <[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]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ring (ray-project#33483) (ray-project#63443)

## Description

The docstring for `tune.sample_from` did not show how to define a
conditional search space, which is its canonical use case. Add a short
`Example` block under `python/ray/tune/search/sample.py` that samples
one parameter from a range derived from another via `spec.config`.

## Related issues

Closes ray-project#33483

[DOC-984]

## Additional information

Target file: `python/ray/tune/search/sample.py`. The example mirrors the
existing conditional-search-space pattern from
`doc/source/tune/doc_code/faq.py` (`__conditional_spaces_start__`
block), so users land on a consistent canonical form whether they read
the API reference or the FAQ.

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

---------

Signed-off-by: Douglas Strodtman <[email protected]>
Co-authored-by: Claude Opus 4.7 (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 unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Doc][Tune] Add a docstring example for tune.sample_from

3 participants