|
41 | 41 | Part, |
42 | 42 | SafetySetting, |
43 | 43 | SafetySettingDict, |
44 | | - Schema, |
45 | 44 | ThinkingConfig, |
46 | 45 | ThinkingLevel, |
47 | 46 | Tool, |
48 | 47 | ToolCodeExecution, |
49 | 48 | ToolConfig, |
50 | 49 | ToolListUnion, |
51 | | - Type, |
52 | 50 | ) |
53 | 51 | from pydantic import JsonValue |
54 | 52 | from shortuuid import uuid |
|
104 | 102 | ToolChoice, |
105 | 103 | ToolFunction, |
106 | 104 | ToolInfo, |
107 | | - ToolParam, |
108 | | - ToolParams, |
109 | 105 | ) |
110 | 106 |
|
111 | 107 | from .util import model_base_url |
@@ -318,8 +314,8 @@ async def generate( |
318 | 314 | ) |
319 | 315 | if config.response_schema is not None: |
320 | 316 | 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) |
323 | 319 | ) |
324 | 320 |
|
325 | 321 | response: GenerateContentResponse | None = None |
@@ -621,7 +617,12 @@ def _categorize_tool( |
621 | 617 | FunctionDeclaration( |
622 | 618 | name=tool.name, |
623 | 619 | 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 | + ) |
625 | 626 | if len(tool.parameters.properties) > 0 |
626 | 627 | else None, |
627 | 628 | ) |
@@ -978,93 +979,6 @@ async def extract_system_message_as_parts( |
978 | 979 | return None |
979 | 980 |
|
980 | 981 |
|
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 | | - |
1068 | 982 | def chat_tool_config(tool_choice: ToolChoice) -> ToolConfig: |
1069 | 983 | if isinstance(tool_choice, ToolFunction): |
1070 | 984 | return ToolConfig( |
|
0 commit comments