Repository navigation
fix(internal/utils): skip nil Parts in the Parts accessors - #1556
Conversation
|
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
left a comment
There was a problem hiding this comment.
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 Please review. |
* fix(internal/utils):skip nil Parts in the Parts accessors * fix(workflow): skip nil Parts in userInput
* fix(internal/utils):skip nil Parts in the Parts accessors * fix(workflow): skip nil Parts in userInput
…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)
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
Problem:
In
internal/utils/utils.go,genai.Content.Partsis[]*genai.Part, so a nil element isrepresentable 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.Partin all three accessors.Behavior change
Content with a nil
*genai.Partno longer panics.Testing Plan
New test
TestPartAccessorsSkipNilPartsininternal/utils/utils_test.gotests against nil*genai.Partin leading, middle and trailing position, all three accessors.go test ./internal/utils -vall passes.Unit Tests:
With your source change reverted and your tests kept, which test fails?
TestPartAccessorsSkipNilPartsininternal/utils/utils_test.goChecklist