fix(core): avoid NPE when Agent description is missing#1164
Conversation
|
Hi @svetanis, thank you for your contribution! We appreciate you taking the time to submit this pull request. Currently this PR is under review by our team, we will keep you posted if any additional information is required. thank you. |
ef12285 to
49451e5
Compare
|
Thanks for the update! |
Prevents a NullPointerException in the google-genai library when an Agent is defined without a description. Includes a regression test.
49451e5 to
233b83b
Compare
|
Thanks for the review, @kvmilos! All three points addressed:
All 27 |
Prevents a NullPointerException in the google-genai library when an Agent is defined without a description. Includes a regression test.
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):
2. Or, if no issue exists, describe the change:
If applicable, please follow the issue templates to provide as much detail as
possible.
Problem:
Calling AgentTool.declaration() triggers a NullPointerException when an Agent is defined without an explicit description. The crash occurs because the underlying google-genai library's generated FunctionDeclaration builder uses Optional.of() for the description field, which is not null-safe.
Solution:
Coerce a null description to an empty string in AgentTool.declaration() using Guava's Strings.nullToEmpty(), so the google-genai builder always receives a non-null value. This matches the Python, Go, and Kotlin ports, which send an empty description when an agent has none.
Testing Plan
Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.
Unit Tests:
Summary of passed java test results:
Passed AgentToolTest.declaration_withNullDescription_usesEmptyDescription, which verifies that a null description no longer triggers an NPE and results in an empty description.
Manual End-to-End (E2E) Tests:
Verified by running a sample agentic workflow where the Agent class lacked a description field. Previously, this resulted in a crash during tool registration; it now initializes and executes correctly.
Checklist
Additional context
Stack Trace:
java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:220)
at java.base/java.util.Optional.of(Optional.java:113)
at com.google.genai.types.AutoValue_FunctionDeclaration$Builder.description(AutoValue_FunctionDeclaration.java:160)
at com.google.adk.tools.AgentTool.declaration(AgentTool.java:135)
Note on Root Cause:
While this PR ensures stability within adk-java, the underlying issue appears to be in the google-genai library. The generated AutoValue_FunctionDeclaration.Builder uses Optional.of() for the description field, which enforces a non-null constraint. A more robust upstream fix would involve the generator using Optional.ofNullable(). This local fix is necessary to prevent crashes in the meantime.