- Notifications
You must be signed in to change notification settings - Fork1k
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue inhttps://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
After v0.3.3 release then tools that returns images in the form of BinaryContent throws an error, when instrumenting with logfire. Downgrading to v0.3.1 fixes the problem.
The error is
File "/home/jonathan/miniconda3/lib/python3.12/site-packages/pydantic_ai/_agent_graph.py", line 545, in _handle_tool_calls
async for event in process_function_tools(
File "/home/jonathan/miniconda3/lib/python3.12/site-packages/pydantic_ai/_agent_graph.py", line 737, in process_function_tools
result = task.result()
^^^^^^^^^^^^^
File "/home/jonathan/miniconda3/lib/python3.12/site-packages/pydantic_ai/tools.py", line 369, in run
response.model_response_str()
File "/home/jonathan/miniconda3/lib/python3.12/site-packages/pydantic_ai/messages.py", line 466, in model_response_str
return tool_return_ta.dump_json(self.content).decode()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/jonathan/miniconda3/lib/python3.12/site-packages/pydantic/type_adapter.py", line 581, in dump_json
return self.serializer.to_json(
^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.PydanticSerializationError: Error serializing to JSON: invalid utf-8 sequence of 1 bytes from index 0
Since I really, really like using pydantic-ai with logfire then I thought I'll post it here.@DouweM
Example Code
importasynciofromdataclassesimportdataclassimportplotly.expressaspxfrompydantic_aiimportAgent,BinaryContent,RunContextfrompydantic_ai.models.openaiimportOpenAIResponsesModelimportlogfirelogfire.configure()@dataclassclassEmptyDeps:passmodel=OpenAIResponsesModel(model_name="o3")assistant=Agent(model=model,deps_type=EmptyDeps)@assistant.toolasyncdefmake_plot(ctx:RunContext[EmptyDeps])->BinaryContent:"""Return a tiny Plotly figure as PNG bytes – triggers the bug."""fig=px.scatter(x=[1,2,3],y=[1,4,9],title="Demo plot")png=fig.to_image(format="png",engine="kaleido")returnBinaryContent(png,media_type="image/png")asyncdefmain()->None:# One-shot, non-streaming call.reply=awaitassistant.run("please make a plot",deps=EmptyDeps())print(reply)# ← program never reaches here; exception raised earlier# If you comment out this line, the program runs fine.Agent.instrument_all()if__name__=="__main__":asyncio.run(main())
Python, Pydantic AI & LLM client version
v0.4.2