Skip to content

Commit e89c8dd

Browse files
elainegan-openaicodex
authored andcommitted
Narrow streamed agent tool recovery
Only recover streamed nested-agent text when the nested run loop task has already finished, so caller-triggered cancellation still propagates. Also keep the formatting and mypy follow-up for agent.py clean. Co-authored-by: Codex <noreply@openai.com>
1 parent e76d591 commit e89c8dd

1 file changed

Lines changed: 4 additions & 9 deletions

File tree

src/agents/agent.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ def _recover_streamed_agent_tool_text(run_result: Any) -> str | None:
140140
if not isinstance(outputs, list):
141141
continue
142142
for output_item in outputs:
143-
text = ItemHelpers.extract_last_text(output_item)
144-
if text:
145-
recovered_chunks.append(text)
143+
chunk = ItemHelpers.extract_last_text(output_item)
144+
if chunk:
145+
recovered_chunks.append(chunk)
146146

147147
if not recovered_chunks:
148148
return None
@@ -151,9 +151,6 @@ def _recover_streamed_agent_tool_text(run_result: Any) -> str | None:
151151

152152
def _can_recover_cancelled_streamed_agent_tool(run_result: Any) -> bool:
153153
"""Recover text only after the nested streamed run has already reached local terminal state."""
154-
if getattr(run_result, "is_complete", False):
155-
return True
156-
157154
run_loop_task = getattr(run_result, "run_loop_task", None)
158155
if isinstance(run_loop_task, asyncio.Task):
159156
return run_loop_task.done()
@@ -851,9 +848,7 @@ async def dispatch_stream_events() -> None:
851848
except asyncio.CancelledError:
852849
if not _can_recover_cancelled_streamed_agent_tool(run_result_streaming):
853850
raise
854-
recovered_text = _recover_streamed_agent_tool_text(
855-
run_result_streaming
856-
)
851+
recovered_text = _recover_streamed_agent_tool_text(run_result_streaming)
857852
if not recovered_text:
858853
raise
859854
run_result_streaming.final_output = recovered_text

0 commit comments

Comments
 (0)