Skip to content

Commit d7fbca5

Browse files
fix: enable sequential tool execution in OpenAI client
Removes premature loop termination after tool execution to allow sequential tool calls. The model can now see tool results and make additional tool calls as needed. Fixes #845 Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
1 parent ddae919 commit d7fbca5

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

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

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -906,29 +906,7 @@ def chat_completion_with_tools(
906906
should_continue = True
907907
break
908908

909-
if not should_continue:
910-
# Get final response after tool calls
911-
if stream:
912-
final_response = self.process_stream_response(
913-
messages=messages,
914-
model=model,
915-
temperature=temperature,
916-
tools=formatted_tools,
917-
start_time=start_time,
918-
console=console,
919-
display_fn=display_fn,
920-
reasoning_steps=reasoning_steps,
921-
**kwargs
922-
)
923-
else:
924-
final_response = self.create_completion(
925-
messages=messages,
926-
model=model,
927-
temperature=temperature,
928-
stream=False,
929-
**kwargs
930-
)
931-
break
909+
# Continue the loop to allow more tool calls
932910

933911
iteration_count += 1
934912
else:
@@ -1083,29 +1061,7 @@ async def achat_completion_with_tools(
10831061
should_continue = True
10841062
break
10851063

1086-
if not should_continue:
1087-
# Get final response after tool calls
1088-
if stream:
1089-
final_response = await self.process_stream_response_async(
1090-
messages=messages,
1091-
model=model,
1092-
temperature=temperature,
1093-
tools=formatted_tools,
1094-
start_time=start_time,
1095-
console=console,
1096-
display_fn=display_fn,
1097-
reasoning_steps=reasoning_steps,
1098-
**kwargs
1099-
)
1100-
else:
1101-
final_response = await self.acreate_completion(
1102-
messages=messages,
1103-
model=model,
1104-
temperature=temperature,
1105-
stream=False,
1106-
**kwargs
1107-
)
1108-
break
1064+
# Continue the loop to allow more tool calls
11091065

11101066
iteration_count += 1
11111067
else:

test_sequential_fix.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test script to verify sequential tool execution works correctly with OpenAI client.
4+
This demonstrates the fix for issue #845.
5+
"""
6+
7+
from praisonaiagents import Agent
8+
9+
def get_stock_price(company_name: str) -> str:
10+
"""
11+
Get the stock price of a company
12+
13+
Args:
14+
company_name (str): The name of the company
15+
16+
Returns:
17+
str: The stock price of the company
18+
"""
19+
print(f"[Tool Called] get_stock_price with company_name='{company_name}'")
20+
return f"The stock price of {company_name} is 100"
21+
22+
def multiply(a: int, b: int) -> int:
23+
"""
24+
Multiply two numbers
25+
"""
26+
print(f"[Tool Called] multiply with a={a}, b={b}")
27+
return a * b
28+
29+
# Test with OpenAI client
30+
print("Testing sequential tool execution with OpenAI client...")
31+
print("-" * 60)
32+
33+
agent = Agent(
34+
instructions="You are a helpful assistant. You can use the tools provided to you to help the user.",
35+
llm="gpt-4o",
36+
tools=[get_stock_price, multiply]
37+
)
38+
39+
result = agent.start("what is the stock price of Google? multiply the Google stock price with 2")
40+
print("\n" + "=" * 60)
41+
print("FINAL RESULT:")
42+
print(result)
43+
print("=" * 60)
44+
45+
# Expected behavior:
46+
# 1. The agent should call get_stock_price("Google") and get "100"
47+
# 2. The agent should then call multiply(100, 2) and get "200"
48+
# 3. The final response should mention both the stock price and the result of multiplication

0 commit comments

Comments
 (0)