Skip to content

fix(internal/utils): skip nil Parts in the Parts accessors - #1556

Merged
wojtas000 merged 8 commits into
google:mainfrom
dhakad22klx:fix/internal/utils-parts-accessors-panic
Sep 13, 2026
Merged

wojtas000 merged 8 commits into
google:mainfrom
dhakad22klx:fix/internal/utils-parts-accessors-panic

Conversation

@dhakad22klx

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

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

Problem:
In internal/utils/utils.go, genai.Content.Parts is []*genai.Part, so a nil element is
representable in content that comes from outside the library. All three
accessors dereference it unchecked and panic: FunctionCalls,
FunctionResponses, TextParts.

Solution:
Check and Skip nil *genai.Part in all three accessors.

Behavior change

Content with a nil *genai.Part no longer panics.

Testing Plan

New test TestPartAccessorsSkipNilParts in internal/utils/utils_test.go tests against nil *genai.Part in leading, middle and trailing position, all three accessors.

deepak@deepakdhakad:~/adk-go$ golangci-lint run
0 issues.
deepak@deepakdhakad:~/adk-go$ 

go test ./internal/utils -v all passes.

Unit Tests:

  • All unit tests pass locally.

With your source change reverted and your tests kept, which test fails?
TestPartAccessorsSkipNilParts in internal/utils/utils_test.go

