@@ -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