Skip to content

Conversation

@paulparkinson
Copy link

fix(tools): Add type checking for schema parameter in _sanitize_schema_formats_for_gemini

Fixes #4363

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:

Problem:
The _sanitize_schema_formats_for_gemini() function in _gemini_schema_util.py assumes the schema parameter is always a dictionary and immediately calls schema.items() on line 160. However, when the function is called recursively with nested schemas, it can receive lists or primitive types, causing an AttributeError: 'list' object has no attribute 'items'.

Solution:
Added early type checking at the beginning of the function to handle non-dict schema types:

  • If schema is a list, return it as-is (pass through)
  • If schema is a primitive type, return it as-is (pass through)
  • Only proceed with the schema.items() iteration when schema is actually a dict

This approach maintains backward compatibility while preventing the AttributeError for recursive calls with non-dict types.

Testing Plan

Unit Tests:

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

The fix addresses a runtime error that would occur when processing schemas with nested non-dict types. Existing unit tests covering schema sanitization should now pass without errors for these edge cases.

Manual End-to-End (E2E) Tests:

To manually test this fix:

  1. Create a tool with a schema that contains nested list or primitive types
  2. Call _sanitize_schema_formats_for_gemini() with such a schema
  3. Verify that no AttributeError is raised
  4. Verify that the schema is processed correctly

The fix is defensive in nature - it handles edge cases that were previously causing crashes. The core logic for dict schemas remains unchanged.

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.

Additional context

The fix adds 8 lines of defensive type checking code that prevents a crash scenario when the function encounters non-dict types during recursive processing. This is a minimal, targeted fix that preserves all existing functionality while extending support to handle edge cases properly.

Code change location: src/google/adk/tools/_gemini_schema_util.py lines 148-154

…a_formats_for_gemini

Fixes google#4363

Added early type checking to handle non-dict schema types (lists and primitives)
before attempting to iterate with schema.items(). This prevents AttributeError
when the function is called recursively with non-dict types.
@google-cla
Copy link

google-cla bot commented Feb 3, 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 @paulparkinson, 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 enhances the robustness of the schema sanitization utility for Gemini by resolving a critical AttributeError. The change introduces defensive type checking to ensure that the function can correctly process nested schemas that may include lists or primitive types, thereby preventing runtime failures and improving the overall stability of the tool integration.

Highlights

  • Bug Fix: Addresses an AttributeError in _sanitize_schema_formats_for_gemini() that occurred when the schema parameter was a list or primitive type during recursive calls, instead of the expected dictionary.
  • Type Checking: Implements early type checking at the function's entry point to correctly handle list and non-dictionary primitive schema types by returning them as-is.
  • Robustness: Ensures the schema sanitization function gracefully processes diverse schema structures, preventing crashes and maintaining backward compatibility for existing dictionary-based schemas.

🧠 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/tools/_gemini_schema_util.py
    • Implemented early type checking for the schema parameter to explicitly handle list and non-dictionary primitive types.
    • Modified the function to return list and primitive schema types directly, bypassing dictionary-specific operations that would cause an AttributeError.
Activity
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.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Feb 3, 2026
@adk-bot
Copy link
Collaborator

adk-bot commented Feb 3, 2026

Response from ADK Triaging Agent

Hello @paulparkinson, thank you for creating this PR!

This PR is a bug fix. Could you please provide logs or a screenshot after the fix is applied? This information will help reviewers to review your PR more efficiently. Thanks!

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 fixes a potential AttributeError in _sanitize_schema_formats_for_gemini by adding a type check for the schema parameter. The fix is correct, but I've suggested a way to simplify the new logic. I also pointed out that the function's type hints are now inconsistent with its behavior and should be updated to maintain code clarity and correctness.

Comment on lines +148 to +155
# Handle non-dict schemas early
if isinstance(schema, list):
# Handle array schemas - pass through
return schema
elif not isinstance(schema, dict):
# Handle primitive types - pass through
return schema

Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic to handle non-dictionary schemas can be simplified. Instead of checking for list and then not dict, a single check for not isinstance(schema, dict) is sufficient as it covers lists, primitives, and any other non-dictionary types.

Additionally, with this change, the function's type hints are now incorrect. The schema parameter and the return type are both hinted as dict[str, Any], but the function now accepts and returns other types. Please update the function signature to reflect this, for example:

def _sanitize_schema_formats_for_gemini(
    schema: Any, preserve_null_type: bool = False
) -> Any:
Suggested change
# Handle non-dict schemas early
if isinstance(schema, list):
# Handle array schemas - pass through
return schema
elif not isinstance(schema, dict):
# Handle primitive types - pass through
return schema
# Handle non-dict schemas early
if not isinstance(schema, dict):
return schema

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

Labels

tools [Component] This issue is related to tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

McpToolset schema converter fails with array properties in Oracle SQLcl MC

2 participants