Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/google/adk/tools/_gemini_schema_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@


class _ExtendedJSONSchema(JSONSchema):
const: Optional[Any] = Field(
default=None,
description="""Optional. Restricts the value to a single constant.""",
)
property_ordering: Optional[list[str]] = Field(
default=None,
description="""Optional. The order of the properties. Not a standard field in open api spec. Only used to support the order of the properties.""",
Expand Down Expand Up @@ -183,6 +187,8 @@ def _sanitize_schema_formats_for_gemini(
"any_of", # 'one_of', 'all_of', 'not' to come
}
snake_case_schema: dict[str, Any] = {}
has_const = False
const_value = None
dict_schema_field_names: tuple[str, ...] = (
"properties",
"defs",
Expand Down Expand Up @@ -218,9 +224,15 @@ def _sanitize_schema_formats_for_gemini(
(current_type == "string" and field_value in ("date-time", "enum"))
):
snake_case_schema[field_name] = field_value
elif field_name == "const":
has_const = True
const_value = field_value
elif field_name in supported_fields and field_value is not None:
snake_case_schema[field_name] = field_value

if has_const and const_value is not None:
snake_case_schema["enum"] = [str(const_value)]

return _sanitize_schema_type(snake_case_schema, preserve_null_type)


Expand Down
5 changes: 5 additions & 0 deletions tests/unittests/tools/test_gemini_schema_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ def test_to_gemini_schema_enum(self):
gemini_schema = _to_gemini_schema(openapi_schema)
assert gemini_schema.enum == ["a", "b", "c"]

def test_to_gemini_schema_const(self):
openapi_schema = {"type": "string", "const": "BaseAgent"}
gemini_schema = _to_gemini_schema(openapi_schema)
assert gemini_schema.enum == ["BaseAgent"]

def test_to_gemini_schema_required(self):
openapi_schema = {
"type": "object",
Expand Down