[Bugfix][Frontend] Preserve Cohere and Anthropic streaming errors (#61947)
Signed-off-by: Kevin <jia__kuan@163.com> Co-authored-by: OpenAI Codex <noreply@openai.com>
This commit is contained in:
@@ -49,6 +49,9 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatMessage,
|
||||
)
|
||||
from vllm.entrypoints.serve.engine.protocol import PromptTokenUsageInfo, UsageInfo
|
||||
from vllm.entrypoints.serve.exception_handling.error_response import (
|
||||
create_error_response,
|
||||
)
|
||||
from vllm.entrypoints.serve.exception_handling.handlers.validation import (
|
||||
validation_exception_handler,
|
||||
)
|
||||
@@ -1034,7 +1037,7 @@ def _make_stream_chunk(
|
||||
stop_reason: int | str | None = None,
|
||||
choices: list[ChatCompletionResponseStreamChoice] | None = None,
|
||||
usage: UsageInfo | None = None,
|
||||
) -> str:
|
||||
) -> ChatCompletionStreamResponse:
|
||||
if choices is None:
|
||||
choices = [
|
||||
ChatCompletionResponseStreamChoice(
|
||||
@@ -1051,7 +1054,7 @@ def _make_stream_chunk(
|
||||
choices=choices,
|
||||
usage=usage,
|
||||
)
|
||||
return f"data: {chunk.model_dump_json()}"
|
||||
return chunk
|
||||
|
||||
|
||||
def _tc(*, args, id=None, name=None):
|
||||
@@ -1062,6 +1065,39 @@ def _tc(*, args, id=None, name=None):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("block_type", [None, "text", "thinking", "tool_use"])
|
||||
async def test_stream_error_preserves_message_and_closes_block(block_type):
|
||||
async def source():
|
||||
if block_type is not None:
|
||||
yield _make_stream_chunk(delta=DeltaMessage(role="assistant"))
|
||||
if block_type == "text":
|
||||
delta = DeltaMessage(content="Hello")
|
||||
elif block_type == "thinking":
|
||||
delta = DeltaMessage(reasoning="Thinking")
|
||||
else:
|
||||
delta = DeltaMessage(
|
||||
tool_calls=[_tc(id="call_1", name="search", args='{"q":')]
|
||||
)
|
||||
yield _make_stream_chunk(delta=delta)
|
||||
yield create_error_response(ValueError("backend failed"))
|
||||
|
||||
converter = _make_stream_converter()
|
||||
events = _parse_sse_events(
|
||||
[event async for event in converter.message_stream_converter(source())]
|
||||
)
|
||||
assert events[-1] == (
|
||||
"error",
|
||||
{
|
||||
"type": "error",
|
||||
"error": {"type": "internal_error", "message": "backend failed"},
|
||||
},
|
||||
)
|
||||
assert not any(event_type == "message_stop" for event_type, _ in events)
|
||||
if block_type is not None:
|
||||
assert events[-2][0] == "content_block_stop"
|
||||
|
||||
|
||||
class TestMessageStreamConverterToolUseContentBuffering:
|
||||
"""Regression test for tool_use arguments being silently dropped.
|
||||
|
||||
@@ -1074,7 +1110,7 @@ class TestMessageStreamConverterToolUseContentBuffering:
|
||||
async def test_tool_use_args_not_dropped_when_content_in_same_chunk(
|
||||
self,
|
||||
):
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(role="assistant"),
|
||||
usage=UsageInfo(prompt_tokens=10, total_tokens=10),
|
||||
@@ -1111,11 +1147,10 @@ class TestMessageStreamConverterToolUseContentBuffering:
|
||||
completion_tokens=20,
|
||||
),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
|
||||
events = _parse_sse_events(output)
|
||||
@@ -1156,7 +1191,7 @@ class TestMessageStreamConverterToolUseContentBuffering:
|
||||
stream jumps straight from finish_reason to [DONE], skipping the
|
||||
empty-choices usage chunk."""
|
||||
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(role="assistant"),
|
||||
usage=UsageInfo(prompt_tokens=10, total_tokens=10),
|
||||
@@ -1178,11 +1213,10 @@ class TestMessageStreamConverterToolUseContentBuffering:
|
||||
finish_reason="tool_calls",
|
||||
)
|
||||
# No empty-choices usage chunk — go straight to [DONE].
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
|
||||
events = _parse_sse_events(output)
|
||||
@@ -1216,7 +1250,7 @@ class TestMessageStartIncludesTypeAndRole:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_message_start_contains_message_type_and_role(self):
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(content="Hello"),
|
||||
usage=UsageInfo(
|
||||
@@ -1226,11 +1260,10 @@ class TestMessageStartIncludesTypeAndRole:
|
||||
),
|
||||
)
|
||||
yield _make_stream_chunk(finish_reason="stop")
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
|
||||
events = _parse_sse_events(output)
|
||||
@@ -1260,7 +1293,7 @@ class TestStreamingCacheUsageSemantics:
|
||||
message_start omits cache fields. The final chunk carries
|
||||
prompt_tokens_details, so message_delta carries resolved values."""
|
||||
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(role="assistant", content="hi"),
|
||||
usage=UsageInfo(prompt_tokens=100, total_tokens=100),
|
||||
@@ -1277,11 +1310,10 @@ class TestStreamingCacheUsageSemantics:
|
||||
),
|
||||
),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
events = _parse_sse_events(output)
|
||||
|
||||
@@ -1305,7 +1337,7 @@ class TestStreamingCacheUsageSemantics:
|
||||
"""When the final chunk reports cached_tokens=0, message_delta carries
|
||||
cache fields = 0 (cache miss); message_start still omits them."""
|
||||
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(role="assistant"),
|
||||
usage=UsageInfo(prompt_tokens=50, total_tokens=50),
|
||||
@@ -1322,11 +1354,10 @@ class TestStreamingCacheUsageSemantics:
|
||||
),
|
||||
),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
events = _parse_sse_events(output)
|
||||
|
||||
@@ -1346,7 +1377,7 @@ class TestStreamingCacheUsageSemantics:
|
||||
"""If --enable-prompt-tokens-details is off, no chunk carries cache
|
||||
info; both message_start and message_delta omit cache fields."""
|
||||
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(role="assistant"),
|
||||
usage=UsageInfo(prompt_tokens=30, total_tokens=30),
|
||||
@@ -1356,11 +1387,10 @@ class TestStreamingCacheUsageSemantics:
|
||||
choices=[],
|
||||
usage=UsageInfo(prompt_tokens=30, completion_tokens=2, total_tokens=32),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
events = _parse_sse_events(output)
|
||||
|
||||
@@ -1628,7 +1658,7 @@ class TestStopSequenceReason:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_streaming_stop_string_maps_to_stop_sequence(self):
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(content="hi"),
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=5, completion_tokens=0),
|
||||
@@ -1638,11 +1668,10 @@ class TestStopSequenceReason:
|
||||
choices=[],
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=8, completion_tokens=3),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
|
||||
events = _parse_sse_events(output)
|
||||
@@ -1654,7 +1683,7 @@ class TestStopSequenceReason:
|
||||
async def test_streaming_no_stop_string_emits_explicit_null_stop_sequence(self):
|
||||
"""exclude_unset=True drops stop_sequence unless it is set explicitly."""
|
||||
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(delta=DeltaMessage(role="assistant"))
|
||||
yield _make_stream_chunk(delta=DeltaMessage(content="hi"))
|
||||
yield _make_stream_chunk(finish_reason="stop")
|
||||
@@ -1662,11 +1691,10 @@ class TestStopSequenceReason:
|
||||
choices=[],
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=8, completion_tokens=3),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
output = []
|
||||
async for event in converter.message_stream_converter(sse_input()):
|
||||
async for event in converter.message_stream_converter(chunk_input()):
|
||||
output.append(event)
|
||||
|
||||
events = _parse_sse_events(output)
|
||||
@@ -1724,7 +1752,7 @@ class TestToolUseStopReason:
|
||||
],
|
||||
)
|
||||
async def test_streaming(self, args, finish_reason, stop_reason, expected):
|
||||
async def sse_input():
|
||||
async def chunk_input():
|
||||
yield _make_stream_chunk(delta=DeltaMessage(role="assistant"))
|
||||
yield _make_stream_chunk(
|
||||
delta=DeltaMessage(
|
||||
@@ -1738,11 +1766,10 @@ class TestToolUseStopReason:
|
||||
choices=[],
|
||||
usage=UsageInfo(prompt_tokens=5, total_tokens=8, completion_tokens=3),
|
||||
)
|
||||
yield "data: [DONE]"
|
||||
|
||||
converter = _make_stream_converter()
|
||||
events = _parse_sse_events(
|
||||
[e async for e in converter.message_stream_converter(sse_input())]
|
||||
[e async for e in converter.message_stream_converter(chunk_input())]
|
||||
)
|
||||
|
||||
msg_deltas = [data for ev_type, data in events if ev_type == "message_delta"]
|
||||
|
||||
Reference in New Issue
Block a user