deepak@deepakdhakad:~/adk-go$ go test ./internal/utils -run TestPartAccessorsSkipNilParts -v
=== RUN   TestPartAccessorsSkipNilParts
=== RUN   TestPartAccessorsSkipNilParts/leading
--- FAIL: TestPartAccessorsSkipNilParts (0.00s)
    --- FAIL: TestPartAccessorsSkipNilParts/leading (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
.
.
.
FAIL    google.golang.org/adk/v2/internal/utils 0.006s
FAIL
deepak@deepakdhakad:~/adk-go$

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.

@google-cla

google-cla Bot commented Sep 9, 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.

@wojtas000
wojtas000 self-requested a review September 10, 2026 12:26

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

Changes requested. The guards are correct; what blocks is the Closes: #1552 claim.

Clean: the three guards match the house pattern (scheduler.go:686, base_flow.go:185), short-circuit to exactly the old predicate for a non-nil part, and the new test panics on the unfixed base at internal/utils/utils.go:86, so it pins the fix rather than passing either way. Breaking changes: none — internal/utils is not public API and no caller indexes the results positionally.

Blocking — #1552's reproduction still panics. The issue sets two bars and this clears one. Its Acceptance line (three accessors, nil in leading/middle/trailing, unit test) is fully met. Its Reproduction is not: userInput at workflow/workflow.go:342 has the identical unguarded part.Text deref, on the path the issue names — workflowAgent.run → Workflow.Run → userInput.

Drop this in as internal/repro1552/repro_test.go on your branch and run go test ./internal/repro1552/ — it panics on this PR's head, and passes once the guard below is added:

package repro1552

import (
	"context"
	"strings"
	"testing"

	"google.golang.org/genai"

	"google.golang.org/adk/v2/agent"
	"google.golang.org/adk/v2/agent/workflowagent"
	"google.golang.org/adk/v2/runner"
	"google.golang.org/adk/v2/workflow"
)

func TestNilPartStillPanics(t *testing.T) {
	upper := workflow.NewFunctionNode("upper", func(ctx agent.Context, in string) (string, error) {
		return strings.ToUpper(in), nil
	}, workflow.NodeConfig{})
	a, err := workflowagent.New(workflowagent.Config{Name: "wf", Edges: workflow.Chain(workflow.Start, upper)})
	if err != nil {
		t.Fatal(err)
	}
	r, err := runner.NewInMemory("app", a)
	if err != nil {
		t.Fatal(err)
	}

	msg := &genai.Content{Role: genai.RoleUser, Parts: []*genai.Part{nil, {Text: "hi"}}}
	for range r.Run(context.Background(), "u", "s", msg, agent.RunConfig{}) {
	}
}
panic: runtime error: invalid memory address or nil pointer dereference
google.golang.org/adk/v2/workflow.userInput(...)          workflow/workflow.go:342
google.golang.org/adk/v2/workflow.(*Workflow).Run(...)    workflow/workflow.go:312

That path takes untrusted input, so this is not only a hand-built-content case: adkrest JSON-decodes newMessage into a genai.Content (models/runtime.go:30) and passes it straight to Runner.Run, and a body containing "parts":[null,{"text":"hi"}] decodes to a nil *genai.Part.

Please extend this PR with the same guard in userInput, plus a test that fails without it — the snippet above works as one:

	for _, part := range uc.Parts {
		if part != nil && part.Text != "" {

It is one line in the same class you are already fixing, and it is what makes Closes: #1552 true.

Unattached, same class, pre-existing and not this diff's job — but they belong in whatever sweep eventually closes #1552: internal/utils/utils.go:153 (AppendInstructions), internal/llminternal/contents_processor.go:606, :686, :735, and runner/runner.go:1081.

@dhakad22klx

dhakad22klx commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Changes requested. The guards are correct; what blocks is the Closes: #1552 claim.

Clean: the three guards match the house pattern (scheduler.go:686, base_flow.go:185), short-circuit to exactly the old predicate for a non-nil part, and the new test panics on the unfixed base at internal/utils/utils.go:86, so it pins the fix rather than passing either way. Breaking changes: none — internal/utils is not public API and no caller indexes the results positionally.

Blocking — #1552's reproduction still panics. The issue sets two bars and this clears one. Its Acceptance line (three accessors, nil in leading/middle/trailing, unit test) is fully met. Its Reproduction is not: userInput at workflow/workflow.go:342 has the identical unguarded part.Text deref, on the path the issue names — workflowAgent.run → Workflow.Run → userInput.

Drop this in as internal/repro1552/repro_test.go on your branch and run go test ./internal/repro1552/ — it panics on this PR's head, and passes once the guard below is added:

package repro1552

import (
	"context"
	"strings"
	"testing"

	"google.golang.org/genai"

	"google.golang.org/adk/v2/agent"
	"google.golang.org/adk/v2/agent/workflowagent"
	"google.golang.org/adk/v2/runner"
	"google.golang.org/adk/v2/workflow"
)

func TestNilPartStillPanics(t *testing.T) {
	upper := workflow.NewFunctionNode("upper", func(ctx agent.Context, in string) (string, error) {
		return strings.ToUpper(in), nil
	}, workflow.NodeConfig{})
	a, err := workflowagent.New(workflowagent.Config{Name: "wf", Edges: workflow.Chain(workflow.Start, upper)})
	if err != nil {
		t.Fatal(err)
	}
	r, err := runner.NewInMemory("app", a)
	if err != nil {
		t.Fatal(err)
	}

	msg := &genai.Content{Role: genai.RoleUser, Parts: []*genai.Part{nil, {Text: "hi"}}}
	for range r.Run(context.Background(), "u", "s", msg, agent.RunConfig{}) {
	}
}
panic: runtime error: invalid memory address or nil pointer dereference
google.golang.org/adk/v2/workflow.userInput(...)          workflow/workflow.go:342
google.golang.org/adk/v2/workflow.(*Workflow).Run(...)    workflow/workflow.go:312

That path takes untrusted input, so this is not only a hand-built-content case: adkrest JSON-decodes newMessage into a genai.Content (models/runtime.go:30) and passes it straight to Runner.Run, and a body containing "parts":[null,{"text":"hi"}] decodes to a nil *genai.Part.

Please extend this PR with the same guard in userInput, plus a test that fails without it — the snippet above works as one:

	for _, part := range uc.Parts {
		if part != nil && part.Text != "" {

It is one line in the same class you are already fixing, and it is what makes Closes: #1552 true.

Unattached, same class, pre-existing and not this diff's job — but they belong in whatever sweep eventually closes #1552: internal/utils/utils.go:153 (AppendInstructions), internal/llminternal/contents_processor.go:606, :686, :735, and runner/runner.go:1081.

Hi @wojtas000 Pushed requested changes. Checking nil Parts in userInput and added test that will fail without it. Below are the terminal results.

deepak@deepakdhakad:~/adk-go$ go test ./workflow/
ok      google.golang.org/adk/v2/workflow       0.659s
deepak@deepakdhakad:~/adk-go$ go test ./internal/utils/
ok      google.golang.org/adk/v2/internal/utils 0.004s
deepak@deepakdhakad:~/adk-go$

Please review.

@wojtas000 wojtas000 changed the title fix(internal/utils):skip nil Parts in the Parts accessors fix(internal/utils): skip nil Parts in the Parts accessors Sep 13, 2026

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

LGTM, thanks

@wojtas000
wojtas000 merged commit f4666ec into google:main Sep 13, 2026
14 checks passed
kdroste-google pushed a commit to kdroste-google/adk-go that referenced this pull request Sep 29, 2026
* fix(internal/utils):skip nil Parts in the Parts accessors

* fix(workflow): skip nil Parts in userInput
li-xuankun312 added a commit to li-xuankun312/einsteinOS that referenced this pull request Sep 29, 2026
* fix(internal/utils):skip nil Parts in the Parts accessors

* fix(workflow): skip nil Parts in userInput
wolo-lab added a commit that referenced this pull request Oct 2, 2026
…1630, #630) (#1692)

* fix(internal/utils): skip nil Parts in the Parts accessors (#1556)

(cherry picked from commit f4666ec)

* fix(llminternal): build arrays when assembling streamed function call arguments (#1630)

(cherry picked from commit d6bdb0d)

* fix: skip empty model responses in generateContent (#630)

(cherry picked from commit 8b0de7b)
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.

internal/utils: Parts accessors panic on a nil genai.Part

2 participants