Skip to content

Commit f561373

Browse files
committed
Only include the data and media_type keys for BinaryImage
1 parent d0e1e9c commit f561373

File tree

2 files changed

+94
-182
lines changed

2 files changed

+94
-182
lines changed

pydantic_ai_slim/pydantic_ai/_output.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,13 @@ def build_json_schema(self) -> JsonSchema:
413413
json_schema = TypeAdapter(str).json_schema()
414414
json_schemas.append(json_schema)
415415

416-
special_output_types: list[type] = []
417416
if self.allows_deferred_tools:
418-
special_output_types.append(DeferredToolRequests)
417+
json_schema = TypeAdapter(DeferredToolRequests).json_schema(mode='serialization')
418+
json_schemas.append(json_schema)
419419
if self.allows_image:
420-
special_output_types.append(_messages.BinaryImage)
421-
for output_type in special_output_types:
422-
output_type_json_schema = TypeAdapter(output_type).json_schema(mode='serialization')
423-
json_schemas.append(output_type_json_schema)
420+
json_schema = TypeAdapter(_messages.BinaryImage).json_schema()
421+
json_schema = {k:v for k,v in json_schema['properties'].items() if k in ['data', 'media_type']}
422+
json_schemas.append(json_schema)
424423

425424
if len(json_schemas) == 1:
426425
return json_schemas[0]

tests/test_agent_output_schemas.py

