Skip to content

Commit dab6d91

Browse files
committed
fix(models): attach trailing reasoning on replay and reserve fallback call slots
Addresses the Codex review findings on the PR: - Converter.items_to_messages now attaches pending reasoning state to the current assistant message when a reasoning item arrives after that message was already built. A streamed turn can now store [message, reasoning], and leaving the state pending dropped DeepSeek reasoning_content and Anthropic signed thinking blocks when the next item flushed the message. - Late reasoning items no longer shift unannounced function calls onto already-announced output indexes: tracked-but-unannounced calls reserve the slots the layout formula assumes they occupy when a neighboring slot is handed out (on streamed call announcement and on message announcement). Adds regression tests for both paths.
1 parent 82332df commit dab6d91

3 files changed

Lines changed: 226 additions & 2 deletions

File tree

‎src/agents/models/chatcmpl_converter.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,16 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
10001000
if reasoning_texts:
10011001
pending_reasoning_content = "\n".join(reasoning_texts)
10021002

1003+
if current_assistant_msg is not None:
1004+
# A streamed turn can store the reasoning item after the
1005+
# assistant message it belongs to (reasoning that streams
1006+
# after visible chunks gets a later output slot, so the
1007+
# message is already present here). Attach the pending
1008+
# reasoning state to that message now; leaving it pending
1009+
# would drop it when the next item flushes the message.
1010+
apply_pending_thinking_blocks(current_assistant_msg)
1011+
apply_pending_reasoning_content(current_assistant_msg)
1012+
10031013
# 8) compaction items => reject for chat completions
10041014
elif isinstance(item, dict) and item.get("type") == "compaction":
10051015
raise UserError(

‎src/agents/models/chatcmpl_stream_handler.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ class _StreamOutputLayout:
203203
the next slot nothing has claimed yet at creation time. The final
204204
``response.completed.output`` list is assembled by sorting on these indexes,
205205
keeping positions consistent with what consumers saw on the wire.
206+
207+
Because the reasoning item claims its slot at creation time, tracked-but-
208+
unannounced calls must reserve the slots the slot model assumes they occupy
209+
as soon as a neighboring slot is handed out; otherwise a late reasoning item
210+
would claim one of those slots and the formulas would shift the fallback
211+
calls onto indexes already announced on the wire.
206212
"""
207213

208214
assistant_message_output_idx: int | None = None
@@ -213,8 +219,7 @@ def _reasoning_output_count(self) -> int:
213219
return 1 if self.reasoning_output_idx is not None else 0
214220

215221
def _next_free_output_index(self) -> int:
216-
"""The first slot no announced item has claimed; unannounced function calls
217-
tracked in ``state.function_calls`` reserve nothing."""
222+
"""The first slot no announced item has claimed."""
218223
occupied = list(self.function_call_output_idxs.values())
219224
if self.assistant_message_output_idx is not None:
220225
occupied.append(self.assistant_message_output_idx)
@@ -231,6 +236,13 @@ def assistant_message_output_index(self, state: StreamingState) -> int:
231236
output_index = self._reasoning_output_count()
232237
if self.function_call_output_idxs:
233238
output_index += len(state.function_calls)
239+
# Calls already tracked but not announced yet reserve the slots
240+
# below the message the formula assumes they occupy.
241+
for position, call_index in enumerate(state.function_calls):
242+
if call_index not in self.function_call_output_idxs:
243+
self.function_call_output_idxs[call_index] = output_index - (
244+
len(state.function_calls) - position
245+
)
234246
self.assistant_message_output_idx = output_index
235247

236248
return self.assistant_message_output_idx
@@ -254,6 +266,15 @@ def function_call_output_index(
254266
output_index = self._reasoning_output_count()
255267
if self.assistant_message_output_idx is None:
256268
output_index += function_call_offset
269+
# Calls tracked below this one reserve the slots the slot model
270+
# leaves for them, so a late reasoning item cannot shift them onto
271+
# already-announced indexes.
272+
for position in range(function_call_offset):
273+
call_index = function_call_indices[position]
274+
if call_index not in self.function_call_output_idxs:
275+
self.function_call_output_idxs[call_index] = (
276+
output_index - (function_call_offset - position)
277+
)
257278
else:
258279
function_calls_before_message = (
259280
self.assistant_message_output_idx - self._reasoning_output_count()

‎tests/models/test_openai_chatcompletions_stream.py‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4608,3 +4608,196 @@ async def test_stream_handler_late_reasoning_after_function_call_gets_distinct_o
46084608
assert outputs[0].name == "my_func"
46094609
assert isinstance(outputs[1], ResponseReasoningItem)
46104610
assert outputs[1].summary[0].text == "thinking"
4611+
4612+
4613+
@pytest.mark.asyncio
4614+
async def test_trailing_reasoning_content_survives_replay_after_visible_message() -> None:
4615+
"""Reasoning that streams after visible chunks is stored after the message it
4616+
belongs to; the replay conversion must still attach it to that message."""
4617+
chunks = [
4618+
ChatCompletionChunk(
4619+
id="chunk-id",
4620+
created=1,
4621+
model="fake",
4622+
object="chat.completion.chunk",
4623+
choices=[Choice(index=0, delta=ChoiceDelta(content="answer"))],
4624+
),
4625+
ChatCompletionChunk(
4626+
id="chunk-id",
4627+
created=1,
4628+
model="fake",
4629+
object="chat.completion.chunk",
4630+
choices=[Choice(index=0, delta=ChoiceDelta.model_construct(reasoning_content="summary"))],
4631+
),
4632+
]
4633+
4634+
events = await _collect_handler_events(*chunks, model="deepseek-r1")
4635+
added = [event for event in events if event.type == "response.output_item.added"]
4636+
assert [event.output_index for event in added] == [0, 1]
4637+
4638+
completed_event = next(event for event in events if event.type == "response.completed")
4639+
outputs = completed_event.response.output
4640+
assert isinstance(outputs[0], ResponseOutputMessage)
4641+
assert isinstance(outputs[1], ResponseReasoningItem)
4642+
4643+
replayed_messages = Converter.items_to_messages(
4644+
[item.model_dump() for item in outputs],
4645+
model="deepseek-r1",
4646+
)
4647+
assert len(replayed_messages) == 1
4648+
assert replayed_messages[0]["reasoning_content"] == "summary" # type: ignore[typeddict-item]
4649+
assert replayed_messages[0]["content"] == "answer"
4650+
4651+
4652+
@pytest.mark.asyncio
4653+
async def test_trailing_thinking_blocks_survive_replay_after_visible_message() -> None:
4654+
"""Signed thinking blocks stored after the turn's message must not be dropped
4655+
when the next turn is converted."""
4656+
chunks = [
4657+
_thinking_chunk(content="answer"),
4658+
_thinking_chunk(
4659+
thinking_blocks=[{"type": "thinking", "thinking": "hidden", "signature": "sig"}]
4660+
),
4661+
]
4662+
4663+
events = await _collect_handler_events(*chunks, model="anthropic/claude-4-opus")
4664+
completed_event = next(event for event in events if event.type == "response.completed")
4665+
outputs = completed_event.response.output
4666+
assert isinstance(outputs[0], ResponseOutputMessage)
4667+
assert isinstance(outputs[1], ResponseReasoningItem)
4668+
4669+
replayed_messages = Converter.items_to_messages(
4670+
[item.model_dump() for item in outputs],
4671+
model="anthropic/claude-4-opus",
4672+
preserve_thinking_blocks=True,
4673+
)
4674+
assert len(replayed_messages) == 1
4675+
assert replayed_messages[0]["thinking_blocks"] == [ # type: ignore[typeddict-item]
4676+
{"type": "thinking", "thinking": "hidden", "signature": "sig"}
4677+
]
4678+
assert replayed_messages[0]["content"] == "answer"
4679+
4680+
4681+
@pytest.mark.allow_call_model_methods
4682+
@pytest.mark.asyncio
4683+
async def test_fallback_call_and_late_reasoning_keep_streamed_output_indexes(
4684+
monkeypatch,
4685+
) -> None:
4686+
"""A fallback call preceding a streamed call reserves its slot, so late
4687+
reasoning cannot shift it onto the streamed call's announced index."""
4688+
fallback_first = ChoiceDeltaToolCall(
4689+
index=0,
4690+
function=ChoiceDeltaToolCallFunction(
4691+
name="fallback_first",
4692+
arguments='{"a": 1}',
4693+
),
4694+
type="function",
4695+
)
4696+
streamed_second_start = ChoiceDeltaToolCall(
4697+
index=1,
4698+
id="tool-call-2",
4699+
function=ChoiceDeltaToolCallFunction(
4700+
name="streamed_second",
4701+
arguments="",
4702+
),
4703+
type="function",
4704+
)
4705+
streamed_second_args = ChoiceDeltaToolCall(
4706+
index=1,
4707+
function=ChoiceDeltaToolCallFunction(arguments='{"b": 2}'),
4708+
type="function",
4709+
)
4710+
4711+
chunk1 = ChatCompletionChunk(
4712+
id="chunk-id",
4713+
created=1,
4714+
model="fake",
4715+
object="chat.completion.chunk",
4716+
choices=[Choice(index=0, delta=ChoiceDelta(tool_calls=[fallback_first]))],
4717+
)
4718+
chunk2 = ChatCompletionChunk(
4719+
id="chunk-id",
4720+
created=1,
4721+
model="fake",
4722+
object="chat.completion.chunk",
4723+
choices=[Choice(index=0, delta=ChoiceDelta(tool_calls=[streamed_second_start]))],
4724+
)
4725+
chunk3 = ChatCompletionChunk(
4726+
id="chunk-id",
4727+
created=1,
4728+
model="fake",
4729+
object="chat.completion.chunk",
4730+
choices=[Choice(index=0, delta=ChoiceDelta(tool_calls=[streamed_second_args]))],
4731+
)
4732+
chunk4 = ChatCompletionChunk(
4733+
id="chunk-id",
4734+
created=1,
4735+
model="fake",
4736+
object="chat.completion.chunk",
4737+
choices=[
4738+
Choice(index=0, delta=ChoiceDelta.model_construct(reasoning_content="late-thought"))
4739+
],
4740+
usage=CompletionUsage(completion_tokens=1, prompt_tokens=1, total_tokens=2),
4741+
)
4742+
4743+
async def fake_stream() -> AsyncIterator[ChatCompletionChunk]:
4744+
for chunk in (chunk1, chunk2, chunk3, chunk4):
4745+
yield chunk
4746+
4747+
async def patched_fetch_response(self, *args, **kwargs):
4748+
resp = Response(
4749+
id="resp-id",
4750+
created_at=0,
4751+
model="fake-model",
4752+
object="response",
4753+
output=[],
4754+
tool_choice="none",
4755+
tools=[],
4756+
parallel_tool_calls=False,
4757+
)
4758+
return resp, fake_stream()
4759+
4760+
monkeypatch.setattr(OpenAIChatCompletionsModel, "_fetch_response", patched_fetch_response)
4761+
model = OpenAIProvider(use_responses=False).get_model("gpt-4")
4762+
4763+
output_events = []
4764+
async for event in model.stream_response(
4765+
system_instructions=None,
4766+
input="",
4767+
model_settings=ModelSettings(),
4768+
tools=[],
4769+
output_schema=None,
4770+
handoffs=[],
4771+
tracing=ModelTracing.DISABLED,
4772+
previous_response_id=None,
4773+
conversation_id=None,
4774+
prompt=None,
4775+
):
4776+
output_events.append(event)
4777+
4778+
added_by_name = {
4779+
event.item.name: event.output_index
4780+
for event in output_events
4781+
if event.type == "response.output_item.added"
4782+
and isinstance(event.item, ResponseFunctionToolCall)
4783+
}
4784+
assert added_by_name == {"fallback_first": 0, "streamed_second": 1}
4785+
4786+
reasoning_added = [
4787+
event
4788+
for event in output_events
4789+
if event.type == "response.output_item.added"
4790+
and isinstance(event.item, ResponseReasoningItem)
4791+
]
4792+
assert [event.output_index for event in reasoning_added] == [2]
4793+
4794+
done_indexes = [
4795+
event.output_index for event in output_events if event.type == "response.output_item.done"
4796+
]
4797+
assert sorted(done_indexes) == [0, 1, 2]
4798+
4799+
completed = next(
4800+
event.response for event in output_events if event.type == "response.completed"
4801+
)
4802+
assert [item.type for item in completed.output] == ["function_call", "function_call", "reasoning"]
4803+
assert [item.name for item in completed.output[:2]] == ["fallback_first", "streamed_second"]

0 commit comments

Comments
 (0)