Skip to content

fix(core): avoid NPE when Agent description is missing#1164

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
svetanis:fix-agent-tool-description
Jul 22, 2026
Merged

fix(core): avoid NPE when Agent description is missing#1164
copybara-service[bot] merged 1 commit into
google:mainfrom
svetanis:fix-agent-tool-description

Conversation

@svetanis

@svetanis svetanis commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

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

  • 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_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

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

@hemasekhar-p hemasekhar-p self-assigned this Jul 16, 2026
@hemasekhar-p

Copy link
Copy Markdown
Contributor

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.

@svetanis
svetanis force-pushed the fix-agent-tool-description branch from ef12285 to 49451e5 Compare July 17, 2026 05:10
@svetanis

Copy link
Copy Markdown
Contributor Author

@hemasekhar-p ,

Thanks for the update!
Just a quick heads up - I went ahead and rebased the branch onto the latest main.

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

Thanks @svetanis for catching the bug and for the PR. Just a few small things below.

Comment thread core/src/main/java/com/google/adk/tools/AgentTool.java Outdated
Comment thread core/src/main/java/com/google/adk/tools/AgentTool.java Outdated
Comment thread core/src/test/java/com/google/adk/tools/AgentToolTest.java Outdated
@kvmilos kvmilos added the waiting on reporter Waiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale. label Jul 21, 2026
Prevents a NullPointerException in the google-genai library when an Agent is defined without a description. Includes a regression test.
@svetanis
svetanis force-pushed the fix-agent-tool-description branch from 49451e5 to 233b83b Compare July 22, 2026 00:20
@svetanis

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @kvmilos! All three points addressed:

  1. Dependency — dropped commons-lang3.StringUtils.isNotBlank for Guava's Strings.nullToEmpty.

  2. Port parity — now sends an empty description when the agent has none, matching the Python, Go, and Kotlin ports.
    This also simplified the fix to a single line with no name fallback:

    String desc = Strings.nullToEmpty(this.description());
  3. Test — fixed the no-op assertion (assertThat(declaration).isPresent();), renamed the test to declaration_withNullDescription_usesEmptyDescription, and asserted the empty description via hasValue("").

All 27 AgentToolTest tests pass.
The branch has also been rebased against the latest main.

@kvmilos kvmilos removed the waiting on reporter Waiting for reaction by reporter. Failing that, maintainers will eventually closed it as stale. label Jul 22, 2026
@kvmilos kvmilos self-assigned this Jul 22, 2026
@kvmilos kvmilos changed the title fix(core): fallback to name when Agent description is missing fix(core): avoid NPE when Agent description is missing Jul 22, 2026
@copybara-service
copybara-service Bot merged commit bcb6216 into google:main Jul 22, 2026
15 checks passed
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.

3 participants