Skip to content

Commit 6610c66

Browse files
authored
fix(examples): Check for BaseModel before serializing json (#2729)
1 parent f4a6f0e commit 6610c66

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

examples/pydantic_ai_examples/weather_agent_gradio.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44

55
from httpx import AsyncClient
6+
from pydantic import BaseModel
67

78
from pydantic_ai.messages import ToolCallPart, ToolReturnPart
89
from pydantic_ai_examples.weather_agent import Deps, weather_agent
@@ -48,9 +49,11 @@ async def stream_from_agent(prompt: str, chatbot: list[dict], past_messages: lis
4849
gr_message.get('metadata', {}).get('id', '')
4950
== call.tool_call_id
5051
):
51-
gr_message['content'] += (
52-
f'\nOutput: {json.dumps(call.content)}'
53-
)
52+
if isinstance(call.content, BaseModel):
53+
json_content = call.content.model_dump_json()
54+
else:
55+
json_content = json.dumps(call.content)
56+
gr_message['content'] += f'\nOutput: {json_content}'
5457
yield gr.skip(), chatbot, gr.skip()
5558
chatbot.append({'role': 'assistant', 'content': ''})
5659
async for message in result.stream_text():

0 commit comments

Comments
 (0)