Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/google/adk/tools/_gemini_schema_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ def _sanitize_schema_formats_for_gemini(
schema: dict[str, Any], preserve_null_type: bool = False
) -> dict[str, Any]:
"""Filters the schema to only include fields that are supported by JSONSchema."""
# 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

Comment on lines +148 to +155
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

supported_fields: set[str] = set(_ExtendedJSONSchema.model_fields.keys())
# Gemini rejects schemas that include `additionalProperties`, so drop it.
supported_fields.discard("additional_properties")
Expand Down