Skip to content

fix(session): skip nil parts in IsFinalResponse classification - #2189

Merged
wolo-lab merged 3 commits into
google:mainfrom
PratikDhanave:fix/isfinalresponse-nil-part
Nov 9, 2026
Merged

wolo-lab merged 3 commits into
google:mainfrom
PratikDhanave:fix/isfinalresponse-nil-part

Conversation

@PratikDhanave

@PratikDhanave PratikDhanave commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem

hasFunctionCalls, hasFunctionResponses and hasTrailingCodeExecutionResult (session/session.go) dereference each entry of Content.Parts without a nil check, so a nil part reaching Event.IsFinalResponse() — a public method called by the console launcher (cmd/launcher/console), the logging and analytics plugins, llmagent, and the LLM flow — panics:

e := &session.Event{LLMResponse: model.LLMResponse{
    Content: &genai.Content{Parts: []*genai.Part{nil}},
}}
_ = e.IsFinalResponse() // nil-pointer panic

Several sibling helpers that walk Parts already guard nil — EventCompaction.clone in the same file, internal/utils.FunctionCalls, compaction's llm_summarizer, memory/inmemory — and the analytics plugin strips nils before calling IsFinalResponse at 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.) IsFinalResponse had no in-package test.

Change

Guard nil entries in hasFunctionCalls and hasFunctionResponses the way the guarded siblings do (part != nil && …). hasTrailingCodeExecutionResult walks 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.

TestIsFinalResponseNilParts pins 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 (hasFunctionCalls runs first and dereferences the nil); the two code-execution rows are the only ones that reach that branch, so they also fail if the CodeExecutionResult check 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.

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.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

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 an issue already exists, add Closes: #<number> or Related: #<number> to the description.
  • If not, open one — the link prefills the bug template with your PR title — then reference it the same way.

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 wolo-lab 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 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:

  1. hasTrailingCodeExecutionResult looks 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} makes IsFinalResponse() return true, while the same slice without the nil returns false. 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 with hasFunctionCalls and hasFunctionResponses, which skip a nil wherever it sits, and with the analytics plugin, which strips nils before calling IsFinalResponse. 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.
  2. 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 CodeExecutionResult check deleted outright, ./session/... and every other package that calls IsFinalResponse stay green.

Description:

  • "Every sibling helper that walks Parts guards 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 in internal/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. hasFunctionCalls runs first and dereferences the leading nil.

Worth knowing, not blocking (code this PR does not touch):

  • With OutputKey set, llmagent still panics on the same input, now at llmagent.go:537 instead of inside IsFinalResponse. The console and the logging plugin dereference parts before they reach IsFinalResponse at all. So of the callers the description names, the ones that stop crashing are base_flow and the workflow agent node. #1769 covers the logging plugin. A guard at llmagent.go:537 would fit here or in a follow-up, your call.
  • Once [nil] gets past IsFinalResponse, isThoughtOnlyTurn reports 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.

Comment thread session/event_test.go Resolved
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).
@PratikDhanave

Copy link
Copy Markdown
Contributor Author

Both must-change items addressed in bffc6e8, and the description corrected:

  • Trailing-nil rule: went with skip-the-nils — hasTrailingCodeExecutionResult now walks back to the last non-nil part, so [{CodeExecutionResult}, nil] classifies the same as [{CodeExecutionResult}] (false), agreeing with hasFunctionCalls/hasFunctionResponses and with the analytics plugin's pre-stripping. The helper's doc comment states the rule.
  • The two code-execution rows are in (nil after → false, nil before → false). You're right they're the only rows reaching that branch — noted that in the description too.
  • Description corrections: the "every sibling helper guards nil" claim now lists the actual guarded/unguarded split (with the unguarded walks you enumerated called out as out of scope, logging plugin deferred to fix: drop null parts from REST requests and skip nil parts on read #1769), and it now says all rows panic before the fix since hasFunctionCalls dereferences first.

On the llmagent.go:537 guard with OutputKey set — agreed it's the same class; I'd rather keep this PR single-purpose and send it as a follow-up if that works for you.

@wolo-lab wolo-lab 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.

LGTM, thanks!

@wolo-lab
wolo-lab merged commit 75ae920 into google:main Nov 9, 2026
15 checks passed
wolo-lab added a commit that referenced this pull request Nov 16, 2026
, #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)
ocharrez added a commit to ocharrez/adk-go that referenced this pull request Nov 30, 2026
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
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