Repository navigation
fix(llminternal): keep streamed function-call and event ids stable - #2308
Conversation
A model can stream one function call over several chunks. Only the first chunk carries the call id. The streaming aggregator kept that id for the aggregated call, but the continuation chunks went out with an empty id. The flow then gave each of them a different client id. Each response of a streaming model call also got its own event id. The partial events of one response and the aggregated event that completes them had unrelated ids, so a client could not tell they belong togther. adk-python fills a continuation chunk's missing id from the call being streamed (utils/streaming_utils.py), including a chunk that only closes the call. It also builds one model response event per LLM call and reuses its id for every partial chunk. It mints a fresh id only after a complete event (flows/llm_flows/base_llm_flow.py). The aggregator now copies the active call id onto continuation chunks that have none, with or without partial arguments. generateContent reuses one event id until a complete response or an error has been yielded. An on-model-error callback can turn an error into a saved event, so the next response must not share its id. The next id is drawn only when a response needs it, so non-streaming runs draw the same ids from the UUID provider as before.
wojtas000
left a comment
There was a problem hiding this comment.
Two small changes are needed before this merges, both about the lazy id draw in generateContent: the new comment gives a reason that doesn't hold, and no test pins the lazy draw.
Must change before merge:
- The comment at base_flow.go#L1259-L1263 says a new id "is drawn only when the next response arrives, so every id drawn from the provider ends up on an event". An unrecovered model error breaks the second half.
generateContentdraws an id for the error,callLLMyields(nil, err)and returns, and no event carries that id. I rancallLLMwith a counting UUID provider and a model that yields only an error, and got one draw and no event (main behaves the same). The test comment at base_flow_test.go#L1548-L1551 makes the same promise to an app with a deterministic provider, and at the flow levelfinalizeModelResponseEventalso draws an id throughsession.NewEventand then overwrites it (base_flow.go#L1359-L1360). Both comments should say only what holds: an id is drawn when a response arrives, never ahead of one. - Nothing pins that lazy draw. With
var eventID stringreplaced byeventID := platform.NewUUID(ctx), the whole root module suite still passes, because every row ofTestGenerateContentDrawsOneIDPerEventyields at least one response. A row where the model yields nothing, or only(nil, nil), and expects zero draws would catch it.
Two optional notes:
- The description says non-streaming mode is unchanged. That holds for models that send one complete response when
streamis false, but not for a model that marks responsesPartialanyway. UnderStreamingModeNonethe in-treepartialFunctionCallMockModelnow gets one id for its two partials and its final event, where main gave three. adk-python doesn't check the streaming mode either, so I think the behavior is fine and only the sentence is off. If you'd rather keep non-streaming exactly as before, gating the reset onuseStreamwith auseStream=falsetest row would do it. Your call. - One consumer might be worth naming under "Behavior change", optionally. The in-memory
memory.AddEventsToMemorykeeps the first event per id, so a caller that passes it everything the runner yielded in SSE mode now stores the first partial and drops the final event. Nothing in the repo calls it that way, and adk-python's in-memory service dedupes the same way.
What I ran, sandboxed: build, vet, gofmt and the full suite on head and on main, plus the touched packages on a merge with current main. With the source change reverted your new tests fail as described, and removing either of the new branches in the stream aggregator or changing the reset condition fails the suite.
generateContent draws an event id from the UUID provider when a response arrives, never ahead of one. No test covred that: drawing the id before the first response left the whole suite green, because every row of TestGenerateContentDrawsOneIDPerEvent yielded a response. The test now has a row where the model yields nothing and a row where it yields only a nil response with a nil error. Both expect no ids and zero draws, and both fail when the id is drawn eagarly. The comments on generateContent and on the test now say only what holds. An id is drawn when a response arrives. A model error that no callback recovers ends the id without an event carrying it. The comments therefore do not claim that every drawn id ends up on an event.
|
In f46cc5a the comments on |
wojtas000
left a comment
There was a problem hiding this comment.
Both items from my earlier review are addressed, so I'm approving. One suggestion, not blocking, and it comes from the wording I suggested last time: an error with no response also gets a new event id, and neither the comments nor the tests say so.
The comment at base_flow.go#L1259-L1263 says a new id "is drawn when the next response arrives", and the test comment at base_flow_test.go#L1548-L1550 says the same. My earlier suggestion left out errors. When the model yields (nil, err) and no id is open, generateContent draws one, and callLLM puts it on the event an on-model-error callback returns. Nothing tests that case. I changed if eventID == "" to if eventID == "" && resp != nil, which is what the comment describes, and the whole suite still passed. With that change, the event built for a recovered error that comes first, or right after a complete response, leaves the flow with an empty Event.ID. Let's say an id is drawn when a response or an error arrives. Let's also add two rows to TestGenerateContentDrawsOneIDPerEvent: a model that yields only an error (one id), and a complete response followed by an error alone (two ids). I ran both rows. They pass on this head and fail with the change above.
The two new rows fail when the id is drawn before the loop. I re-ran the same checks on this head and on a merge with current main, and they all pass.
…2071, #1249, #2070, #2190, #2301, #2310, #2308) (#2350) * fix(llminternal): keep a streamed text chunk's thought signature on the merged part (#2071) (cherry picked from commit 28201d6) * test(llminternal): pin keep-last semantics for streaming usage metadata (#1249) (cherry picked from commit afe6c86) * fix(llminternal): keep ModelVersion on the aggregated streaming response (#2070) (cherry picked from commit 8be9129) * fix(llminternal): drop nil parts in the streaming aggregator (#2190) (cherry picked from commit 46ff99b) * fix(llminternal): skip live responses that carry nothing (#2301) (cherry picked from commit 8cddb9d) * fix(llminternal): keep responses that carry only grounding metadata (#2310) (cherry picked from commit fc6de07) * fix(llminternal): keep streamed function-call and event ids stable (#2308) (cherry picked from commit 3188db9)
Please ensure you have read the contribution guide before creating a pull request.
Linked issue
None.
2. Describe the change:
Problem:
When a model streams one function call over several chunks, only the first chunk carries the call id. The streaming aggregator used that id for the aggregated call, but the continuation chunks were emitted with an empty id. The flow then gave each of them a different client id. So the partial events of one call disagred on its id.
Separately, every response of a streaming model call got its own event id. The partial events of one response and the aggregated event that completes them had unrelated ids. An SSE client could not tell which partials the final event replaces.
Solution:
streamingResponseAggregatorcopies the id of the call being streamed onto a continuation chunk that has none, as adk-python does. That covers chunks that carry partial arguments and nameless chunks that only close the call, such asFunctionCall{WillContinue: false}.generateContentreuses one event id for every partial response of a model call. A complete response ends the id, and so does an error, because an on-model-error callback can turn it into a saved event. The next response draws a new id, so distinct saved events in one call get distinct ids. An id is drawn when a response arrives, never ahead of one. This matches the single model response event adk-python builds per LLM call.Behavior change
In SSE streaming mode:
Event.ID. Before, every partial event had a unique id. Code that assumed partial event ids are unique will see repeats. Partial events are not saved to the session,so stored events still have unique ids.gcp.vertex.agent.event_idattribute on thegenerate_contentspan is now the id of the event stored for that response. Before, for a streamed response it was the id of a partial event that is never stored.A model that sends only complete responses when not streaming sees no change. Each response still gets its own id, drawn at the same point as before. An installed
platform.WithUUIDProviderproduces the same event ids.A model that marks responses
Partialeven when not streaming now gets one id for those partials and the response that completes them. This matches adk-python, which does not check the streaming mode.memory.AddEventsToMemorykeeps the first event per id. Passing it every event the runner yields in SSE mode stores the first partial insted of the final event.Testing Plan
Unit Tests:
I added these tests to
internal/llminternal/stream_aggregator_test.go:TestStreamingFunctionCallContinuationCarriesCallID: a continuation chunk without an id gets the streamed call's id, in the partial response and in the aggregated one. It runs three closing chunks: one with partial arguments, one that only setsWillContinueto false, and an empty one.TestProgressiveSSEPartialsShareEventID: three partials and the aggregated response of one call share one id; the function response and the second model call get different ids.TestProgressiveSSETextStreamSharesEventID: every partial of a pure text stream and the final response share one id.TestCompleteResponsesOfOneModelCallGetDistinctEventIDs: two complete responses from one model call (text, then a function call) get distinct ids. This test passes without the change too; it fails if the id is not re-minted after a complete response.TestRecoveredModelErrorGetsItsOwnEventID: a model streams a partial response, then an error (with or without a response), then a complete response, and an on-model-error callback recovers the error. The recovered event and the complete response are both saved, and they get distinct ids. This test also passes without the change; it fails if an error does not end the id.I added this test to
internal/llminternal/base_flow_test.go:TestGenerateContentDrawsOneIDPerEvent: with a counting UUID provider,generateContentyields the expected ids and draws exactly one id per distinct event id. It covers one complete response, partials and the response that completes them, two complete responses, and an error between partial and complete responses. A model that yields nothing, and one that yields only(nil, nil), draw no ids. It also checks that each model error comes through unchanged.No existing test pinned the old behavior, so none changed.
With your source change reverted and your tests kept, which test fails?
TestStreamingFunctionCallContinuationCarriesCallID, all three cases (partial response 1 function call ID = "", want "explicit_fc_id_123").TestProgressiveSSEPartialsShareEventID(events[1].ID = ..., want ... (the id of the partial events[0]), and the same for events 2 and 3).TestProgressiveSSETextStreamSharesEventID(same message for events 1 to 3).TestGenerateContentDrawsOneIDPerEvent, the partials case and the error case (each response got its own id).I also mutated each new branch in trun. Removing the fill on a nameless closing chunk fails the two matching cases of
TestStreamingFunctionCallContinuationCarriesCallID. Not ending the id on an error fails both cases ofTestRecoveredModelErrorGetsItsOwnEventIDand the error case ofTestGenerateContentDrawsOneIDPerEvent. Drawing a spare id per response fails every case ofTestGenerateContentDrawsOneIDPerEventthat yields a response. Drawing the id before the first response arrives fails the two rows where the model yields no response. Removing the id renewal failsTestCompleteResponsesOfOneModelCallGetDistinctEventIDs, and renewing after every response fails both shared-id tests.Results:
go build ./...succeeds.go test -race -count=1 ./internal/llminternal/... ./internal/telemetry/... ./agent/llmagent/ ./runner/passes.go test -count=1 ./...passes in all 119 packages that have tests.golangci-lint run --new-from-rev=origin/main ./...reports 0 issues, andgo mod tidy -diffprints nothing.Parity (adk-python v2.12.0; line numbers refer to that tag):
src/google/adk/utils/streaming_utils.py, a continuation chunk without an id takesstate.id(lines 414 to 415).src/google/adk/flows/llm_flows/base_llm_flow.pybuilds onemodel_response_eventper LLM call and re-mints its id only after a non-partial event.Partialflag before after-model callbacks run, while Python checks the postprocessed event (base_llm_flow.pyline 446). The flow keeps a partial response partial when an after-model callback replaces it (inheritModelFields), so those agree. An error has no flag to check, so Go ends the id after every error: an on-model-error callback's replacement is saved as a complete event.streaming_utils.pylines 405 to 406). In Go such a stream's partial chunks still get separate client ids; that is left for a follow-up.streaming_utils.pylines 366 to 368, reached through lines 382 to 386). adk-go does the same for a closing chunk that carries partial arguments, withWillContinuefalse or unset. A closing chunk with no name and no partial arguments, such asFunctionCall{WillContinue: false}, now carries the call's id. It does not end the call, on main or with this change, whiletest_streaming_function_call_empty_final_chunk_completes_callexpects it to. After such a chunk, a second streamed call in the same response still merges into the first, and text that follows is placed before the call.Manual End-to-End (E2E) Tests:
I did not run against a live model. The new unit tests port these adk-python scenarios, which adk-go now matches:
tests/unittests/utils/test_streaming_utils.py::TestStreamingFunctionCallArgs::test_streaming_fc_continuation_populates_state_idtests/unittests/flows/llm_flows/test_progressive_sse_streaming.py::test_progressive_sse_text_stream_shares_event_idtests/unittests/flows/llm_flows/test_progressive_sse_streaming.py::test_progressive_sse_partials_share_event_idTestCompleteResponsesOfOneModelCallGetDistinctEventIDsfollowstest_progressive_sse_saved_events_get_distinct_idsin the same Python file.Checklist
Additional context
None.