Skip to content

Commit a0e8b24

Browse files
committed
No duplicate schemas
1 parent 0b55d53 commit a0e8b24

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def _build_processor(
385385

386386
return UnionOutputProcessor(outputs=outputs, strict=strict, name=name, description=description)
387387

388-
def build_json_schema(self) -> JsonSchema:
388+
def build_json_schema(self) -> JsonSchema: # noqa: C901
389389
# allow any output with {'type': 'string'} if no constraints
390390
if not any([self.allows_deferred_tools, self.allows_image, self.object_def, self.toolset]):
391391
return TypeAdapter(str).json_schema()
@@ -407,19 +407,23 @@ def build_json_schema(self) -> JsonSchema:
407407
json_schema = tool_processor.object_def.json_schema
408408
if k := tool_processor.outer_typed_dict_key:
409409
json_schema = json_schema['properties'][k]
410-
json_schemas.append(json_schema)
410+
if json_schema not in json_schemas:
411+
json_schemas.append(json_schema)
411412

412413
elif self.allows_text:
413414
json_schema = TypeAdapter(str).json_schema()
414415
json_schemas.append(json_schema)
415416

416417
if self.allows_deferred_tools:
417418
json_schema = TypeAdapter(DeferredToolRequests).json_schema(mode='serialization')
418-
json_schemas.append(json_schema)
419+
if json_schema not in json_schemas:
420+
json_schemas.append(json_schema)
421+
419422
if self.allows_image:
420423
json_schema = TypeAdapter(_messages.BinaryImage).json_schema()
421424
json_schema = {k: v for k, v in json_schema['properties'].items() if k in ['data', 'media_type']}
422-
json_schemas.append(json_schema)
425+
if json_schema not in json_schemas:
426+
json_schemas.append(json_schema)
423427

424428
if len(json_schemas) == 1:
425429
return json_schemas[0]

tests/test_agent_output_schemas.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ async def test_tool_output_json_schema():
101101
'test',
102102
output_type=[ToolOutput(bool), ToolOutput(bool), ToolOutput(bool)],
103103
)
104-
assert agent.output_json_schema() == snapshot(
105-
{'anyOf': [{'type': 'boolean'}, {'type': 'boolean'}, {'type': 'boolean'}]}
106-
)
104+
assert agent.output_json_schema() == snapshot({'type': 'boolean'})
107105

108106

109107
async def test_native_output_json_schema():

0 commit comments

Comments
 (0)