Repository navigation
fix(llminternal): keep responses that carry only grounding metadata - #2310
Conversation
The non-live flow dropped a model response with no content, no error code and no interruption. A response that carried only grounding metadata matched that check, so its metadata never reachd an event. adk-python keeps such responses. BaseLlmFlow._postprocess_async skips an empty response only when it also has no grounding_metadata. The flow now does the same. A saved grounding-only event has no content. An agent with IncludeContents none starts its request at the latest user or other-agent event, and it accepted such an empty event as that start. The event never reaches the request, so the user input before it was cut out. Error responses and responses with no parts could already do this. adk-python's _get_current_turn_contents only starts a turn at an event that passes _should_include_event_in_context, which rejects empty events. The turn-start search now skips them the same way, with the emptiness check the contents processor already used to drop events.
wojtas000
left a comment
There was a problem hiding this comment.
I compared both changes with adk-python's _postprocess_async and _get_current_turn_contents, and followed the new content-less event through Flow.Run, the runner and the code that consumes events. I also ran the two new tests on main. Each one fails without its own source change and passes with it.
None of my notes block, and all of them are optional. The behavior-change section leaves out two cases that users will see after upgrading:
- In streaming mode, a chunk that carries only grounding metadata now reaches callers as a partial event with no content. The final aggregated event still carries the metadata, as it did before.
- An agent with
IncludeContents: none, run with a user message that has no parts, now gets the previous user turn, its own earlier reply and the "Continue processing" prompt. Before this change the request had no contents. This matches adk-python.
|
Both hold, and the description of #2319 lists them. In streaming mode, a grounding-only chunk now reachs callers as a partial event with no content, and the final aggregated event keeps the metadata. An |
…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:
Grounding metadata from a search tool can arrive on a model response that has no content. The non-live flow skips any response with no content, no error code and no interruption. A grounding-only response therefore never becomes an event. Callers that render citations or search suggestions see nothing for these respones. adk-python keeps them.
Solution:
Flow.runOneStepkeeps a response that has no content when it carriesGroundingMetadata, matching the skip condition inBaseLlmFlow._postprocess_async.IncludeContents: noneno longer starts its current turn at an event that gives the model nothing to read. Such an event has no content with a role and parts, no transcription and no compaction summary. It never reaches the request,so starting the turn there cut the user input out of it. A grounding-only event saved by the first change would often be that event, for example in aSequentialAgentwhose second agent hasIncludeContents: none. adk-python's_get_current_turn_contentsonly starts a turn at an event that passes_should_include_event_in_context, which rejects empty events. The emptiness check is the one the contents processor already used to drop events, moved intoisEmptyEventso both places share it.Behavior change
Event.GroundingMetadata. The event is saved to the session. Before, the response was dropped.IncludeContents: nonethat runs after another agent's event with no content now gets the user input that came before that event. Before, its turn started at the empty event, which left the user input out of the request. Besides grounding-only events, this applies to error responses and responses with no parts, which could already be saved before this change.Testing Plan
Unit Tests:
I added these tests:
TestRunKeepsGroundingOnlyResponse(internal/llminternal/base_flow_test.go): a grounding-only response yields one event with the metadata, and an empty response still yields no event.TestContentsRequestProcessor_IncludeContentsNone_SkipsEmptyPivot(internal/llminternal/contents_processor_test.go): after a user message and another agent's grounding-only, error or part-less reply, anIncludeContents: noneagent's request holds the user message.No existing test pinned the old behavior.
With your source change reverted and your tests kept, which test fails?
TestRunKeepsGroundingOnlyResponse/grounding_metadata_only: the event list isnilwhere one event with the metadata is expected.TestContentsRequestProcessor_IncludeContentsNone_SkipsEmptyPivot, all three subtests: the request contents arenilwhere the user message is expected.Inverting the new guard (
resp.GroundingMetadata != nil) failsTestRunKeepsGroundingOnlyResponse/empty_response. InisEmptyEvent, dropping the zero-parts condition fails the part-less subtest; dropping the role, transcription or compaction condition fails existing tests (TestBuildContents_ResponseInSuffixToOtherAgentCallBeforeIt,TestContentsRequestProcessor/TranscriptionAggregation,TestContentsRequestProcessor_Compaction).go build ./...succeeds.go test -race -count=1 ./internal/llminternal/... ./agent/workflowagents/...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:
flows/llm_flows/base_llm_flow.py(_postprocess_async) andflows/llm_flows/context/_contents.py(_get_current_turn_contents, lines 656 to 749, and_contains_empty_content, lines 256 to 285) from adk-python 2.12.0. The non-live skip condition now matches, includingnot llm_response.grounding_metadata, and so does the rule that an empty event cannot start the current turn._contains_empty_contentalso counts content whose parts are all thoughts as empty. adk-go keeps its existing rule for that case: another agent's thought-only event is already skipped as a turn start, because foreign conversion discards it.Manual End-to-End (E2E) Tests:
I did not run an end-to-end test against a live model. adk-go now matches the adk-python unit test
tests/unittests/flows/llm_flows/test_base_llm_flow.py::test_postprocess_async_yields_grounding_metadata_only, where a response with onlygrounding_metadatayields exactly one event carrying it.TestRunKeepsGroundingOnlyResponsechecks the same thing throughFlow.Run.Checklist
Additional context
The live connection drops grounding metadata too. #2265 copies it in
LiveConnection.Recv, so this PR leaves the live path alone.#2309 changes the
postprocesscall just above the skip condition inrunOneStep. The two PRs touch neighboring lines, so whichver lands second needs a small rebase.