From e819e2c07df261f41b521e2c9b784bdc5e5d959a Mon Sep 17 00:00:00 2001 From: tkasuz <63289889+tkasuz@users.noreply.github.com> Date: Mon, 1 Sep 2025 21:44:53 +0900 Subject: [PATCH] fix(openai): prevent UnboundLocalError for choice variable in stream method Signed-off-by: tkasuz <63289889+tkasuz@users.noreply.github.com> --- src/strands/models/openai.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/strands/models/openai.py b/src/strands/models/openai.py index 1076fbae4..8d1cafeca 100644 --- a/src/strands/models/openai.py +++ b/src/strands/models/openai.py @@ -351,6 +351,7 @@ async def stream( yield self.format_chunk({"chunk_type": "content_start", "data_type": "text"}) tool_calls: dict[int, list[Any]] = {} + choice = None async for event in response: # Defensive: skip events with empty or missing choices @@ -388,7 +389,8 @@ async def stream( yield self.format_chunk({"chunk_type": "content_stop", "data_type": "tool"}) - yield self.format_chunk({"chunk_type": "message_stop", "data": choice.finish_reason}) + if choice and choice.finish_reason: + yield self.format_chunk({"chunk_type": "message_stop", "data": choice.finish_reason}) # Skip remaining events as we don't have use for anything except the final usage payload async for event in response: