fix: preserve partial and turn_complete when after_model_callback returns a replacement - #7038
Open
zhuhongd wants to merge 1 commit into
Open
fix: preserve partial and turn_complete when after_model_callback returns a replacement#7038zhuhongd wants to merge 1 commit into
zhuhongd wants to merge 1 commit into
Conversation
…urns a replacement A callback that returns a replacement LlmResponse (as the documented after_model_callback contract suggests) rarely sets the streaming-control fields. LlmResponse.partial defaults to None, which the streaming protocol treats as a final response, so during SSE streaming every replaced delta became a final event: clients rendered N final responses and the Runner persisted every fragment to the session as a separate complete model event. Treat None as unset and inherit partial/turn_complete from the response being replaced at both after-model-callback call sites (live and SSE). Explicit values set by a callback are still respected. Fixes google#7035
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to Issue or Description of Change
1. Link to an existing issue:
partialduring SSE streaming - every delta is persisted to the session as a final event #7035Problem:
The documented
after_model_callbackcontract invites callbacks to return a replacement response, but during SSE streaming the callback fires on every streamedLlmResponseand the replacement is used wholesale.LlmResponse.partialdefaults toNone, which the streaming protocol treats as final, so any rebuilt replacement silently converts every delta into a final response: clients render N "final" responses, and theRunnerpersists every fragment to the session as a separate complete model event (session history corruption). Self-contained repro in #7035.Solution:
Treat
Noneas "unset" and inheritpartial/turn_completefrom the response being replaced, at both after-model-callback call sites (live and SSE paths), via a small helper_inherit_unset_streaming_fields. Explicit values set by a callback (e.g.partial=Falseto deliberately finalize) are still respected. This makes the documented callback pattern safe without removing any behavior a callback could previously express intentionally.Testing Plan
Unit Tests:
Added to
tests/unittests/flows/llm_flows/test_base_llm_flow_partial_handling.py:test_after_model_callback_replacement_preserves_partial_in_sse— adoc-contract callback (rebuilds
LlmResponse) on a streamed turn of 3partial deltas + aggregated final now yields
[True, True, True, None]partial flags (was
[None, None, None, None]before the fix).test_after_model_callback_explicit_partial_false_is_respected— acallback that deliberately finalizes a delta is not overridden.
test_inherit_unset_streaming_fields_inherits_when_unsetandtest_inherit_unset_streaming_fields_respects_explicit_values— directunit tests of the helper, including
turn_complete.Manual End-to-End (E2E) Tests:
Ran the self-contained repro script from #7035 (fake streaming model, no API
key required) against this branch: the BUG scenario now matches CONTROL —
deltas stay
partial=Trueand the session persists exactly one final modelevent instead of one event per delta. The
model_copyworkaround scenario isunchanged.
Checklist
Additional context
Formatted with
isort+pyinkperpyproject.toml. Found while running aproduction multi-agent assistant where a guardrail-style
after_model_callbackrebuilt responses per the documented contract.