Skip to content

fix(core): fallback to name when Agent description is missing#1164

Open
svetanis wants to merge 1 commit intogoogle:mainfrom
svetanis:fix-agent-tool-description
Open

fix(core): fallback to name when Agent description is missing#1164
svetanis wants to merge 1 commit intogoogle:mainfrom
svetanis:fix-agent-tool-description

Conversation

@svetanis
Copy link
Copy Markdown

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):

  • Closes: N/A
  • Related: N/A

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:
Implemented defensive logic in AgentTool.java to sanitize the description. If this.description() is null or blank, the tool now falls back to using this.name(). This ensures the builder always receives a valid string and prevents the crash.

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:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Summary of passed java test results:
Passed AgentToolTest.declaration_withNullDescription_fallsBackToName which verifies that a null description no longer triggers an NPE and correctly falls back to the agent's name.

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

  • I have read the CONTRIBUTING.md document.
  • My pull request contains a single commit.
  • 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.

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.

Prevents a NullPointerException in the google-genai library when an Agent is defined without a description. Includes a regression test.
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.

1 participant