Skip to content

Commit 45e02bd

Browse files
committed
fix(chatcmpl): align streamed length truncation with non-streaming path
1 parent 738034b commit 45e02bd

2 files changed

Lines changed: 182 additions & 12 deletions

File tree

‎src/agents/models/chatcmpl_stream_handler.py‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ async def buffer_tool_call_stream(
445445

446446
if has_passthrough_output:
447447
passthrough_choices.append(choice)
448-
elif choice.finish_reason == "content_filter":
449-
# A content-filtered choice ends the stream with an empty delta, so it
450-
# would otherwise be dropped here and the handler would never see the
451-
# finish_reason it needs to synthesize the refusal. Forward a
452-
# delta-stripped copy so buffering semantics are unchanged.
448+
elif choice.finish_reason in {"content_filter", "length"}:
449+
# A content-filtered or truncated choice ends the stream with an empty
450+
# delta, so it would otherwise be dropped here and the handler would
451+
# never see the finish_reason it needs to synthesize the refusal.
452+
# Forward a delta-stripped copy so buffering semantics are unchanged.
453453
passthrough_choices.append(choice.model_copy(update={"delta": ChoiceDelta()}))
454454

455455
if passthrough_choices or chunk.usage is not None:
@@ -630,7 +630,10 @@ async def handle_stream(
630630
# safety block only through finish_reason == "content_filter" with an
631631
# empty delta and no refusal field. Track it so we can synthesize an
632632
# explicit refusal after the stream if nothing else was emitted.
633+
# A completion truncated before any visible token (finish_reason ==
634+
# "length") has the same shape and must not collapse into an empty turn.
633635
saw_content_filter = False
636+
saw_length = False
634637
async for chunk in stream:
635638
if not state.started:
636639
state.started = True
@@ -675,6 +678,8 @@ async def handle_stream(
675678

676679
if choice.finish_reason == "content_filter":
677680
saw_content_filter = True
681+
elif choice.finish_reason == "length":
682+
saw_length = True
678683

679684
if not choice.delta:
680685
continue
@@ -1117,17 +1122,18 @@ async def handle_stream(
11171122
sequence_number=sequence_number.get_and_increment(),
11181123
)
11191124

1120-
# Content-filter refusal with no emitted output: synthesize a refusal so
1121-
# the completed response carries a ResponseOutputRefusal rather than an
1122-
# empty turn. Only when nothing else was produced (text / refusal / tool
1123-
# calls) — a content_filter that still emitted content is left as-is.
1125+
# Content-filter refusal / zero-token truncation with no emitted output:
1126+
# synthesize a refusal so the completed response carries a
1127+
# ResponseOutputRefusal rather than an empty turn. Only when nothing else
1128+
# was produced (text / refusal / tool calls) — a content_filter or length
1129+
# that still emitted content is left as-is.
11241130
if (
1125-
saw_content_filter
1131+
(saw_content_filter or saw_length)
11261132
and state.text_content_index_and_output is None
11271133
and state.refusal_content_index_and_output is None
11281134
and not state.function_calls
11291135
):
1130-
# A content-filtered turn (e.g. Bedrock) can terminate with no
1136+
# A content-filtered or truncated turn (e.g. Bedrock) can terminate with no
11311137
# emitted output. Its leading empty "" content delta is suppressed
11321138
# above so no text part opens, so we announce a fresh assistant
11331139
# message and place the refusal at content index 0. A reasoning item
@@ -1137,7 +1143,11 @@ async def handle_stream(
11371143
# the sole content part, is at content_index 0 in both the stream and
11381144
# response.completed regardless of any reasoning item.
11391145
refusal_index = 0
1140-
refusal_message = "Response withheld by the provider's content filter."
1146+
refusal_message = (
1147+
"Response withheld by the provider's content filter."
1148+
if saw_content_filter
1149+
else "Response truncated because the provider's maximum token limit was reached."
1150+
)
11411151
state.refusal_content_index_and_output = (
11421152
refusal_index,
11431153
ResponseOutputRefusal(refusal=refusal_message, type="refusal"),

‎tests/models/test_openai_chatcompletions_stream.py‎

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
37843944
async 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

Comments
 (0)