Skip to content

Commit 953c956

Browse files
Google: Use JSON Schema fields instead of Google Schema conversion
Use parameters_json_schema and response_schema_json_schema instead of converting to Google's proprietary Schema format via schema_from_param. This preserves full JSON Schema semantics including anyOf types, which Gemini 3+ handles correctly via the REST API but were being lost when converted to Google Schema (anyOf became TYPE_UNSPECIFIED). Removes schema_from_param() which is no longer needed. See: google/adk-python#3424
1 parent e836caf commit 953c956

1 file changed

Lines changed: 8 additions & 94 deletions

File tree

‎src/inspect_ai/model/_providers/google.py‎

Lines changed: 8 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@
4141
Part,
4242
SafetySetting,
4343
SafetySettingDict,
44-
Schema,
4544
ThinkingConfig,
4645
ThinkingLevel,
4746
Tool,
4847
ToolCodeExecution,
4948
ToolConfig,
5049
ToolListUnion,
51-
Type,
5250
)
5351
from pydantic import JsonValue
5452
from shortuuid import uuid
@@ -104,8 +102,6 @@
104102
ToolChoice,
105103
ToolFunction,
106104
ToolInfo,
107-
ToolParam,
108-
ToolParams,
109105
)
110106

111107
from .util import model_base_url
@@ -318,8 +314,8 @@ async def generate(
318314
)
319315
if config.response_schema is not None:
320316
parameters.response_mime_type = "application/json"
321-
parameters.response_schema = schema_from_param(
322-
config.response_schema.json_schema, nullable=None
317+
parameters.response_schema_json_schema = (
318+
config.response_schema.json_schema.model_dump(exclude_none=True)
323319
)
324320

325321
response: GenerateContentResponse | None = None
@@ -621,7 +617,12 @@ def _categorize_tool(
621617
FunctionDeclaration(
622618
name=tool.name,
623619
description=tool.description,
624-
parameters=schema_from_param(tool.parameters)
620+
# Use parameters_json_schema to preserve full JSON Schema
621+
# including anyOf types, which Gemini 3+ handles correctly
622+
# but the Schema format doesn't support
623+
parameters_json_schema=tool.parameters.model_dump(
624+
exclude_none=True
625+
)
625626
if len(tool.parameters.properties) > 0
626627
else None,
627628
)
@@ -978,93 +979,6 @@ async def extract_system_message_as_parts(
978979
return None
979980

980981

981-
# https://ai.google.dev/gemini-api/tutorials/extract_structured_data#define_the_schema
982-
def schema_from_param(
983-
param: ToolParam | ToolParams,
984-
nullable: bool | None = False,
985-
description: str | None = None,
986-
) -> Schema:
987-
if isinstance(param, ToolParams):
988-
param = ToolParam(
989-
type=param.type, properties=param.properties, required=param.required
990-
)
991-
992-
# use fallback description if the param doesn't have its own
993-
param_description = param.description or description
994-
995-
if param.type == "number":
996-
return Schema(
997-
type=Type.NUMBER, description=param_description, nullable=nullable
998-
)
999-
elif param.type == "integer":
1000-
return Schema(
1001-
type=Type.INTEGER, description=param_description, nullable=nullable
1002-
)
1003-
elif param.type == "boolean":
1004-
return Schema(
1005-
type=Type.BOOLEAN, description=param_description, nullable=nullable
1006-
)
1007-
elif param.type == "string":
1008-
if param.format == "date-time":
1009-
return Schema(
1010-
type=Type.STRING,
1011-
description=param_description,
1012-
format="^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$",
1013-
nullable=nullable,
1014-
)
1015-
elif param.format == "date":
1016-
return Schema(
1017-
type=Type.STRING,
1018-
description=param_description,
1019-
format="^[0-9]{4}-[0-9]{2}-[0-9]{2}$",
1020-
nullable=nullable,
1021-
)
1022-
elif param.format == "time":
1023-
return Schema(
1024-
type=Type.STRING,
1025-
description=param_description,
1026-
format="^[0-9]{2}:[0-9]{2}:[0-9]{2}$",
1027-
nullable=nullable,
1028-
)
1029-
return Schema(
1030-
type=Type.STRING, description=param_description, nullable=nullable
1031-
)
1032-
elif param.type == "array":
1033-
return Schema(
1034-
type=Type.ARRAY,
1035-
description=param_description,
1036-
items=schema_from_param(param.items) if param.items else None,
1037-
nullable=nullable,
1038-
)
1039-
elif param.type == "object":
1040-
return Schema(
1041-
type=Type.OBJECT,
1042-
description=param_description,
1043-
properties={k: schema_from_param(v) for k, v in param.properties.items()}
1044-
if param.properties is not None
1045-
else {},
1046-
required=param.required,
1047-
nullable=nullable,
1048-
)
1049-
# convert unions to optional params if the second type is 'null'
1050-
elif param.anyOf:
1051-
if len(param.anyOf) == 2 and param.anyOf[1].type == "null":
1052-
return schema_from_param(
1053-
param.anyOf[0], nullable=True, description=param_description
1054-
)
1055-
else:
1056-
return Schema(type=Type.TYPE_UNSPECIFIED, description=param_description)
1057-
elif param.enum:
1058-
return Schema(
1059-
type=Type.STRING,
1060-
format="enum",
1061-
enum=param.enum,
1062-
description=param_description,
1063-
)
1064-
else:
1065-
return Schema(type=Type.TYPE_UNSPECIFIED, description=param_description)
1066-
1067-
1068982
def chat_tool_config(tool_choice: ToolChoice) -> ToolConfig:
1069983
if isinstance(tool_choice, ToolFunction):
1070984
return ToolConfig(

0 commit comments

Comments
 (0)