forked from livekit/agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_schema_gemini.py
More file actions
186 lines (163 loc) Β· 5.67 KB
/
Copy pathtest_schema_gemini.py
File metadata and controls
186 lines (163 loc) Β· 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import datetime
from typing import Optional
import pytest
from google.genai import types
from pydantic import BaseModel, Field
from livekit.plugins.google import utils
# Gemini Schema Tests
# Test for inlining $ref definitions
async def test_json_def_replaced():
class Location(BaseModel):
lat: float
lng: float = 1.1
class Locations(BaseModel):
locations: list[Location]
json_schema = Locations.model_json_schema()
# Original schema with $defs as produced by Pydantic.
expected_schema = {
"$defs": {
"Location": {
"properties": {
"lat": {"title": "Lat", "type": "number"},
"lng": {"default": 1.1, "title": "Lng", "type": "number"},
},
"required": ["lat"],
"title": "Location",
"type": "object",
}
},
"properties": {
"locations": {
"items": {"$ref": "#/$defs/Location"},
"title": "Locations",
"type": "array",
}
},
"required": ["locations"],
"title": "Locations",
"type": "object",
}
assert json_schema == expected_schema
gemini_schema = utils._GeminiJsonSchema(json_schema).simplify()
expected_gemini_schema = {
"properties": {
"locations": {
"items": {
"properties": {
"lat": {"type": types.Type.NUMBER},
"lng": {"type": types.Type.NUMBER},
},
"required": ["lat"],
"type": types.Type.OBJECT,
},
"type": types.Type.ARRAY,
}
},
"required": ["locations"],
"type": types.Type.OBJECT,
}
assert gemini_schema == expected_gemini_schema
# Test for handling anyOf (optional field)
async def test_json_def_replaced_any_of():
class Location(BaseModel):
lat: float
lng: float
class Locations(BaseModel):
op_location: Optional[Location] = None
json_schema = Locations.model_json_schema()
gemini_schema = utils._GeminiJsonSchema(json_schema).simplify()
# The anyOf containing the Location ref and {"type": "null"} is merged,
# so op_location becomes the inlined Location with "nullable": True.
expected_gemini_schema = {
"properties": {
"op_location": {
"properties": {
"lat": {"type": types.Type.NUMBER},
"lng": {"type": types.Type.NUMBER},
},
"required": ["lat", "lng"],
"type": types.Type.OBJECT,
"nullable": True,
}
},
"type": types.Type.OBJECT,
}
assert gemini_schema == expected_gemini_schema
# Test for recursive $ref β should raise ValueError
async def test_json_def_recursive():
class Location(BaseModel):
lat: float
lng: float
nested_locations: list["Location"]
Location.model_rebuild()
json_schema = Location.model_json_schema()
expected_schema = {
"$defs": {
"Location": {
"properties": {
"lat": {"title": "Lat", "type": "number"},
"lng": {"title": "Lng", "type": "number"},
"nested_locations": {
"items": {"$ref": "#/$defs/Location"},
"title": "Nested Locations",
"type": "array",
},
},
"required": ["lat", "lng", "nested_locations"],
"title": "Location",
"type": "object",
}
},
"$ref": "#/$defs/Location",
}
assert json_schema == expected_schema
with pytest.raises(
ValueError,
match=r"Recursive `\$ref`s in JSON Schema are not supported by Gemini",
):
utils._GeminiJsonSchema(json_schema).simplify()
# Test for preserving format, and description on string fields
async def test_json_def_date():
class FormattedStringFields(BaseModel):
d: datetime.date
dt: datetime.datetime
t: datetime.time = Field(description="")
td: datetime.timedelta = Field(description="my timedelta")
json_schema = FormattedStringFields.model_json_schema()
expected_schema = {
"properties": {
"d": {"format": "date", "title": "D", "type": "string"},
"dt": {"format": "date-time", "title": "Dt", "type": "string"},
"t": {"format": "time", "title": "T", "type": "string", "description": ""},
"td": {
"format": "duration",
"title": "Td",
"type": "string",
"description": "my timedelta",
},
},
"required": ["d", "dt", "t", "td"],
"title": "FormattedStringFields",
"type": "object",
}
assert json_schema == expected_schema
gemini_schema = utils._GeminiJsonSchema(json_schema).simplify()
expected_gemini_schema = {
"properties": {
"d": {"format": "date", "type": types.Type.STRING},
"dt": {"format": "date-time", "type": types.Type.STRING},
"t": {
"format": "time",
"type": types.Type.STRING,
"description": "",
},
"td": {
"format": "duration",
"type": types.Type.STRING,
"description": "my timedelta",
},
},
"required": ["d", "dt", "t", "td"],
"type": types.Type.OBJECT,
}
assert gemini_schema == expected_gemini_schema