@@ -3780,6 +3780,166 @@ async def source() -> AsyncIterator[ChatCompletionChunk]:
37803780 assert not ChatCmplStreamHandler ._delta_has_passthrough_output (terminal_choices [0 ].delta )
37813781
37823782
3783+ @pytest .mark .asyncio
3784+ async def test_handler_stream_synthesizes_refusal_on_length_truncation () -> None :
3785+ """A stream that terminates with finish_reason == "length" and no emitted
3786+ content must synthesize a ResponseOutputRefusal instead of an empty turn,
3787+ matching the non-streaming path."""
3788+ terminal = ChatCompletionChunk (
3789+ id = "chunk-id" ,
3790+ created = 1 ,
3791+ model = "fake" ,
3792+ object = "chat.completion.chunk" ,
3793+ choices = [Choice (index = 0 , delta = ChoiceDelta (), finish_reason = "length" )],
3794+ )
3795+
3796+ output_events = await _collect_handler_events (terminal )
3797+
3798+ types = [e .type for e in output_events ]
3799+ assert "response.refusal.delta" in types
3800+ assert types [- 1 ] == "response.completed"
3801+
3802+ refusal_deltas = [e for e in output_events if e .type == "response.refusal.delta" ]
3803+ assert refusal_deltas and refusal_deltas [0 ].delta == (
3804+ "Response truncated because the provider's maximum token limit was reached."
3805+ )
3806+
3807+ completed_event = output_events [- 1 ]
3808+ assert isinstance (completed_event , ResponseCompletedEvent )
3809+ assistant_msg = completed_event .response .output [0 ]
3810+ assert isinstance (assistant_msg , ResponseOutputMessage )
3811+ assert len (assistant_msg .content ) == 1
3812+ refusal_part = assistant_msg .content [0 ]
3813+ assert isinstance (refusal_part , ResponseOutputRefusal )
3814+ assert (
3815+ refusal_part .refusal
3816+ == "Response truncated because the provider's maximum token limit was reached."
3817+ )
3818+
3819+
3820+ @pytest .mark .asyncio
3821+ async def test_handler_stream_length_does_not_clobber_text () -> None :
3822+ """A length finish_reason arriving after real text was streamed must not
3823+ synthesize a refusal."""
3824+ chunk1 = _chunk_with ([Choice (index = 0 , delta = ChoiceDelta (content = "answer" ))])
3825+ chunk2 = _chunk_with ([Choice (index = 0 , delta = ChoiceDelta (), finish_reason = "length" )])
3826+
3827+ output_events = await _collect_handler_events (chunk1 , chunk2 )
3828+
3829+ assert "response.refusal.delta" not in [e .type for e in output_events ]
3830+ completed_event = output_events [- 1 ]
3831+ assert isinstance (completed_event , ResponseCompletedEvent )
3832+ assistant_msg = completed_event .response .output [0 ]
3833+ assert isinstance (assistant_msg , ResponseOutputMessage )
3834+ text_part = assistant_msg .content [0 ]
3835+ assert isinstance (text_part , ResponseOutputText )
3836+ assert text_part .text == "answer"
3837+
3838+
3839+ @pytest .mark .allow_call_model_methods
3840+ @pytest .mark .asyncio
3841+ async def test_buffered_stream_synthesizes_refusal_on_length_truncation (monkeypatch ) -> None :
3842+ """With tool-call buffering enabled, a stream that terminates with
3843+ finish_reason == "length" and no emitted content must still synthesize a
3844+ ResponseOutputRefusal, mirroring the content_filter buffered behavior."""
3845+ chunk1 = ChatCompletionChunk (
3846+ id = "chunk-id" ,
3847+ created = 1 ,
3848+ model = "fake" ,
3849+ object = "chat.completion.chunk" ,
3850+ choices = [Choice (index = 0 , delta = ChoiceDelta (role = "assistant" , content = "" ))],
3851+ )
3852+ chunk2 = ChatCompletionChunk (
3853+ id = "chunk-id" ,
3854+ created = 1 ,
3855+ model = "fake" ,
3856+ object = "chat.completion.chunk" ,
3857+ choices = [Choice (index = 0 , delta = ChoiceDelta (), finish_reason = "length" )],
3858+ usage = CompletionUsage (completion_tokens = 0 , prompt_tokens = 7 , total_tokens = 7 ),
3859+ )
3860+
3861+ output_events = await _buffered_stream_events (monkeypatch , [chunk1 , chunk2 ])
3862+
3863+ types = [e .type for e in output_events ]
3864+ assert "response.refusal.delta" in types
3865+ assert types [- 1 ] == "response.completed"
3866+
3867+ refusal_deltas = [e for e in output_events if e .type == "response.refusal.delta" ]
3868+ assert refusal_deltas and refusal_deltas [0 ].delta
3869+
3870+ completed_event = output_events [- 1 ]
3871+ assert isinstance (completed_event , ResponseCompletedEvent )
3872+ assistant_msg = completed_event .response .output [0 ]
3873+ assert isinstance (assistant_msg , ResponseOutputMessage )
3874+ assert len (assistant_msg .content ) == 1
3875+ refusal_part = assistant_msg .content [0 ]
3876+ assert isinstance (refusal_part , ResponseOutputRefusal )
3877+ assert (
3878+ refusal_part .refusal
3879+ == "Response truncated because the provider's maximum token limit was reached."
3880+ )
3881+
3882+
3883+ @pytest .mark .allow_call_model_methods
3884+ @pytest .mark .asyncio
3885+ async def test_buffered_stream_length_does_not_clobber_text (monkeypatch ) -> None :
3886+ """A length finish_reason arriving after real text was streamed must not
3887+ synthesize a refusal, even with buffering enabled."""
3888+ chunk1 = ChatCompletionChunk (
3889+ id = "chunk-id" ,
3890+ created = 1 ,
3891+ model = "fake" ,
3892+ object = "chat.completion.chunk" ,
3893+ choices = [Choice (index = 0 , delta = ChoiceDelta (content = "answer" ))],
3894+ )
3895+ chunk2 = ChatCompletionChunk (
3896+ id = "chunk-id" ,
3897+ created = 1 ,
3898+ model = "fake" ,
3899+ object = "chat.completion.chunk" ,
3900+ choices = [Choice (index = 0 , delta = ChoiceDelta (), finish_reason = "length" )],
3901+ usage = CompletionUsage (completion_tokens = 1 , prompt_tokens = 7 , total_tokens = 8 ),
3902+ )
3903+
3904+ output_events = await _buffered_stream_events (monkeypatch , [chunk1 , chunk2 ])
3905+
3906+ assert "response.refusal.delta" not in [e .type for e in output_events ]
3907+ completed_event = output_events [- 1 ]
3908+ assert isinstance (completed_event , ResponseCompletedEvent )
3909+ assistant_msg = completed_event .response .output [0 ]
3910+ assert isinstance (assistant_msg , ResponseOutputMessage )
3911+ text_part = assistant_msg .content [0 ]
3912+ assert isinstance (text_part , ResponseOutputText )
3913+ assert text_part .text == "answer"
3914+
3915+
3916+ @pytest .mark .asyncio
3917+ async def test_buffer_tool_call_stream_forwards_length_finish_reason () -> None :
3918+ """The buffering layer must forward a length-truncated terminal choice even
3919+ though its delta is empty, so the finish_reason reaches the handler instead
3920+ of being swallowed (mirrors the content_filter forwarding behavior)."""
3921+ chunks = [
3922+ _chunk_with ([Choice (index = 0 , delta = ChoiceDelta (content = "" ))]),
3923+ _chunk_with ([Choice (index = 0 , delta = ChoiceDelta (), finish_reason = "length" )]),
3924+ ]
3925+
3926+ async def source () -> AsyncIterator [ChatCompletionChunk ]:
3927+ for chunk in chunks :
3928+ yield chunk
3929+
3930+ buffered = [c async for c in ChatCmplStreamHandler .buffer_tool_call_stream (source ())]
3931+
3932+ terminal_choices = [
3933+ choice
3934+ for chunk in buffered
3935+ for choice in chunk .choices
3936+ if choice .finish_reason == "length"
3937+ ]
3938+ assert len (terminal_choices ) == 1
3939+ # The forwarded copy carries no delta output.
3940+ assert not ChatCmplStreamHandler ._delta_has_passthrough_output (terminal_choices [0 ].delta )
3941+
3942+
37833943@pytest .mark .asyncio
37843944async def test_buffer_tool_call_stream_does_not_duplicate_tool_calls_finish () -> None :
37853945 """finish_reason == "tool_calls" is still emitted only by the synthesized
0 commit comments