Skip to content

Commit f7947f4

Browse files
authored
google: add more information about schema on union (#2426)
1 parent 544ff88 commit f7947f4

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

pydantic_ai_slim/pydantic_ai/profiles/_json_schema.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ def _handle_array(self, schema: JsonSchema) -> JsonSchema:
137137
return schema
138138

139139
def _handle_union(self, schema: JsonSchema, union_kind: Literal['anyOf', 'oneOf']) -> JsonSchema:
140-
members = schema.get(union_kind)
141-
if not members:
140+
try:
141+
members = schema.pop(union_kind)
142+
except KeyError:
142143
return schema
143144

144145
handled = [self._handle(member) for member in members]
@@ -149,7 +150,7 @@ def _handle_union(self, schema: JsonSchema, union_kind: Literal['anyOf', 'oneOf'
149150

150151
if len(handled) == 1:
151152
# In this case, no need to retain the union
152-
return handled[0]
153+
return handled[0] | schema
153154

154155
# If we have keys besides the union kind (such as title or discriminator), keep them without modifications
155156
schema = schema.copy()

tests/models/test_gemini.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,24 +360,25 @@ class QueryDetails(BaseModel):
360360

361361
# This tests that the enum values are properly converted to strings for Gemini
362362
assert m._get_tools(mrp) == snapshot(
363-
_GeminiTools(
364-
function_declarations=[
365-
_GeminiFunction(
366-
name='result',
367-
description='This is the tool for the final Result',
368-
parameters={
363+
{
364+
'function_declarations': [
365+
{
366+
'name': 'result',
367+
'description': 'This is the tool for the final Result',
368+
'parameters': {
369369
'properties': {
370370
'progress': {
371371
'items': {'enum': ['100', '80', '60', '40', '20'], 'type': 'string'},
372372
'type': 'array',
373373
'nullable': True,
374+
'default': None,
374375
}
375376
},
376377
'type': 'object',
377378
},
378-
)
379+
}
379380
]
380-
)
381+
}
381382
)
382383

383384

@@ -406,12 +407,12 @@ class Locations(BaseModel):
406407
)
407408
mrp = m.customize_request_parameters(mrp)
408409
assert m._get_tools(mrp) == snapshot(
409-
_GeminiTools(
410-
function_declarations=[
411-
_GeminiFunction(
412-
name='result',
413-
description='This is the tool for the final Result',
414-
parameters={
410+
{
411+
'function_declarations': [
412+
{
413+
'name': 'result',
414+
'description': 'This is the tool for the final Result',
415+
'parameters': {
415416
'properties': {
416417
'op_location': {
417418
'properties': {
@@ -421,13 +422,14 @@ class Locations(BaseModel):
421422
'required': ['lat', 'lng'],
422423
'nullable': True,
423424
'type': 'object',
425+
'default': None,
424426
}
425427
},
426428
'type': 'object',
427429
},
428-
)
430+
}
429431
]
430-
)
432+
}
431433
)
432434

433435

0 commit comments

Comments
 (0)