Repository navigation
fix: drop null parts from REST requests and skip nil parts on read - #1769
Conversation
|
Hi @Trainingcqy Thanks for putting together this fix The defensive p == nil checks across contents_processor, event.go, and logging_plugin cleanly resolve the panics . Along with these checks, what do you think about also cleaning incoming messages when requests are decoded in server/adkrest/controllers/runtime.go? Specifically, inside decodeRequestBody, we could strip nil parts from runAgentRequest.NewMessage.Parts (e.g. using slices.DeleteFunc). That way, nil pointers are never written to the session database in the first place, which also protects any custom plugins or user callbacks reading event.Content.Parts. |
|
Thanks for the suggestion. Done in 7484267: after decoding, |
karolpiotrowicz
left a comment
There was a problem hiding this comment.
Must change before merge:
- The
FunctionCallbranch of the rewrittenMarshalJSONloop, which turns a nilArgsinto"args":{}, has no test (inline). - A content whose parts are all nil now logs an empty string, where the same message after REST decoding logs
None(inline). - The
/run_liveparagraph in the description describes a panic that no longer happens on main (below).
I ran the full test suite on this branch merged into current main, and then the new tests with each changed file reverted. Each of the twelve new test cases fails without its fix and passes with it, and each of the four guards in contents_processor.go is caught by its own case. Requests and stored events without null parts decode, marshal and render the debug graph exactly as before.
Two notes on code this PR does not change:
onEventin the logging plugin still panics on a nil part. With the new skip informatContent,onEventgets one line further and then dereferences the same part inevent.IsFinalResponse()(logging_plugin.go#L161) and in its own loop (L167-L174). I reproduced it on main and on this branch with an event holding[nil, {Text: "x"}]. User messages never go throughonEvent, so the REST requests this PR fixes are not affected. What is left is an agent or model yielding a nil part, which overlaps with #1607 and fits a follow-up better than this PR.- The
/run_liveparagraph in the description is out of date. Since #1795 landed,runnerLiveSession.Senddrops nil parts before anything reads them (runner.go#L844-L852 on main), so a live message with a null part no longer panics where the description says it does (runner.go#L813 at this branch's base). Worth trimming that sentence so the remaining known panics list stays accurate.
The two inline notes are small.
|
Thanks for the review. Addressed in 0316e50. Revised the
|
Every item in this review is addressed on the current head. The review that follows replaces it.
karolpiotrowicz
left a comment
There was a problem hiding this comment.
Must change before merge:
- A
TestFormatContentSkipsNilPartsrow for content with no parts (inline).
The three points from my earlier review are addressed on this head:
- The new
nil_before_function_callrow fails with"args":nullwhen theargs == nildefault inMarshalJSON(event.go#L201-L203) is removed, so that branch is now covered. only_nilfails against the previousformatContent, which returned"", and passes now. I also ranformatContenton content with only nil parts, with no parts and with nil content, and each returnsNone.- The
/run_liveparagraph is gone from the description.
I re-ran the same checks on this head merged into current main, with no new failures.
|
Thanks for reviewing again. Addressed in a2208f5. |
The one item this review raised is fixed and verified on the current head.
karolpiotrowicz
left a comment
There was a problem hiding this comment.
The new no_parts row (logging_plugin_test.go#L32) covers the case my earlier review asked about. It fails when the post-loop check in formatContent (logging_plugin.go#L113-L115) is removed. It is also the only row that fails when that check is changed to return None only after a skipped nil part, so content with no parts is now pinned on its own rather than through only_nil.
I re-ran the same checks on this head merged into current main, with no new failures.
|
@Trainingcqy , please resolve conflicts |
karolpiotrowicz
left a comment
There was a problem hiding this comment.
Since my earlier review the branch has only merged main, so I checked the conflict resolution. The null-part handling in decodeRequestBody, CreateSessionHandler, the contents processor and formatContent came through unchanged. Dropping your Event.MarshalJSON and event-graph changes loses nothing, because #2192 skips nil parts in the same loops and has its own tests for them.
I re-ran the same checks on the merged head and they are all clean. The steps from #1768 also pass end to end through the REST server with the logging plugin enabled, and they still fail on main.
|
Merged the latest main again in 19f3118 to resolve the conflict with #2342. Only |
Linked issue
1. Link the issue this change is for.
2. Describe the change:
Problem:
An event with a nil part can be written to a session through
newMessagein/runand/run_sse, or througheventswhen a session is created. The following places do not check for nil and panic when they encounter a nil part:internal/llminternal/contents_processor.go:872shouldExcludeEventinternal/llminternal/contents_processor.go:812ConvertForeignEvent, whenIncludeContentsisnoneor a single_turn placement hides the history, and the latest event in the current branch is another agent's event with a nil partplugin/loggingplugin/logging_plugin.go:94formatContentmergeFunctionResponseEventsatinternal/llminternal/contents_processor.go:710and:724does not check for nil either. Every event it receives has already passed throughshouldExcludeEvent, so a nil part causes a panic at:872first. OnceshouldExcludeEventskips nil,mergeFunctionResponseEventspanics at these two lines whenever a long running tool's result contains a nil part.Solution:
The places above now skip nil parts, as the
internal/utilsaccessors already do.REST also drops null parts when it decodes a request:
decodeRequestBodydoes this fornewMessagein/runand/run_sse, and the session creation handler does the same forevents. Nil parts no longer reach the session through these three endpoints, and plugins and callbacks that readevent.Content.Partsdo not see them either. The read side still needs to skip nil: SDK callers write to a session without going through REST decoding, and a session that already holds a nil part can only be handled on read.adk-python rejects a null part while decoding the request and returns 422; see
src/google/adk/cli/api_server.py:562for the/runand/run_sserequest body, and:586foreventsin a session creation request. This PR takes a different approach: REST still accepts such a request and only drops the null parts in it; a null part carries no content, so dropping it loses no data. If rejecting it during decoding is preferred, that can go in a separate PR that returns 400, following the existing adkrest convention.Behavior change
What behaves differently for someone already on the current release?
A
/runor/run_sserequest with a null part returns 200. The null part is dropped during decoding, so it is not written to the session and does not appear in requests to the model; a message whose parts are all null is treated as an empty message. adk-python returns 422 for such a request; see above for why this PR differs. With loggingplugin enabled, the server no longer drops the connection on such a request. When a session is created, null parts ineventsare dropped the same way.When a session already holds a nil part, whether it was written before this fix, by the Agent Engine server, or by an SDK caller, later turns no longer fail, including when another agent's event has a nil part.
With the Vertex AI session backend, a nil part that bypasses adkrest decoding, such as one in an Agent Engine server request or in a message from an SDK caller, still causes a panic at
session/vertexai/vertexai_client.go:681. An SDK call that enablesRunConfig.SaveInputBlobsAsArtifactsand passes a message with a nil part still panics atrunner/runner.go:1107. These two places are out of scope for this PR.Testing Plan
Unit Tests:
With your source change reverted and your tests kept, which test fails?
TestContentsRequestProcessor:NilPartInUserEvent,AuthEventAfterNilPartTestContentsRequestProcessor_Rearrange: the twonil partcasesTestConvertForeignEvent:NilPartExcludedTestFormatContentSkipsNilPartsTestCreateSessionDropsNilPartsTestDecodeRequestBody_DropsNilPartsThe first four panic on a nil pointer dereference; the last two fail their assertions because the events stored in the session and the decoded request still contain null parts.
On Windows,
go test -race -shuffle=on ./internal/llminternal/... ./server/adkrest/... ./session/... ./plugin/...passes. On Linux under WSL,go test -shuffle=on ./...passes with go1.26.10.golangci-lint runreports no issues.Manual End-to-End (E2E) Tests:
Both backends, Gemini API and Vertex AI, were run on
49904c0donce before the fix and once after. The server is based onexamples/quickstart, with the model pinned togemini-3.8-flashand the write timeout set to 2 minutes through-write-timeout 2m, so that the server does not drop the connection before a slow model response arrives. s0 is the control session, s1 follows the steps in the issue, and s2 is created with a request whoseeventsinclude another agent's event with a null part. Before the fix, both/runcalls on s1 return 500, the server drops the connection when s1 is read and when s2 is created, both/runcalls on s2 return 500, and s0 succeeds throughout. After the fix, every step returns 200. Because null parts are dropped during decoding, the E2E run after the fix does not exercise the path that reads a stored nil part; the unit tests above cover it.Log
Checklist