Skip to content

fix: drop null parts from REST requests and skip nil parts on read - #1769

Merged
wolo-lab merged 13 commits into
google:mainfrom
Trainingcqy:fix/nil-parts-in-stored-events
Nov 19, 2026
Merged

wolo-lab merged 13 commits into
google:mainfrom
Trainingcqy:fix/nil-parts-in-stored-events

Conversation

@Trainingcqy

@Trainingcqy Trainingcqy commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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 newMessage in /run and /run_sse, or through events when 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:872 shouldExcludeEvent
  • internal/llminternal/contents_processor.go:812 ConvertForeignEvent, when IncludeContents is none or a single_turn placement hides the history, and the latest event in the current branch is another agent's event with a nil part
  • plugin/loggingplugin/logging_plugin.go:94 formatContent

mergeFunctionResponseEvents at internal/llminternal/contents_processor.go:710 and :724 does not check for nil either. Every event it receives has already passed through shouldExcludeEvent, so a nil part causes a panic at :872 first. Once shouldExcludeEvent skips nil, mergeFunctionResponseEvents panics 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/utils accessors already do.

REST also drops null parts when it decodes a request: decodeRequestBody does this for newMessage in /run and /run_sse, and the session creation handler does the same for events. Nil parts no longer reach the session through these three endpoints, and plugins and callbacks that read event.Content.Parts do 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:562 for the /run and /run_sse request body, and :586 for events in 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 /run or /run_sse request 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 in events are 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 enables RunConfig.SaveInputBlobsAsArtifacts and passes a message with a nil part still panics at runner/runner.go:1107. These two places are out of scope for this PR.

Testing Plan

Unit Tests:

  • All unit tests pass locally.

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

  • TestContentsRequestProcessor: NilPartInUserEvent, AuthEventAfterNilPart
  • TestContentsRequestProcessor_Rearrange: the two nil part cases
  • TestConvertForeignEvent: NilPartExcluded
  • TestFormatContentSkipsNilParts
  • TestCreateSessionDropsNilParts
  • TestDecodeRequestBody_DropsNilParts

The 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 run reports no issues.

Manual End-to-End (E2E) Tests:

Both backends, Gemini API and Vertex AI, were run on 49904c0d once before the fix and once after. The server is based on examples/quickstart, with the model pinned to gemini-3.8-flash and 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 whose events include another agent's event with a null part. Before the fix, both /run calls on s1 return 500, the server drops the connection when s1 is read and when s2 is created, both /run calls 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
===== gemini, base 49904c0d =====
backend: BackendGeminiAPI, model: gemini-3.8-flash
server: web -port 18300 -write-timeout 2m api
POST sessions/s0  {}
  -> 200
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I can only provide information about the current time and weather in a specific city. If you have a question about the time or weather for a particular location, please let me know!
POST sessions/s1  {}
  -> 200
