Skip to content

Commit 1bdfc20

Browse files
Aditya Singhadityasingh2400
authored andcommitted
fix: address CodeRabbit review comments on PR #3
- Import ResponseCreatedEvent and reset _reasoning_snapshot to "" when a ResponseCreatedEvent is received inside the retry stream loop, fixing the bug where snapshot text would be duplicated across retries - In test_reasoning_delta_event_type_field: add found=False flag and assert found after the loop so the test properly fails when no ReasoningDeltaEvent is emitted
1 parent 63831e7 commit 1bdfc20

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/agents/run_internal/run_loop.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from openai.types.responses import (
1515
Response,
1616
ResponseCompletedEvent,
17+
ResponseCreatedEvent,
1718
ResponseFunctionToolCall,
1819
ResponseOutputItemDoneEvent,
1920
)
@@ -1485,6 +1486,12 @@ async def rewind_model_request() -> None:
14851486
async for event in retry_stream:
14861487
streamed_result._event_queue.put_nowait(RawResponsesStreamEvent(data=event))
14871488

1489+
# Reset the reasoning snapshot at the start of each new response attempt
1490+
# (e.g., after a retry) so the snapshot field never contains stale text
1491+
# from a failed previous attempt.
1492+
if isinstance(event, ResponseCreatedEvent):
1493+
_reasoning_snapshot = ""
1494+
14881495
# Emit a ReasoningDeltaEvent for reasoning/thinking deltas so consumers don't have
14891496
# to unwrap the raw event themselves.
14901497
if isinstance(event, ResponseReasoningSummaryTextDeltaEvent):

tests/test_reasoning_delta_stream_event.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ async def test_reasoning_delta_event_type_field() -> None:
9797
agent = Agent(name="A", model=model)
9898
result = Runner.run_streamed(agent, input="hi")
9999

100+
found = False
100101
async for event in result.stream_events():
101102
if isinstance(event, ReasoningDeltaEvent):
102103
assert event.type == "reasoning_delta"
104+
found = True
103105
break
106+
assert found, "Expected at least one ReasoningDeltaEvent but none were emitted"
104107

105108

106109
@pytest.mark.asyncio

0 commit comments

Comments
 (0)