@@ -37,6 +37,7 @@ class M3 < OpenAI::Helpers::StructuredOutput::BaseModel
37
37
38
38
U1 = OpenAI ::Helpers ::StructuredOutput ::UnionOf [ Integer , A1 ]
39
39
U2 = OpenAI ::Helpers ::StructuredOutput ::UnionOf [ :type , m2 : M2 , m3 : M3 ]
40
+ U3 = OpenAI ::Helpers ::StructuredOutput ::UnionOf [ A1 , A1 ]
40
41
41
42
def test_coerce
42
43
cases = {
@@ -159,6 +160,12 @@ class M10 < OpenAI::Helpers::StructuredOutput::BaseModel
159
160
required :b , -> { M9 }
160
161
end
161
162
163
+ class M11 < OpenAI ::Helpers ::StructuredOutput ::BaseModel
164
+ required :a , U3
165
+ required :b , A1
166
+ required :c , A1
167
+ end
168
+
162
169
def test_definition_reusing
163
170
cases = {
164
171
M6 => {
@@ -296,6 +303,27 @@ def test_definition_reusing
296
303
}
297
304
} ,
298
305
:$ref => "#/$defs/"
306
+ } ,
307
+ U3 => {
308
+ anyOf : [
309
+ { type : "array" , items : { type : "string" } } ,
310
+ { type : "array" , items : { type : "string" } }
311
+ ]
312
+ } ,
313
+ M11 => {
314
+ type : "object" ,
315
+ properties : {
316
+ a : {
317
+ anyOf : [
318
+ { type : "array" , items : { type : "string" } } ,
319
+ { type : "array" , items : { type : "string" } }
320
+ ]
321
+ } ,
322
+ b : { type : "array" , items : { type : "string" } } ,
323
+ c : { type : "array" , items : { type : "string" } }
324
+ } ,
325
+ required : %w[ a b c ] ,
326
+ additionalProperties : false
299
327
}
300
328
}
301
329
@@ -307,23 +335,23 @@ def test_definition_reusing
307
335
end
308
336
end
309
337
310
- class M11 < OpenAI ::Helpers ::StructuredOutput ::BaseModel
338
+ class M12 < OpenAI ::Helpers ::StructuredOutput ::BaseModel
311
339
required :a , OpenAI ::Helpers ::StructuredOutput ::ParsedJson
312
340
end
313
341
314
342
def test_parsed_json
315
343
assert_pattern do
316
- M11 . new ( a : { dog : "woof" } ) => { a : { dog : "woof" } }
344
+ M12 . new ( a : { dog : "woof" } ) => { a : { dog : "woof" } }
317
345
end
318
346
319
347
err = JSON ::ParserError . new ( "unexpected token at 'invalid json'" )
320
348
321
- m1 = M11 . new ( a : err )
349
+ m1 = M12 . new ( a : err )
322
350
assert_raises ( OpenAI ::Errors ::ConversionError ) do
323
351
m1 . a
324
352
end
325
353
326
- m2 = OpenAI ::Internal ::Type ::Converter . coerce ( M11 , { a : err } )
354
+ m2 = OpenAI ::Internal ::Type ::Converter . coerce ( M12 , { a : err } )
327
355
assert_raises ( OpenAI ::Errors ::ConversionError ) do
328
356
m2 . a
329
357
end
0 commit comments