Skip to content

fix(llminternal): keep streamed function-call and event ids stable - #2308

Merged
jjsasha63 merged 3 commits into
google:mainfrom
jjsasha63:fix/streaming-ids
Nov 13, 2026
Merged

jjsasha63 merged 3 commits into
google:mainfrom
jjsasha63:fix/streaming-ids

Conversation

@jjsasha63

@jjsasha63 jjsasha63 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • streamingResponseAggregator copies 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 as FunctionCall{WillContinue: false}.
  • generateContent reuses 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:

  • Partial events and the aggregated event that completes them now have the same 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.
  • Continuation chunks of a streamed function call, including a chunk that only closes the call, now carry the call's id. Before, they carried an empty id, which the flow replaced with a fresh client id per chunk.
  • The gcp.vertex.agent.event_id attribute on the generate_content span 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.WithUUIDProvider produces the same event ids.

A model that marks responses Partial even 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.AddEventsToMemory keeps 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:

  • All unit tests pass locally.

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 sets WillContinue to 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, generateContent yields 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 of TestRecoveredModelErrorGetsItsOwnEventID and the error case of TestGenerateContentDrawsOneIDPerEvent. Drawing a spare id per response fails every case of TestGenerateContentDrawsOneIDPerEvent that 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 fails TestCompleteResponsesOfOneModelCallGetDistinctEventIDs, 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, and go mod tidy -diff prints nothing.

Parity (adk-python v2.12.0; line numbers refer to that tag):

  • Matches: in src/google/adk/utils/streaming_utils.py, a continuation chunk without an id takes state.id (lines 414 to 415). src/google/adk/flows/llm_flows/base_llm_flow.py builds one model_response_event per LLM call and re-mints its id only after a non-partial event.
  • Diverges: Go decides on the model response's Partial flag before after-model callbacks run, while Python checks the postprocessed event (base_llm_flow.py line 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.
  • Not ported: when the first chunk of a streamed call has no id, adk-python mints a client id in the aggregator (streaming_utils.py lines 405 to 406). In Go such a stream's partial chunks still get separate client ids; that is left for a follow-up.
  • Not ported: adk-python flushes the streamed call when a chunk closes it (streaming_utils.py lines 366 to 368, reached through lines 382 to 386). adk-go does the same for a closing chunk that carries partial arguments, with WillContinue false or unset. A closing chunk with no name and no partial arguments, such as FunctionCall{WillContinue: false}, now carries the call's id. It does not end the call, on main or with this change, while test_streaming_function_call_empty_final_chunk_completes_call expects 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_id
  • tests/unittests/flows/llm_flows/test_progressive_sse_streaming.py::test_progressive_sse_text_stream_shares_event_id
  • tests/unittests/flows/llm_flows/test_progressive_sse_streaming.py::test_progressive_sse_partials_share_event_id

TestCompleteResponsesOfOneModelCallGetDistinctEventIDs follows test_progressive_sse_saved_events_get_distinct_ids in the same Python file.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

None.

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.
@jjsasha63 jjsasha63 self-assigned this Nov 13, 2026
@jjsasha63
jjsasha63 requested a review from wojtas000 November 13, 2026 11:02

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

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. generateContent draws an id for the error, callLLM yields (nil, err) and returns, and no event carries that id. I ran callLLM with 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 level finalizeModelResponseEvent also draws an id through session.NewEvent and 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 string replaced by eventID := platform.NewUUID(ctx), the whole root module suite still passes, because every row of TestGenerateContentDrawsOneIDPerEvent yields 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 stream is false, but not for a model that marks responses Partial anyway. Under StreamingModeNone the in-tree partialFunctionCallMockModel now 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 on useStream with a useStream=false test row would do it. Your call.
  • One consumer might be worth naming under "Behavior change", optionally. The in-memory memory.AddEventsToMemory keeps 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.
@jjsasha63

Copy link
Copy Markdown
Contributor Author

In f46cc5a the comments on generateContent and its test say only that an id is drawn when a response arrives, never ahead of one. The test has two new rows, one where the model yields nothing and one where it yields only (nil, nil), and both expect zero draws. Both fail when I replace var eventID string with eventID := platform.NewUUID(ctx). I kept non-streaming behavior as it is, since adk-python does not check the streaming mode either. The descripton now says it changes for a model that marks responses Partial under StreamingModeNone, and names the memory.AddEventsToMemory case.

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

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.

@jjsasha63
jjsasha63 merged commit 3188db9 into google:main Nov 13, 2026
15 checks passed
@jjsasha63
jjsasha63 deleted the fix/streaming-ids branch November 13, 2026 16:39
wolo-lab added a commit that referenced this pull request Nov 16, 2026
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants