Skip to content

fix: recurse into object properties in _update_type_string for Anthropic adapter#4469

Open
encounter wants to merge 1 commit intogoogle:mainfrom
encounter:fix/anthropic-nested-schema-type-conversion
Open

fix: recurse into object properties in _update_type_string for Anthropic adapter#4469
encounter wants to merge 1 commit intogoogle:mainfrom
encounter:fix/anthropic-nested-schema-type-conversion

Conversation

@encounter
Copy link

@encounter encounter commented Feb 12, 2026

Summary

_update_type_string lowercases Gemini-style type strings (e.g. STRINGstring) for Anthropic API compatibility. It already recursed into items (arrays) and items.properties (arrays of objects), but not into top-level properties of object types. This caused tool schemas with nested object parameters (e.g. Pydantic BaseModel subclasses used directly as function parameters) to produce invalid JSON Schema with uppercase type strings, resulting in Anthropic API 400 errors:

{'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'tools.1.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12 (https://json-schema.org/draft/2020-12). Learn more about tool use at https://docs.claude.com/en/docs/tool-use.'}, 'request_id': 'x'}

The fix recurses into properties at every level, which also subsumes the existing items.properties handling.

Reproduction

Use a Pydantic model with nested objects as a FunctionTool parameter with the Claude model class:

class Address(BaseModel):
    city: str = ""
    state: str = ""

class Profile(BaseModel):
    name: str = ""
    address: Address = Field(default_factory=Address)

async def update_profile(profile: Profile, tool_context: ToolContext) -> dict:
    ...

root_agent = LlmAgent(
    model=Claude(model="claude-opus-4-5@20251101"),
    tools=[FunctionTool(update_profile)],
    ...
)

Before this fix, address.city and address.state retain "STRING" (uppercase) in the schema sent to Anthropic, which rejects it.

Test plan

  • Added function_with_nested_object_parameter test case to test_function_declaration_to_tool_param — verifies a 3-level deep object schema (parameter → profile → address → city/state) is fully lowercased
  • Confirmed the new test fails without the fix and passes with it
  • All 18 existing tests in test_anthropic_llm.py continue to pass

…pic adapter

`_update_type_string` lowercases Gemini-style type strings (e.g.
"STRING" -> "string") for Anthropic API compatibility. It already
recursed into `items` (arrays) and `items.properties` (arrays of
objects), but not into top-level `properties` of object types. This
caused tool schemas with nested object parameters (e.g. Pydantic
models) to produce invalid JSON Schema with uppercase type strings,
resulting in Anthropic API 400 errors.

Recurse into `properties` at every level, which also subsumes the
existing `items.properties` handling.
@google-cla
Copy link

google-cla bot commented Feb 12, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @encounter, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where tool schemas with deeply nested object parameters, particularly when using Pydantic models, caused invalid_request_error from the Anthropic API due to uppercase type strings. The core change enhances the _update_type_string utility to recursively lowercase all type strings found within object properties, ensuring proper JSON Schema validation and compatibility with the Anthropic API for complex function declarations.

Highlights

  • Recursive Property Traversal: Modified _update_type_string to recursively process properties at all levels, ensuring type strings within nested objects are correctly lowercased for Anthropic API compatibility.
  • Simplified Logic: The change consolidates the handling of object properties, making the _update_type_string function more robust and removing the need for a separate items.properties check.
  • New Test Case: Introduced a new test, function_with_nested_object_parameter, to validate the fix for deeply nested object schemas (e.g., parameter -> profile -> address -> city/state).

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/google/adk/models/anthropic_llm.py
    • Refactored _update_type_string to universally recurse into properties for type string lowercasing.
    • Removed specific items.properties recursion as it is now covered by the more general properties recursion.
  • tests/unittests/models/test_anthropic_llm.py
    • Added function_with_nested_object_parameter test case to validate the correct transformation of deeply nested object schemas.
Activity
  • A new test case, function_with_nested_object_parameter, was added to test_function_declaration_to_tool_param.
  • The author confirmed that this new test case failed before the fix and passed after applying it.
  • All 18 existing tests in test_anthropic_llm.py continued to pass, indicating no regressions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses a bug in the Anthropic adapter where nested object parameters in tool schemas were not being processed correctly. The fix in _update_type_string correctly implements recursion for properties at all levels, which not only solves the issue but also simplifies and generalizes the existing logic. The addition of the function_with_nested_object_parameter test case is excellent, as it thoroughly validates the fix for deeply nested schemas. The changes are clean, correct, and improve the overall robustness of the integration. Great work!

@adk-bot adk-bot added the models [Component] Issues related to model support label Feb 12, 2026
@ryanaiagent ryanaiagent self-assigned this Feb 13, 2026
@ryanaiagent
Copy link
Collaborator

Hi @encounter, Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

@ryanaiagent ryanaiagent added the needs review [Status] The PR/issue is awaiting review from the maintainer label Feb 13, 2026
@ryanaiagent
Copy link
Collaborator

Hi @wyf7107 , can you please review this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

models [Component] Issues related to model support needs review [Status] The PR/issue is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants