Repository navigation
fix(session): skip nil parts in IsFinalResponse classification - #2189
Conversation
hasFunctionCalls, hasFunctionResponses and hasTrailingCodeExecutionResult dereferenced each entry of Content.Parts without a nil check, so a nil part reaching Event.IsFinalResponse - a public method called by the console launcher, logging and analytics plugins, and the LLM flow - panicked the process. Every sibling helper that walks Parts guards nil (EventCompaction.clone in the same file, internal/utils, base_flow, compaction's summarizer, the in-memory memory service); these three were the exception. Guard nil the same way and pin the behavior with a table test over nil parts mixed with calls, responses and text; the only-nil and trailing-nil cases panic before the fix.
|
Thanks for the PR, @PratikDhanave. One thing before review. Our contribution guide asks for an issue behind each change, and I could not find one linked here.
If this is a small doc or typo fix, ignore me. A maintainer can open the issue for you if you would rather. |
wolo-lab
left a comment
There was a problem hiding this comment.
Two things need changing before merge, both small. The third helper still lets a trailing nil decide the classification, and the new table has no row that would catch it. Nothing else blocks.
Must change before merge:
hasTrailingCodeExecutionResultlooks only at the literal last slot, so with this fix a nil after a code-execution result hides it:Parts: []*genai.Part{{CodeExecutionResult: &genai.CodeExecutionResult{}}, nil}makesIsFinalResponse()returntrue, while the same slice without the nil returnsfalse. That is the opposite of what the title and the new test comment promise ("must skip them the way the rest of the codebase does"), and it disagrees withhasFunctionCallsandhasFunctionResponses, which skip a nil wherever it sits, and with the analytics plugin, which strips nils before callingIsFinalResponse. Walking back to the last non-nil part makes all three helpers agree. If literal-last is the rule you want instead, the helper's doc comment and the test comment need to say so. Before this PR the same input panicked, so this is not a regression. It is the fix choosing a rule its own description contradicts.- Two more table rows, pinned inline on the test. Whichever rule you pick in item 1, they are the only rows that exercise the code-execution branch at all: with the
CodeExecutionResultcheck deleted outright,./session/...and every other package that callsIsFinalResponsestay green.
Description:
- "Every sibling helper that walks
Partsguards nil … these three were the exception" does not hold. Unguarded walks remain in the logging plugin, the console, llmagent, the stream aggregator and contents_processor, which the description lists as guarded, plus an unguarded last-part index ininternal/utils. The positive examples you list are accurate. - All four new rows panic before the fix, not only the nil-only and trailing-nil ones.
hasFunctionCallsruns first and dereferences the leading nil.
Worth knowing, not blocking (code this PR does not touch):
- With
OutputKeyset,llmagentstill panics on the same input, now atllmagent.go:537instead of insideIsFinalResponse. The console and the logging plugin dereference parts before they reachIsFinalResponseat all. So of the callers the description names, the ones that stop crashing arebase_flowand the workflow agent node. #1769 covers the logging plugin. A guard atllmagent.go:537would fit here or in a follow-up, your call. - Once
[nil]gets pastIsFinalResponse,isThoughtOnlyTurnreports an all-nil slice as a thought-only turn (an empty slice is not), so the flow calls the model again, capped at 10. Follow-up material.
Review follow-up: the helper looked only at the literal last slot, so a nil after a code-execution result flipped IsFinalResponse to true while the same slice without the nil returned false. Walk back to the last non-nil part so all three classification helpers agree on skipping nils wherever they sit, and add the two table rows that exercise the code-execution branch (nil after and nil before the result).
|
Both must-change items addressed in bffc6e8, and the description corrected:
On the |
, #1919, #2189, #2285) (#2346) * fix(session/vertexai): drop temp: keys from the initial state on Create (#2100) (cherry picked from commit 0c109f7) * fix(session): keep in-memory Get from returning events before After (#2089) (cherry picked from commit 10395f0) * fix(session/database): lock app and user state rows in Create (#1930) (cherry picked from commit 2f3b2c2) * fix(session/database): delete a session's events explicitly in Delete (#1774) (cherry picked from commit 7af0f5f) * fix(session): define List order as last update time ascending (#1919) (cherry picked from commit 8be7e25) * fix(session): skip nil parts in IsFinalResponse classification (#2189) (cherry picked from commit 75ae920) * fix(session): count a final error event as a final response (#2285) (cherry picked from commit d9ecb34)
genai.Content.Parts is a []*genai.Part, so a callback reply or a non-genai model backend can put a nil entry in an event. google#2189 made Event.IsFinalResponse skip nil parts, but the console launcher and the logging plugin, which the issue lists among IsFinalResponse's callers, still dereferenced every part themselves and panicked: - the console launcher printing an event's text - the console's scan for pending interrupts - the logging plugin's OnEvent, collecting function call and response names - the logging plugin's BeforeModel, joining the system instruction In the console the panic ends the process: nothing between the workflow scheduler and the console's range loop recovers it. The console print loop and the system instruction loop now skip nil parts. The interrupt scan and OnEvent use the nil-safe LLMResponse.FunctionCalls and FunctionResponses accessors, as adk-python's logging plugin uses event.get_function_calls() at d1d78328 (src/google/adk/plugins/logging_plugin.py:152-157). For existing users: an event or system instruction with a nil part used to crash the console and the logging plugin. The nil part is now skipped. Output for content without nil parts is unchanged. Fixes google#1607
Problem
hasFunctionCalls,hasFunctionResponsesandhasTrailingCodeExecutionResult(session/session.go) dereference each entry ofContent.Partswithout a nil check, so a nil part reachingEvent.IsFinalResponse()— a public method called by the console launcher (cmd/launcher/console), the logging and analytics plugins,llmagent, and the LLM flow — panics:Several sibling helpers that walk
Partsalready guard nil —EventCompaction.clonein the same file,internal/utils.FunctionCalls, compaction'sllm_summarizer,memory/inmemory— and the analytics plugin strips nils before callingIsFinalResponseat all. (Unguarded walks do remain elsewhere — the logging plugin, the console,llmagent, the stream aggregator,contents_processor— so the nil-skipping convention is the majority rule, not universal; those callers are out of scope here, and the logging plugin's is covered by #1769.)IsFinalResponsehad no in-package test.Change
Guard nil entries in
hasFunctionCallsandhasFunctionResponsesthe way the guarded siblings do (part != nil && …).hasTrailingCodeExecutionResultwalks back to the last non-nil part, so a stray nil after a code-execution result doesn't flip the classification — all three helpers agree on skipping nils wherever they sit, matching the analytics plugin's pre-stripping behavior.TestIsFinalResponseNilPartspins it with six rows: nil-only, nil-before-function-call, nil-before-function-response, trailing-nil-after-text, nil-after-code-execution-result, and nil-before-code-execution-result. All rows panic before the fix (hasFunctionCallsruns first and dereferences the nil); the two code-execution rows are the only ones that reach that branch, so they also fail if theCodeExecutionResultcheck is deleted outright.Verification
go test ./session/... -race -count=1— pass; the new test panics with the fix reverted.golangci-lint run ./session/...— 0 issues.