Skip to content

feat!: add sampling-strategy retry/repair to stream() - #1697

Merged
ajbozarth merged 6 commits into
generative-computing:mainfrom
ajbozarth:feat/403-streaming-sampling-results
Oct 21, 2026
Merged

ajbozarth merged 6 commits into
generative-computing:mainfrom
ajbozarth:feat/403-streaming-sampling-results

Conversation

@ajbozarth

Copy link
Copy Markdown
Member

Issue

Fixes #403

Description

stream() previously aborted on a failed requirement with no recourse. This adds the retry/repair behavior of non-streaming sampling to streaming by accepting a SamplingStrategy: a failed attempt is repaired and retried up to loop_budget, and the winning attempt is projected onto the Streamer's terminal state (full_text, mot, final_validations). Requirements set on the strategy are merged with those passed to stream(). With strategy=None, behavior is unchanged.

Two triggers drive a retry:

  • a mid-stream "fail" cancels the attempt at the failing chunk and repairs via the new stream_repair();
  • a completed attempt whose final validate() fails repairs via the existing repair().

Per-attempt history is recorded on Streamer.attempts (a new StreamAttempt dataclass), and a RetryEvent is emitted before each re-attempt. CompletedEvent gains attempts_used.

stream_repair() is a non-abstract method on BaseSamplingStrategy that raises NotImplementedError by default (custom strategies stay instantiable; streaming repair is opt-in); RejectionSamplingStrategy, RepairTemplateStrategy, and MultiTurnStrategy implement it. repair()/stream_repair() past_results is widened to Sequence[ComputedModelOutputThunk | None] so streaming can pass its per-attempt results (None for early-broken attempts) without narrowing existing callers.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

stream() aborted on a failed requirement with no recourse. Accepting a
SamplingStrategy lets it repair and retry a failed attempt up to loop_budget,
mirroring non-streaming sampling. A mid-stream "fail" repairs via a new
stream_repair(); a failing final validate() repairs via repair(). Per-attempt
history is recorded on Streamer.attempts, and strategy=None is unchanged.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
…requirements

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth
ajbozarth requested a review from a team as a code owner September 29, 2026 17:25
@github-actions github-actions Bot added the enhancement New feature or request label Sep 29, 2026
@ajbozarth ajbozarth added area/sampling SamplingStrategy, SamplingResult, ModelOption, generation options area/streaming Streaming chunks, events, per-chunk validation area/telemetry OTel spans, metrics, tracing, semconv labels Sep 29, 2026
@ajbozarth ajbozarth self-assigned this Sep 29, 2026

@planetf1 planetf1 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.

The main ones are the requirement merge, abandoned streams counting as sampling failures, and the repair() signature change. The rest are smaller.

Comment thread mellea/stdlib/streaming.py Resolved
Comment thread mellea/stdlib/streaming.py Resolved
Comment thread mellea/stdlib/sampling/base.py Resolved
Comment thread mellea/stdlib/streaming.py Resolved
Comment thread mellea/stdlib/streaming.py Resolved
Comment thread mellea/stdlib/sampling/base.py Resolved
Comment thread mellea/stdlib/__init__.py Resolved
Comment thread mellea/stdlib/streaming.py Resolved
Comment thread mellea/stdlib/streaming.py Resolved

@jakelorocco jakelorocco 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.

Agree with Nigel's comments as well.

Comment thread mellea/stdlib/streaming.py Resolved
Comment on lines 352 to 357
class ModelFriendlyRepairStrategy(RepairTemplateStrategy):
"""RepairTemplateStrategy with model-friendly feedback formatting.

Extends RepairTemplateStrategy to use ModelFriendlyFeedbackFormatter for
converting validation failures into actionable repair guidance. This typically
improves LLM performance on repair tasks compared to generic validation reasons.

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.

Should we also override .stream_repair for this one so that it uses the repair strategy for mid-stream repairs?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I dug into this and the short answer is no, primarily because this strategy is designed to work a specific set of requirements that all don't implement stream_validate and thus would never trigger stream_repair. and if a user gave a separate requirement outside those defaults it safely falls back to the default RepairTemplateStrategy.stream_repair

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>
@ajbozarth

Copy link
Copy Markdown
Member Author

I should have review feedback fixes pushed tomorrow morning along with responses inline, I've just run out of time today to wrap it up

@ajbozarth

Copy link
Copy Markdown
Member Author

@planetf1 @jakelorocco I believe I have responded to all your feedback, either addressing it or pushing back with a response or question.

If you could re-review please

@ajbozarth ajbozarth changed the title feat: add sampling-strategy retry/repair to stream() feat!: add sampling-strategy retry/repair to stream() Oct 20, 2026
BREAKING CHANGE: SamplingStrategy.repair()'s past_results parameter widens
from list[ComputedModelOutputThunk] to list[ComputedModelOutputThunk | None].
Custom repair() overrides typed against the narrower signature must widen to
match. No built-in strategy is affected -- they read past_actions[-1] /
past_val[-1], never past_results.

Assisted-by: Claude Code
Signed-off-by: Alex Bozarth <[email protected]>

@planetf1 planetf1 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.

Thanks Alex, LGTM.

@ajbozarth

Copy link
Copy Markdown
Member Author

@jakelorocco do you want to take another look before I merge this? you haven't responded since I addressed your feedback

@ajbozarth
ajbozarth added this pull request to the merge queue Oct 21, 2026
Merged via the queue into generative-computing:main with commit 242de10 Oct 21, 2026
12 checks passed
@ajbozarth
ajbozarth deleted the feat/403-streaming-sampling-results branch October 21, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/sampling SamplingStrategy, SamplingResult, ModelOption, generation options area/streaming Streaming chunks, events, per-chunk validation area/telemetry OTel spans, metrics, tracing, semconv enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: allow streaming for sampling results

3 participants