From 1bad76b5d6a3e58e7db25c6bc8e29ab212170e6c Mon Sep 17 00:00:00 2001 From: harshalmore31 Date: Wed, 13 Aug 2025 19:07:50 +0530 Subject: [PATCH] added structured output for tools ! --- swarms/structs/agent.py | 12 +++++++++--- swarms/tools/base_tool.py | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 54642f3b..1dc5d876 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -1188,9 +1188,15 @@ class Agent: f"[Structured Output] [Time: {time.strftime('%H:%M:%S')}] \n\n {json.dumps(response, indent=4)}", loop_count, ) - elif self.streaming_on: - pass - else: + elif self.streaming_on and isinstance(response, dict) and response.get("choices"): + # Handle streaming tool calls structured output + tool_calls = response.get("choices", [{}])[0].get("message", {}).get("tool_calls", []) + if tool_calls: + self.pretty_print( + f"[Structured Output] [Time: {time.strftime('%H:%M:%S')}] \n\n {json.dumps(tool_calls, indent=4)}", + loop_count, + ) + elif not self.streaming_on: self.pretty_print( response, loop_count ) diff --git a/swarms/tools/base_tool.py b/swarms/tools/base_tool.py index d5d34779..cd0fae38 100644 --- a/swarms/tools/base_tool.py +++ b/swarms/tools/base_tool.py @@ -3109,6 +3109,7 @@ class BaseTool(BaseModel): "id": tc.get("id") }) + return {"choices": [{"message": {"tool_calls": formatted_calls}}]} if formatted_calls else text_response return text_response