Lines changed: 89 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -139,61 +139,32 @@ async def test_image_output_json_schema():
139139
agent = Agent('test', output_type=BinaryImage)
140140
assert agent.output_json_schema() == snapshot(
141141
{
142-
'properties': {
143-
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
144-
'media_type': {
145-
'anyOf': [
146-
{
147-
'enum': ['audio/wav', 'audio/mpeg', 'audio/ogg', 'audio/flac', 'audio/aiff', 'audio/aac'],
148-
'type': 'string',
149-
},
150-
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
151-
{
152-
'enum': [
153-
'application/pdf',
154-
'text/plain',
155-
'text/csv',
156-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
157-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
158-
'text/html',
159-
'text/markdown',
160-
'application/msword',
161-
'application/vnd.ms-excel',
162-
],
163-
'type': 'string',
164-
},
165-
{'type': 'string'},
166-
],
167-
'title': 'Media Type',
168-
},
169-
'vendor_metadata': {
170-
'anyOf': [{'additionalProperties': True, 'type': 'object'}, {'type': 'null'}],
171-
'default': None,
172-
'title': 'Vendor Metadata',
173-
},
174-
'identifier': {
175-
'description': """\
176-
Identifier for the binary content, such as a unique ID.
177-
178-
This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument,
179-
and the tool can look up the file in question by iterating over the message history and finding the matching `BinaryContent`.
180-
181-
This identifier is only automatically passed to the model when the `BinaryContent` is returned by a tool.
182-
If you're passing the `BinaryContent` as a user message, it's up to you to include a separate text part with the identifier,
183-
e.g. "This is file <identifier>:" preceding the `BinaryContent`.
184-
185-
It's also included in inline-text delimiters for providers that require inlining text documents, so the model can
186-
distinguish multiple files.\
187-
""",
188-
'readOnly': True,
189-
'title': 'Identifier',
190-
'type': 'string',
191-
},
192-
'kind': {'const': 'binary', 'default': 'binary', 'title': 'Kind', 'type': 'string'},
142+
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
143+
'media_type': {
144+
'anyOf': [
145+
{
146+
'enum': ['audio/wav', 'audio/mpeg', 'audio/ogg', 'audio/flac', 'audio/aiff', 'audio/aac'],
147+
'type': 'string',
148+
},
149+
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
150+
{
151+
'enum': [
152+
'application/pdf',
153+
'text/plain',
154+
'text/csv',
155+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
156+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
157+
'text/html',
158+
'text/markdown',
159+
'application/msword',
160+
'application/vnd.ms-excel',
161+
],
162+
'type': 'string',
163+
},
164+
{'type': 'string'},
165+
],
166+
'title': 'Media Type',
193167
},
194-
'required': ['data', 'media_type', 'identifier'],
195-
'title': 'BinaryImage',
196-
'type': 'object',
197168
}
198169
)
199170

@@ -205,68 +176,39 @@ async def test_image_output_json_schema():
205176
{'type': 'string'},
206177
{'type': 'boolean'},
207178
{
208-
'properties': {
209-
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
210-
'media_type': {
211-
'anyOf': [
212-
{
213-
'enum': [
214-
'audio/wav',
215-
'audio/mpeg',
216-
'audio/ogg',
217-
'audio/flac',
218-
'audio/aiff',
219-
'audio/aac',
220-
],
221-
'type': 'string',
222-
},
223-
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
224-
{
225-
'enum': [
226-
'application/pdf',
227-
'text/plain',
228-
'text/csv',
229-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
230-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
231-
'text/html',
232-
'text/markdown',
233-
'application/msword',
234-
'application/vnd.ms-excel',
235-
],
236-
'type': 'string',
237-
},
238-
{'type': 'string'},
239-
],
240-
'title': 'Media Type',
241-
},
242-
'vendor_metadata': {
243-
'anyOf': [{'additionalProperties': True, 'type': 'object'}, {'type': 'null'}],
244-
'default': None,
245-
'title': 'Vendor Metadata',
246-
},
247-
'kind': {'const': 'binary', 'default': 'binary', 'title': 'Kind', 'type': 'string'},
248-
'identifier': {
249-
'description': """\
250-
Identifier for the binary content, such as a unique ID.
251-
252-
This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument,
253-
and the tool can look up the file in question by iterating over the message history and finding the matching `BinaryContent`.
254-
255-
This identifier is only automatically passed to the model when the `BinaryContent` is returned by a tool.
256-
If you're passing the `BinaryContent` as a user message, it's up to you to include a separate text part with the identifier,
257-
e.g. "This is file <identifier>:" preceding the `BinaryContent`.
258-
259-
It's also included in inline-text delimiters for providers that require inlining text documents, so the model can
260-
distinguish multiple files.\
261-
""",
262-
'readOnly': True,
263-
'title': 'Identifier',
264-
'type': 'string',
265-
},
179+
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
180+
'media_type': {
181+
'anyOf': [
182+
{
183+
'enum': [
184+
'audio/wav',
185+
'audio/mpeg',
186+
'audio/ogg',
187+
'audio/flac',
188+
'audio/aiff',
189+
'audio/aac',
190+
],
191+
'type': 'string',
192+
},
193+
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
194+
{
195+
'enum': [
196+
'application/pdf',
197+
'text/plain',
198+
'text/csv',
199+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
200+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
201+
'text/html',
202+
'text/markdown',
203+
'application/msword',
204+
'application/vnd.ms-excel',
205+
],
206+
'type': 'string',
207+
},
208+
{'type': 'string'},
209+
],
210+
'title': 'Media Type',
266211
},
267-
'required': ['data', 'media_type', 'identifier'],
268-
'title': 'BinaryImage',
269-
'type': 'object',
270212
},
271213
]
272214
}
@@ -355,68 +297,39 @@ async def test_deferred_output_json_schema():
355297
'type': 'object',
356298
},
357299
{
358-
'properties': {
359-
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
360-
'media_type': {
361-
'anyOf': [
362-
{
363-
'enum': [
364-
'audio/wav',
365-
'audio/mpeg',
366-
'audio/ogg',
367-
'audio/flac',
368-
'audio/aiff',
369-
'audio/aac',
370-
],
371-
'type': 'string',
372-
},
373-
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
374-
{
375-
'enum': [
376-
'application/pdf',
377-
'text/plain',
378-
'text/csv',
379-
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
380-
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
381-
'text/html',
382-
'text/markdown',
383-
'application/msword',
384-
'application/vnd.ms-excel',
385-
],
386-
'type': 'string',
387-
},
388-
{'type': 'string'},
389-
],
390-
'title': 'Media Type',
391-
},
392-
'vendor_metadata': {
393-
'anyOf': [{'additionalProperties': True, 'type': 'object'}, {'type': 'null'}],
394-
'default': None,
395-
'title': 'Vendor Metadata',
396-
},
397-
'kind': {'const': 'binary', 'default': 'binary', 'title': 'Kind', 'type': 'string'},
398-
'identifier': {
399-
'description': """\
400-
Identifier for the binary content, such as a unique ID.
401-
402-
This identifier can be provided to the model in a message to allow it to refer to this file in a tool call argument,
403-
and the tool can look up the file in question by iterating over the message history and finding the matching `BinaryContent`.
404-
405-
This identifier is only automatically passed to the model when the `BinaryContent` is returned by a tool.
406-
If you're passing the `BinaryContent` as a user message, it's up to you to include a separate text part with the identifier,
407-
e.g. "This is file <identifier>:" preceding the `BinaryContent`.
408-
409-
It's also included in inline-text delimiters for providers that require inlining text documents, so the model can
410-
distinguish multiple files.\
411-
""",
412-
'readOnly': True,
413-
'title': 'Identifier',
414-
'type': 'string',
415-
},
300+
'data': {'format': 'binary', 'title': 'Data', 'type': 'string'},
301+
'media_type': {
302+
'anyOf': [
303+
{
304+
'enum': [
305+
'audio/wav',
306+
'audio/mpeg',
307+
'audio/ogg',
308+
'audio/flac',
309+
'audio/aiff',
310+
'audio/aac',
311+
],
312+
'type': 'string',
313+
},
314+
{'enum': ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], 'type': 'string'},
315+
{
316+
'enum': [
317+
'application/pdf',
318+
'text/plain',
319+
'text/csv',
320+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
321+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
322+
'text/html',
323+
'text/markdown',
324+
'application/msword',
325+
'application/vnd.ms-excel',
326+
],
327+
'type': 'string',
328+
},
329+
{'type': 'string'},
330+
],
331+
'title': 'Media Type',
416332
},
417-
'required': ['data', 'media_type', 'identifier'],
418-
'title': 'BinaryImage',
419-
'type': 'object',
420333
},
421334
],
422335
'$defs': {

0 commit comments

Comments
 (0)