3
3
import sys
4
4
from dataclasses import dataclass
5
5
from enum import Enum , EnumMeta
6
- from typing import Literal
6
+ from typing import Literal , get_args
7
7
8
8
from PIL import Image as PILImage
9
9
from genson import SchemaBuilder
10
- from google .genai import types
11
10
from pydantic import BaseModel
12
11
13
12
from outlines import cfg , json_schema , regex
14
13
from outlines .inputs import Chat , Image
15
14
from outlines .models .gemini import GeminiTypeAdapter
15
+ from outlines .types .utils import is_dataclass
16
16
17
17
if sys .version_info >= (3 , 12 ):
18
18
from typing import TypedDict
@@ -135,19 +135,29 @@ def test_gemini_type_adapter_output_invalid(adapter):
135
135
with pytest .raises (TypeError , match = "CFG-based structured outputs" ):
136
136
adapter .format_output_type (cfg ("" ))
137
137
138
- with pytest .raises (TypeError , match = "The Gemini SDK does not accept" ):
139
- adapter .format_output_type (SchemaBuilder ())
140
-
141
- with pytest .raises (TypeError , match = "The Gemini SDK does not" ):
142
- adapter .format_output_type (json_schema ("" ))
143
-
144
138
145
139
def test_gemini_type_adapter_output_none (adapter ):
146
140
result = adapter .format_output_type (None )
147
141
assert result == {}
148
142
149
143
150
- def test_gemini_type_adapter_output_dataclass (adapter , schema ):
144
+ def test_gemini_type_adapter_output_json_schema (adapter , schema ):
145
+ result = adapter .format_output_type (json_schema (schema ))
146
+ assert isinstance (result , dict )
147
+ assert result ["response_mime_type" ] == "application/json"
148
+ assert is_dataclass (result ["response_schema" ])
149
+
150
+
151
+ def test_gemini_type_adapter_output_list_json_schema (adapter , schema ):
152
+ result = adapter .format_output_type (list [json_schema (schema )])
153
+ assert isinstance (result , dict )
154
+ assert result ["response_mime_type" ] == "application/json"
155
+ args = get_args (result ["response_schema" ])
156
+ assert len (args ) == 1
157
+ assert is_dataclass (args [0 ])
158
+
159
+
160
+ def test_gemini_type_adapter_output_dataclass (adapter ):
151
161
@dataclass
152
162
class User :
153
163
user_id : int
@@ -160,7 +170,7 @@ class User:
160
170
}
161
171
162
172
163
- def test_gemini_type_adapter_output_list_dataclass (adapter , schema ):
173
+ def test_gemini_type_adapter_output_list_dataclass (adapter ):
164
174
class User (BaseModel ):
165
175
user_id : int
166
176
name : str
@@ -172,7 +182,7 @@ class User(BaseModel):
172
182
}
173
183
174
184
175
- def test_gemini_type_adapter_output_typed_dict (adapter , schema ):
185
+ def test_gemini_type_adapter_output_typed_dict (adapter ):
176
186
class User (TypedDict ):
177
187
user_id : int
178
188
name : str
@@ -184,7 +194,7 @@ class User(TypedDict):
184
194
}
185
195
186
196
187
- def test_gemini_type_adapter_output_list_typed_dict (adapter , schema ):
197
+ def test_gemini_type_adapter_output_list_typed_dict (adapter ):
188
198
class User (BaseModel ):
189
199
user_id : int
190
200
name : str
@@ -196,7 +206,7 @@ class User(BaseModel):
196
206
}
197
207
198
208
199
- def test_gemini_type_adapter_output_pydantic (adapter , schema ):
209
+ def test_gemini_type_adapter_output_pydantic (adapter ):
200
210
class User (BaseModel ):
201
211
user_id : int
202
212
name : str
@@ -208,7 +218,7 @@ class User(BaseModel):
208
218
}
209
219
210
220
211
- def test_gemini_type_adapter_output_list_pydantic (adapter , schema ):
221
+ def test_gemini_type_adapter_output_list_pydantic (adapter ):
212
222
class User (BaseModel ):
213
223
user_id : int
214
224
name : str
@@ -220,6 +230,26 @@ class User(BaseModel):
220
230
}
221
231
222
232
233
+ def test_gemini_type_adapter_output_genson_schema_builder (adapter ):
234
+ builder = SchemaBuilder ()
235
+ builder .add_schema ({"type" : "object" , "properties" : {"foo" : {"type" : "string" }, "bar" : {"type" : "integer" }}, "required" : ["foo" ]})
236
+ result = adapter .format_output_type (builder )
237
+ assert isinstance (result , dict )
238
+ assert result ["response_mime_type" ] == "application/json"
239
+ assert is_dataclass (result ["response_schema" ])
240
+
241
+
242
+ def test_gemini_type_adapter_output_list_genson_schema_builder (adapter ):
243
+ builder = SchemaBuilder ()
244
+ builder .add_schema ({"type" : "object" , "properties" : {"foo" : {"type" : "string" }, "bar" : {"type" : "integer" }}, "required" : ["foo" ]})
245
+ result = adapter .format_output_type (list [builder ])
246
+ assert isinstance (result , dict )
247
+ assert result ["response_mime_type" ] == "application/json"
248
+ args = get_args (result ["response_schema" ])
249
+ assert len (args ) == 1
250
+ assert is_dataclass (args [0 ])
251
+
252
+
223
253
def test_gemini_type_adapter_output_enum (adapter ):
224
254
class Foo (Enum ):
225
255
Bar = "bar"
0 commit comments