Skip to content

Commit 521bbf4

Browse files
committed
fix(tools): preserve const schemas for Gemini
1 parent 218ea76 commit 521bbf4

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/google/adk/tools/_gemini_schema_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727

2828
class _ExtendedJSONSchema(JSONSchema):
29+
const: Optional[Any] = Field(
30+
default=None,
31+
description="""Optional. Restricts the value to a single constant.""",
32+
)
2933
property_ordering: Optional[list[str]] = Field(
3034
default=None,
3135
description="""Optional. The order of the properties. Not a standard field in open api spec. Only used to support the order of the properties.""",
@@ -183,6 +187,8 @@ def _sanitize_schema_formats_for_gemini(
183187
"any_of", # 'one_of', 'all_of', 'not' to come
184188
}
185189
snake_case_schema: dict[str, Any] = {}
190+
has_const = False
191+
const_value = None
186192
dict_schema_field_names: tuple[str, ...] = (
187193
"properties",
188194
"defs",
@@ -218,9 +224,15 @@ def _sanitize_schema_formats_for_gemini(
218224
(current_type == "string" and field_value in ("date-time", "enum"))
219225
):
220226
snake_case_schema[field_name] = field_value
227+
elif field_name == "const":
228+
has_const = True
229+
const_value = field_value
221230
elif field_name in supported_fields and field_value is not None:
222231
snake_case_schema[field_name] = field_value
223232

233+
if has_const and const_value is not None:
234+
snake_case_schema["enum"] = [str(const_value)]
235+
224236
return _sanitize_schema_type(snake_case_schema, preserve_null_type)
225237

226238

tests/unittests/tools/test_gemini_schema_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ def test_to_gemini_schema_enum(self):
217217
gemini_schema = _to_gemini_schema(openapi_schema)
218218
assert gemini_schema.enum == ["a", "b", "c"]
219219

220+
def test_to_gemini_schema_const(self):
221+
openapi_schema = {"type": "string", "const": "BaseAgent"}
222+
gemini_schema = _to_gemini_schema(openapi_schema)
223+
assert gemini_schema.enum == ["BaseAgent"]
224+
220225
def test_to_gemini_schema_required(self):
221226
openapi_schema = {
222227
"type": "object",

0 commit comments

Comments
 (0)