Skip to content

Adding base64 encoding to tool_return_ta #2186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,9 @@ def otel_event(self, settings: InstrumentationSettings) -> Event:
__repr__ = _utils.dataclasses_no_defaults_repr


tool_return_ta: pydantic.TypeAdapter[Any] = pydantic.TypeAdapter(Any, config=pydantic.ConfigDict(defer_build=True))
tool_return_ta: pydantic.TypeAdapter[Any] = pydantic.TypeAdapter(
Any, config=pydantic.ConfigDict(defer_build=True, ser_json_bytes='base64', val_json_bytes='base64')
)


@dataclass(repr=False)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2934,6 +2934,47 @@ def test_binary_content_all_messages_json():
assert messages == result.all_messages()


def test_tool_return_part_binary_content_serialization():
"""Test that ToolReturnPart can properly serialize BinaryContent."""
png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc```\x00\x00\x00\x04\x00\x01\xf6\x178\x00\x00\x00\x00IEND\xaeB`\x82'
binary_content = BinaryContent(png_data, media_type='image/png')

tool_return = ToolReturnPart(tool_name='test_tool', content=binary_content, tool_call_id='test_call_123')

response_str = tool_return.model_response_str()

assert '"kind":"binary"' in response_str
assert '"media_type":"image/png"' in response_str
assert '"data":"' in response_str

response_obj = tool_return.model_response_object()
assert response_obj['return_value']['kind'] == 'binary'
assert response_obj['return_value']['media_type'] == 'image/png'
assert 'data' in response_obj['return_value']


def test_tool_returning_binary_content_directly():
"""Test that a tool returning BinaryContent directly works correctly."""

def llm(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
if len(messages) == 1:
return ModelResponse(parts=[ToolCallPart('get_image', {})])
else:
return ModelResponse(parts=[TextPart('Image received')])

agent = Agent(FunctionModel(llm))

@agent.tool_plain
def get_image() -> BinaryContent:
"""Return a simple image."""
png_data = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc```\x00\x00\x00\x04\x00\x01\xf6\x178\x00\x00\x00\x00IEND\xaeB`\x82'
return BinaryContent(png_data, media_type='image/png')

# This should work without the serialization error
result = agent.run_sync('Get an image')
assert result.output == 'Image received'


def test_instructions_raise_error_when_system_prompt_is_set():
agent = Agent('test', instructions='An instructions!')

Expand Down