Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(tools): Handle JSON Schema boolean schemas in Gemini schema conversion#4531

Open
onematchfox wants to merge 1 commit intogoogle:maingoogle/adk-python:mainfrom
onematchfox:fix-gemini-schema-boolonematchfox/adk-python:fix-gemini-schema-boolCopy head branch name to clipboard
Open

fix(tools): Handle JSON Schema boolean schemas in Gemini schema conversion#4531
onematchfox wants to merge 1 commit intogoogle:maingoogle/adk-python:mainfrom
onematchfox:fix-gemini-schema-boolonematchfox/adk-python:fix-gemini-schema-boolCopy head branch name to clipboard

Conversation

@onematchfox
Copy link

Problem:
JSON Schema allows true and false as valid boolean schemas, where true accepts any value and false rejects all values. Some MCP servers use this pattern for unconstrained fields. E.g. mcp-grafana - see grafana-mcp-list-tools.json which was obtained from tools/list

The schema sanitizer previously passed booleans through unchanged, causing a Pydantic ValidationError when _ExtendedJSONSchema tried to validate them as schema objects.

1 validation error for _ExtendedJSONSchema
properties.data.items.properties.model
  Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=True, input_type=bool]
    For further information visit https://errors.pydantic.dev/2.12/v/model_attributes_type
Traceback (most recent call last):
  ...
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 561, in run_async
    async for event in agen:
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 549, in _run_with_trace
    async for event in agen:
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 778, in _exec_with_plugin
    async for event in agen:
    ...<64 lines>...
        yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 538, in execute
    async for event in agen:
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 294, in run_async
    async for event in agen:
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/agents/llm_agent.py", line 468, in _run_async_impl
    async for event in agen:
    ...<5 lines>...
        should_pause = True
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 427, in run_async
    async for event in agen:
      last_event = event
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 446, in _run_one_step_async
    async for event in agen:
      yield event
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 578, in _preprocess_async
    await tool.process_llm_request(
        tool_context=tool_context, llm_request=llm_request
    )
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/base_tool.py", line 129, in process_llm_request
    llm_request.append_tools([self])
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/models/llm_request.py", line 255, in append_tools
    declaration = tool._get_declaration()
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/mcp_tool/mcp_tool.py", line 200, in _get_declaration
    parameters = _to_gemini_schema(input_schema)
  File "/.foo/.venv/lib/python3.13/site-packages/google/adk/tools/_gemini_schema_util.py", line 218, in _to_gemini_schema
    json_schema=_ExtendedJSONSchema.model_validate(sanitized_schema),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "/.foo/.venv/lib/python3.13/site-packages/pydantic/main.py", line 716, in model_validate
    return cls.__pydantic_validator__.validate_python(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        obj,
        ^^^^
    ...<5 lines>...
        by_name=by_name,
        ^^^^^^^^^^^^^^^^
    )
    ^
pydantic_core._pydantic_core.ValidationError: 1 validation error for _ExtendedJSONSchema
properties.data.items.properties.model
  Input should be a valid dictionary or object to extract fields from [type=model_attributes_type, input_value=True, input_type=bool]
    For further information visit https://errors.pydantic.dev/2.12/v/model_attributes_type

Solution:

Convert boolean schemas to {"type": "object"} as the closest approximation available in Gemini's schema model.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.
============================= test session starts ==============================
platform linux -- Python 3.13.5, pytest-9.0.2, pluggy-1.6.0
plugins: asyncio-1.3.0, xdist-3.8.0, mock-3.15.1, anyio-4.12.1
asyncio: mode=Mode.AUTO

tests/unittests/tools/test_gemini_schema_util.py: 65 passed in 1.48s
=============================== 65 passed in 1.48s ==============================

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.

…rsion

JSON Schema allows `true` and `false` as valid boolean schemas, where
`true` accepts any value and `false` rejects all values. Some MCP servers
use this pattern for unconstrained fields. The schema sanitizer previously
passed booleans through unchanged, causing a Pydantic ValidationError when
`_ExtendedJSONSchema` tried to validate them as schema objects.

Convert boolean schemas to `{"type": "object"}` as the closest
approximation available in Gemini's schema model.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Signed-off-by: Brian Fox <878612+onematchfox@users.noreply.github.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @onematchfox, 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 a Pydantic ValidationError that occurred when the Gemini schema conversion process encountered boolean JSON schemas (e.g., true or false). These boolean schemas, which are valid in JSON Schema for representing unconstrained or rejected values, were not correctly handled, leading to crashes. The solution implemented converts these boolean schemas into a generic {"type": "object"} representation, providing a safe fallback that prevents validation errors and ensures compatibility with external systems that utilize this JSON Schema pattern.

Highlights

  • Problem Addressed: Resolved a Pydantic ValidationError that occurred when the Gemini schema conversion process encountered boolean JSON schemas (true or false), which are valid in JSON Schema but caused crashes due to incompatibility.
  • Solution Implemented: Boolean JSON schemas are now converted to a generic {"type": "object"} representation during the Gemini schema conversion, providing a safe fallback.
  • Impact: This change prevents validation errors and ensures compatibility with external systems (like some MCP servers) that utilize boolean schemas for unconstrained fields.
  • Testing: New unit tests were added to specifically cover the correct handling of both true and false boolean schemas, including a regression test for schemas within array item properties.

🧠 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
    • Introduced logic within the _sanitize_schema_formats_for_gemini function to detect and handle boolean JSON schemas (true or false).
    • Mapped boolean schemas to {"type": "object"} to ensure compatibility with Gemini's schema model and prevent Pydantic validation errors.
  • tests/unittests/tools/test_gemini_schema_util.py
    • Added test_to_gemini_schema_boolean_true_property to verify correct handling of true boolean schemas.
    • Added test_to_gemini_schema_boolean_false_property to confirm that false boolean schemas are processed without raising errors.
    • Added test_to_gemini_schema_boolean_true_in_array_items_properties as a regression test for boolean true schemas within array item properties.
Activity
  • The author identified a Pydantic ValidationError when processing boolean JSON schemas.
  • A solution was implemented to convert boolean schemas to {"type": "object"}.
  • Unit tests were added and passed locally to validate the fix for both true and false boolean schemas, including a regression test for array item properties.
  • The author performed a self-review and commented the code.
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 18, 2026
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

The pull request correctly addresses the Pydantic ValidationError encountered when processing JSON schemas that use boolean values (e.g., "field": true), which is a valid pattern in JSON Schema (Draft 7+) often used by MCP servers to indicate unconstrained fields. By converting these boolean schemas to an unconstrained object schema ({"type": "object"}), the utility now handles these cases gracefully instead of crashing. The implementation is correctly placed within the recursive sanitization logic and is supported by comprehensive unit tests covering various nesting scenarios including properties and array items.

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.

2 participants

Comments

Close sidebar
Morty Proxy This is a proxified and sanitized view of the page, visit original site.