Skip to content

fix a bug of openai provider stop reason #567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/strands/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async def stream(

tool_calls: dict[int, list[Any]] = {}

has_tool_call = False
async for event in response:
# Defensive: skip events with empty or missing choices
if not getattr(event, "choices", None):
Expand All @@ -374,8 +375,11 @@ async def stream(

for tool_call in choice.delta.tool_calls or []:
tool_calls.setdefault(tool_call.index, []).append(tool_call)
has_tool_call = True

if choice.finish_reason:
if choice.finish_reason == "stop" and has_tool_call:
choice.finish_reason = "tool_calls"
break

yield self.format_chunk({"chunk_type": "content_stop", "data_type": "text"})
Expand Down