mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
fix: Handle list values in Gemini schema sanitization
The schema sanitization utility now recursively processes list items, ensuring that properties with list values (e.g., "required") are correctly handled and not altered. Close #4363 Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 867663267
This commit is contained in:
committed by
Copybara-Service
parent
6bc70a6bab
commit
fd8a9e3962
@@ -142,9 +142,19 @@ def _dereference_schema(schema: dict[str, Any]) -> dict[str, Any]:
|
||||
|
||||
|
||||
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."""
|
||||
schema: Any, preserve_null_type: bool = False
|
||||
) -> Any:
|
||||
"""Filters schemas to only include fields supported by JSONSchema."""
|
||||
if isinstance(schema, list):
|
||||
return [
|
||||
_sanitize_schema_formats_for_gemini(
|
||||
item, preserve_null_type=preserve_null_type
|
||||
)
|
||||
for item in schema
|
||||
]
|
||||
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")
|
||||
@@ -152,7 +162,7 @@ def _sanitize_schema_formats_for_gemini(
|
||||
list_schema_field_names: set[str] = {
|
||||
"any_of", # 'one_of', 'all_of', 'not' to come
|
||||
}
|
||||
snake_case_schema = {}
|
||||
snake_case_schema: dict[str, Any] = {}
|
||||
dict_schema_field_names: tuple[str, ...] = (
|
||||
"properties",
|
||||
"defs",
|
||||
|
||||
@@ -579,6 +579,20 @@ class TestToGeminiSchema:
|
||||
"null",
|
||||
]
|
||||
|
||||
def test_sanitize_schema_formats_for_gemini_with_list_property_value(self):
|
||||
schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"required": ["sql"],
|
||||
"sql": {"type": "string"},
|
||||
},
|
||||
}
|
||||
|
||||
sanitized = _sanitize_schema_formats_for_gemini(schema)
|
||||
|
||||
assert sanitized["properties"]["required"] == ["sql"]
|
||||
assert sanitized["properties"]["sql"]["type"] == "string"
|
||||
|
||||
def test_sanitize_schema_formats_for_gemini_nullable(self):
|
||||
openapi_schema = {
|
||||
"properties": {
|
||||
|
||||
Reference in New Issue
Block a user