Skip to content

Commit 276cce9

Browse files
fix: capture final response after tool execution in LLM
When an agent executes tools and the final iteration contains the answer without additional tool calls, ensure the response text is properly captured and returned instead of returning empty/None. This fixes the issue where agents with multiple tools would return empty responses after successfully executing tools. Fixes #835 Co-authored-by: Mervin Praison <[email protected]>
1 parent 38dfe8e commit 276cce9

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/praisonai-agents/praisonaiagents/llm/llm.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,9 @@ def get_response(
10171017
return final_response_text
10181018
else:
10191019
# No tool calls, we're done with this iteration
1020+
# If we've executed tools in previous iterations, this response contains the final answer
1021+
if iteration_count > 0:
1022+
final_response_text = response_text.strip()
10201023
break
10211024

10221025
except Exception as e:

test_issue_835.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from praisonaiagents import Agent
2+
3+
def get_stock_price(company_name: str) -> str:
4+
"""
5+
Get the stock price of a company
6+
7+
Args:
8+
company_name (str): The name of the company
9+
10+
Returns:
11+
str: The stock price of the company
12+
"""
13+
return f"The stock price of {company_name} is 100"
14+
15+
def multiply(a: int, b: int) -> int:
16+
"""
17+
Multiply two numbers
18+
"""
19+
return a * b
20+
21+
# Test the agent
22+
agent = Agent(
23+
instructions="You are a helpful assistant. You can use the tools provided to you to help the user.",
24+
llm="openai/gpt-4o-mini",
25+
tools=[get_stock_price, multiply],
26+
verbose=True # Enable verbose to see what's happening
27+
)
28+
29+
print("=== Testing Agent with Multiple Tools ===")
30+
result = agent.start("multiply the Google stock price with 2")
31+
print(f"\nFinal Result: {result}")
32+
print(f"Result Type: {type(result)}")
33+
print(f"Is None: {result is None}")
34+
print(f"Is Empty String: {result == ''}")

0 commit comments

Comments
 (0)