POST /run  s1  parts=[null,{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
POST /run  s1  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
GET  sessions/s1
  -> 000 connection closed; server log: http: panic serving ...: runtime error: invalid memory address or nil pointer dereference
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I am only able to answer questions regarding the current time and weather in a specific city. Please provide a city name and ask for its time or weather.
POST sessions/s2  events=[{"author":"other_agent","content":{"parts":[null,{"text":"It is sunny in Paris."}]}}]
  -> 000 connection closed; server log: http: panic serving ...: runtime error: invalid memory address or nil pointer dereference
POST /run  s2  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
POST /run  s2  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
===== gemini, fixed (49904c0d + this PR) =====
backend: BackendGeminiAPI, model: gemini-3.8-flash
server: web -port 18300 -write-timeout 2m api
POST sessions/s0  {}
  -> 200
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I am only able to provide the current time and weather for a specific city. Please ask a question related to time or weather.
POST sessions/s1  {}
  -> 200
POST /run  s1  parts=[null,{"text":"THY"}]
  -> 200 model: I can only answer questions about the current time and weather in a specific city. Please let me know which city you would like to know the time or weather for!
POST /run  s1  parts=[{"text":"THY"}]
  -> 200 model: I can only answer questions about the current time and weather in a specific city. If you would like to know the time or weather somewhere, please specify the city.
GET  sessions/s1
  -> 200 user parts: [{"text": "THY"}], [{"text": "THY"}]
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I can only answer questions regarding the current time and weather in a specific city. Please provide a city name and let me know if you would like to know the time or weather there.
POST sessions/s2  events=[{"author":"other_agent","content":{"parts":[null,{"text":"It is sunny in Paris."}]}}]
  -> 200
POST /run  s2  parts=[{"text":"THY"}]
  -> 200 model: It is currently nighttime in **Paris, France**, so it is not sunny at the moment: * **Current Time:** 4:04 AM CEST (Saturday, October 10, 2026) * **Current Weather:** Mostly cloudy with light rain/showers and temperatures around 11°C–14°C (52°F–57°F)
POST /run  s2  parts=[{"text":"THY"}]
  -> 200 model: Please let me know if you would like to know the current time or weather for a specific city!
===== vertex, base 49904c0d =====
backend: BackendVertexAI, model: gemini-3.8-flash
server: web -port 18300 -write-timeout 2m api
POST sessions/s0  {}
  -> 200
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I can only assist with questions regarding the current time and weather in a specific city. Please let me know which city you would like time or weather information for!
POST sessions/s1  {}
  -> 200
POST /run  s1  parts=[null,{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
POST /run  s1  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
GET  sessions/s1
  -> 000 connection closed; server log: http: panic serving ...: runtime error: invalid memory address or nil pointer dereference
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I cannot assist with that. My sole purpose is to provide the current time and weather for a specific city. Please provide a city name if you need its current time or weather conditions.
POST sessions/s2  events=[{"author":"other_agent","content":{"parts":[null,{"text":"It is sunny in Paris."}]}}]
  -> 000 connection closed; server log: http: panic serving ...: runtime error: invalid memory address or nil pointer dereference
POST /run  s2  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
POST /run  s2  parts=[{"text":"THY"}]
  -> 500 failed to run agent: node "weather_time_agent" panicked: runtime error: invalid memory address or nil pointer dereference
===== vertex, fixed (49904c0d + this PR) =====
backend: BackendVertexAI, model: gemini-3.8-flash
server: web -port 18300 -write-timeout 2m api
POST sessions/s0  {}
  -> 200
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I can only answer questions about the current time and weather in a specific city. Please let me know which city you would like time or weather information for!
POST sessions/s1  {}
  -> 200
POST /run  s1  parts=[null,{"text":"THY"}]
  -> 200 model: I can only provide information about the current time and weather for a specific city. Please let me know which city you would like to know the time or weather for!
POST /run  s1  parts=[{"text":"THY"}]
  -> 200 model: I cannot help with that. My only purpose is to provide the current time and weather for a specific city. Please ask about the time or weather in a city (for example: "What is the weather in Tokyo?" or "What time is it in Paris?").
GET  sessions/s1
  -> 200 user parts: [{"text": "THY"}], [{"text": "THY"}]
POST /run  s0  parts=[{"text":"THY"}]
  -> 200 model: I can only answer questions about the current time and weather in a specific city. Please provide the name of a city you would like time or weather details for.
POST sessions/s2  events=[{"author":"other_agent","content":{"parts":[null,{"text":"It is sunny in Paris."}]}}]
  -> 200
POST /run  s2  parts=[{"text":"THY"}]
  -> 200 model: It is currently nighttime in Paris, France: * **Local Time:** 4:08 AM CEST (Saturday, October 10, 2026) * **Weather:** Mostly cloudy * **Temperature:** Approximately 13°C (56°F) Because it is nighttime, it is currently not sunny.
POST /run  s2  parts=[{"text":"THY"}]
  -> 200 model: You're welcome! Please let me know if you need the current time or weather for a specific city.

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.
  • Any dependent changes have been merged and published in downstream modules.

@indurireddy-TF

Copy link
Copy Markdown
Contributor

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.

@Trainingcqy Trainingcqy changed the title fix: tolerate nil parts that REST requests write to a session fix: drop null parts from REST requests and skip nil parts on read Oct 10, 2026
@Trainingcqy

Copy link
Copy Markdown
Contributor Author

Thanks for the suggestion. Done in 7484267: after decoding, decodeRequestBody drops nil parts from NewMessage.Parts. The events for creating a session are decoded separately in sessions.go and get the same treatment; otherwise nil parts would still be written to the session through that endpoint. The PR keeps its existing checks on the read side: 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. The other commit, e32d6f6, adds an assertion to TestEventGraphHandlerSkipsNilParts that checks whether the event graph highlights the tool; it is unrelated to your suggestion.

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

Must change before merge:

  • The FunctionCall branch of the rewritten MarshalJSON loop, which turns a nil Args into "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_live paragraph 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:

  • onEvent in the logging plugin still panics on a nil part. With the new skip in formatContent, onEvent gets one line further and then dereferences the same part in event.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 through onEvent, 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_live paragraph in the description is out of date. Since #1795 landed, runnerLiveSession.Send drops 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.

Comment thread plugin/loggingplugin/logging_plugin.go Resolved
@Trainingcqy

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 0316e50.

Revised the /run_live part of the description. memory/inmemory.go:86 and plugin/agentanalytics/json_formatter.go:38 no longer panic on current main, so I removed them too. Only Vertex and SaveInputBlobsAsArtifacts remain out of scope now.

onEvent: the IsFinalResponse part is in the scope of #1608; I will open a separate issue and PR later for the loop over parts in onEvent itself.

@karolpiotrowicz
karolpiotrowicz dismissed their stale review October 29, 2026 22:04

Every item in this review is addressed on the current head. The review that follows replaces it.

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

Must change before merge:

  • A TestFormatContentSkipsNilParts row for content with no parts (inline).

The three points from my earlier review are addressed on this head:

  • The new nil_before_function_call row fails with "args":null when the args == nil default in MarshalJSON (event.go#L201-L203) is removed, so that branch is now covered.
  • only_nil fails against the previous formatContent, which returned "", and passes now. I also ran formatContent on content with only nil parts, with no parts and with nil content, and each returns None.
  • The /run_live paragraph is gone from the description.

I re-ran the same checks on this head merged into current main, with no new failures.

@Trainingcqy

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing again. Addressed in a2208f5.

@karolpiotrowicz
karolpiotrowicz dismissed their stale review November 3, 2026 21:56

The one item this review raised is fixed and verified on the current head.

karolpiotrowicz
karolpiotrowicz previously approved these changes Nov 3, 2026

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

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.

@wolo-lab

wolo-lab commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@Trainingcqy , please resolve conflicts

@Trainingcqy

Copy link
Copy Markdown
Contributor Author

@wolo-lab Thanks for the reminder. Merged current main and resolved the conflicts in 519e593.

@Trainingcqy

Copy link
Copy Markdown
Contributor Author

Merged current main again and resolved the conflicts with #2192 in aaa60db. #2192 already handles Event.MarshalJSON and the event graph; both, along with their tests, are kept as they are on main.

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

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.

@Trainingcqy

Copy link
Copy Markdown
Contributor Author

@wolo-lab

Merged the latest main again in 19f3118 to resolve the conflict with #2342. Only plugin/loggingplugin/logging_plugin_test.go conflicted, because both branches added this file; the file now keeps the tests from main, followed by TestFormatContentSkipsNilParts. The changes to the other files are the same as in aaa60db.

@wolo-lab
wolo-lab merged commit dcb7ad9 into google:main Nov 19, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

adkrest: a null part in a request message makes every later turn of the session fail

4 participants