Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

v0.3.3 release breaks logfire integration for BinaryContent #2183

Closed
@josca42

Description

@josca42

Initial Checks

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp