Skip to content

Commit 3b14091

Browse files
committed
feat: add MultiModalContent for customizable multi-modal tool presentation
1 parent 81c734b commit 3b14091

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pydantic_ai_slim/pydantic_ai/_agent_graph.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,19 @@ async def process_function_tools( # noqa C901
734734

735735
processed_contents: list[Any] = []
736736
for content in contents:
737-
if isinstance(content, _messages.MultiModalContentTypes):
737+
if isinstance(content, _messages.MultiModalToolResponse):
738+
# Handle new wrapper class with custom content and tool return
739+
user_parts.append(
740+
_messages.UserPromptPart(
741+
content=list(content.content),
742+
timestamp=result.timestamp,
743+
part_kind='user-prompt',
744+
)
745+
)
746+
processed_contents.append(content.tool_return)
747+
748+
elif isinstance(content, _messages.MultiModalContentTypes):
749+
# Handle direct multimodal content
738750
if isinstance(content, _messages.BinaryContent):
739751
identifier = multi_modal_content_identifier(content.data)
740752
else:
@@ -749,6 +761,7 @@ async def process_function_tools( # noqa C901
749761
)
750762
processed_contents.append(f'See file {identifier}')
751763
else:
764+
# Handle regular content
752765
processed_contents.append(content)
753766

754767
if single_content:

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,24 @@ def format(self) -> str:
303303

304304
UserContent: TypeAlias = 'str | ImageUrl | AudioUrl | DocumentUrl | VideoUrl | BinaryContent'
305305

306+
307+
@dataclass(repr=False)
308+
class MultiModalToolResponse:
309+
"""A wrapper for multi-modal content with customizable prompt and tool return value.
310+
311+
This allows tools to return multi-modal content with custom user prompts and tool return messages,
312+
providing more flexibility than the default "This is file {identifier}:" format.
313+
"""
314+
315+
content: Sequence[UserContent]
316+
"""The content sequence to be sent to the model as a UserPromptPart."""
317+
318+
tool_return: Any
319+
"""The return value to be used in the tool response."""
320+
321+
__repr__ = _utils.dataclasses_no_defaults_repr
322+
323+
306324
# Ideally this would be a Union of types, but Python 3.9 requires it to be a string, and strings don't work with `isinstance``.
307325
MultiModalContentTypes = (ImageUrl, AudioUrl, DocumentUrl, VideoUrl, BinaryContent)
308326
_document_format_lookup: dict[str, DocumentFormat] = {

0 commit comments

Comments
 (0)