From e4b58b8caf74935a029c215aaad3d0c02eb5ac23 Mon Sep 17 00:00:00 2001 From: Charles Lee Date: Sat, 25 Jan 2025 12:35:54 +0700 Subject: [PATCH 1/4] Only include tool_call_id if it's set Google seems to not return a tool_call_id, which breaks the sample. --- examples/pydantic_ai_examples/weather_agent_gradio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pydantic_ai_examples/weather_agent_gradio.py b/examples/pydantic_ai_examples/weather_agent_gradio.py index c8086ac36..af88c3353 100644 --- a/examples/pydantic_ai_examples/weather_agent_gradio.py +++ b/examples/pydantic_ai_examples/weather_agent_gradio.py @@ -43,7 +43,7 @@ async def stream_from_agent(prompt: str, chatbot: list[dict], past_messages: lis 'content': 'Parameters: ' + call_args, 'metadata': { 'title': f'🛠️ Using {TOOL_TO_DISPLAY_NAME[call.tool_name]}', - 'id': call.tool_call_id, + **(({'id': {id_}} if (id_ := call.tool_call_id) is not None else {})) }, } chatbot.append(gr_message) From b538bea79d149c8054e933760edb2bdaa4a21c87 Mon Sep 17 00:00:00 2001 From: Charles Lee Date: Sat, 25 Jan 2025 12:42:38 +0700 Subject: [PATCH 2/4] passing linter linter disliked that fancy one liner, so adding a more explicitly readable version here. --- .../pydantic_ai_examples/weather_agent_gradio.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/pydantic_ai_examples/weather_agent_gradio.py b/examples/pydantic_ai_examples/weather_agent_gradio.py index af88c3353..62bcc8a75 100644 --- a/examples/pydantic_ai_examples/weather_agent_gradio.py +++ b/examples/pydantic_ai_examples/weather_agent_gradio.py @@ -38,13 +38,13 @@ async def stream_from_agent(prompt: str, chatbot: list[dict], past_messages: lis if hasattr(call.args, 'args_json') else json.dumps(call.args.args_dict) ) + if call.tool_call_id is not None: + metadata["id"] = {call.tool_call_id} + gr_message = { - 'role': 'assistant', - 'content': 'Parameters: ' + call_args, - 'metadata': { - 'title': f'🛠️ Using {TOOL_TO_DISPLAY_NAME[call.tool_name]}', - **(({'id': {id_}} if (id_ := call.tool_call_id) is not None else {})) - }, + "role": "assistant", + "content": "Parameters: " + call_args, + "metadata": metadata, } chatbot.append(gr_message) if isinstance(call, ToolReturnPart): From a5d32dfeb503a672fc2d6a1ab9801a24cb2ae4f9 Mon Sep 17 00:00:00 2001 From: Charles Lee Date: Sat, 25 Jan 2025 12:44:05 +0700 Subject: [PATCH 3/4] Update weather_agent_gradio.py --- examples/pydantic_ai_examples/weather_agent_gradio.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/pydantic_ai_examples/weather_agent_gradio.py b/examples/pydantic_ai_examples/weather_agent_gradio.py index 62bcc8a75..918ef3a61 100644 --- a/examples/pydantic_ai_examples/weather_agent_gradio.py +++ b/examples/pydantic_ai_examples/weather_agent_gradio.py @@ -38,6 +38,9 @@ async def stream_from_agent(prompt: str, chatbot: list[dict], past_messages: lis if hasattr(call.args, 'args_json') else json.dumps(call.args.args_dict) ) + metadata = { + "title": f"🛠️ Using {TOOL_TO_DISPLAY_NAME[call.tool_name]}", + } if call.tool_call_id is not None: metadata["id"] = {call.tool_call_id} From db8cc55d92b824ad602385c8cad09631481f6e26 Mon Sep 17 00:00:00 2001 From: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com> Date: Mon, 3 Feb 2025 14:09:22 -0500 Subject: [PATCH 4/4] Linting --- examples/pydantic_ai_examples/weather_agent_gradio.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/pydantic_ai_examples/weather_agent_gradio.py b/examples/pydantic_ai_examples/weather_agent_gradio.py index 918ef3a61..d98ef74f6 100644 --- a/examples/pydantic_ai_examples/weather_agent_gradio.py +++ b/examples/pydantic_ai_examples/weather_agent_gradio.py @@ -39,15 +39,15 @@ async def stream_from_agent(prompt: str, chatbot: list[dict], past_messages: lis else json.dumps(call.args.args_dict) ) metadata = { - "title": f"🛠️ Using {TOOL_TO_DISPLAY_NAME[call.tool_name]}", + 'title': f'🛠️ Using {TOOL_TO_DISPLAY_NAME[call.tool_name]}', } if call.tool_call_id is not None: - metadata["id"] = {call.tool_call_id} + metadata['id'] = {call.tool_call_id} gr_message = { - "role": "assistant", - "content": "Parameters: " + call_args, - "metadata": metadata, + 'role': 'assistant', + 'content': 'Parameters: ' + call_args, + 'metadata': metadata, } chatbot.append(gr_message) if isinstance(call, ToolReturnPart):