Skip to content

Fix GridSampler crashing on enqueued trials with one grid remaining - #6844

Merged
kAIto47802 merged 2 commits into
optuna:masterfrom
EmanueleCornaggia:codex/fix-grid-enqueued-trial
Oct 21, 2026
Merged

kAIto47802 merged 2 commits into
optuna:masterfrom
EmanueleCornaggia:codex/fix-grid-enqueued-trial

Conversation

@EmanueleCornaggia

Copy link
Copy Markdown
Contributor

Motivation

GridSampler raises KeyError("grid_id") after an enqueued trial when exactly one grid point remains. Enqueued trials intentionally have no grid ID, but after_trial assumes the ID exists when deciding whether to stop optimization.

This interrupts optimization before the remaining grid point is evaluated:

import optuna


study = optuna.create_study(sampler=optuna.samplers.GridSampler({"a": [0]}))
study.enqueue_trial({"a": 100})
study.optimize(lambda trial: trial.suggest_int("a", 0, 100))

The expected evaluations are a=100 followed by a=0, after which optimization stops. The same error occurs when an enqueued trial follows a normal trial in a two-point grid.

Description of the changes

  • Use an optional lookup for the trial's grid ID when checking the last remaining grid.
  • Extend the existing enqueued-trial test to cover a single-point grid and a partly completed two-point grid, while retaining the original case. Verify the queued value, complete grid coverage, and termination.

Both added cases failed before the fix; all three cases pass afterward.

Checklist

  • I used an LLM while working on this PR.

@nabenabe0928

Copy link
Copy Markdown
Contributor

This is by design.

@EmanueleCornaggia

Copy link
Copy Markdown
Contributor Author

Thanks for linking #6073! I understand that enqueued trials intentionally have no grid_id and do not count toward grid coverage, so duplicate evaluations are expected.

This PR aims to preserve that behavior while fixing a KeyError("grid_id") when an enqueued trial finishes with exactly one grid point remaining. In that case, after_trial accesses the missing attribute and interrupts optimization, even though the objective returned successfully.

The reproduction in this PR uses a single-point grid and enqueues the trial before optimization starts. The proposed change allows the enqueued trial to finish, then evaluates the remaining grid point normally.

Could you clarify whether the exception itself is also intended, or whether this case can be considered separately from the duplication behavior in #6073?

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Sep 13, 2026
@not522

not522 commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

This seems like behavior that needs to be fixed. Once this PR is ready for review, we will perform the review.

@github-actions github-actions Bot removed the stale Exempt from stale bot labeling. label Sep 24, 2026
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Oct 1, 2026
@EmanueleCornaggia
EmanueleCornaggia marked this pull request as ready for review October 6, 2026 10:09
@github-actions github-actions Bot removed the stale Exempt from stale bot labeling. label Oct 6, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has not seen any recent activity.

@github-actions github-actions Bot added the stale Exempt from stale bot labeling. label Oct 14, 2026
@y0z y0z closed this Oct 15, 2026
@y0z y0z reopened this Oct 15, 2026
@y0z y0z added bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself. and removed stale Exempt from stale bot labeling. labels Oct 15, 2026
@y0z

y0z commented Oct 15, 2026

Copy link
Copy Markdown
Member

@not522 @kAIto47802 Could you review this PR?

@not522 not522 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@not522 not522 removed their assignment Oct 16, 2026

@kAIto47802 kAIto47802 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kAIto47802 kAIto47802 added this to the v5.1.0 milestone Oct 21, 2026
@kAIto47802
kAIto47802 merged commit de52376 into optuna:master Oct 21, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue/PR about behavior that is broken. Not for typos/examples/CI/test but for Optuna itself.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants