fix: report stop_sequence stop_reason in Anthropic Messages API (#45807)

Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com>
Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
Yufeng He
2026-08-20 03:23:43 +00:00
committed by GitHub
co-authored by Chauncey
parent f85e060ec2
commit e85dd21497
2 changed files with 122 additions and 5 deletions
+23 -5
View File
@@ -631,7 +631,13 @@ class AnthropicServingMessages(OpenAIServingChat):
)
choice = generator.choices[0]
if choice.finish_reason == "stop":
result.stop_reason = "end_turn"
# vLLM reports the matched stop string in stop_reason (a str);
# an int stop-token-id or None (natural EOS) maps to end_turn.
if isinstance(choice.stop_reason, str):
result.stop_reason = "stop_sequence"
result.stop_sequence = choice.stop_reason
else:
result.stop_reason = "end_turn"
elif choice.finish_reason == "length":
result.stop_reason = "max_tokens"
elif choice.finish_reason == "tool_calls":
@@ -715,6 +721,9 @@ class AnthropicServingMessages(OpenAIServingChat):
first_item = True
finish_reason = None
# Matched stop string, when generation stopped on one (a str);
# int stop-token-id / None are not stop sequences.
stop_sequence: int | str | None = None
state = _ActiveBlockState()
# Map from tool call index to tool_use_id
tool_index_to_id: dict[int, str] = {}
@@ -824,12 +833,20 @@ class AnthropicServingMessages(OpenAIServingChat):
if len(origin_chunk.choices) == 0:
for event in stop_and_flush():
yield event
stop_reason = self.stop_reason_map.get(
finish_reason or "stop"
)
if isinstance(stop_sequence, str):
stop_delta = AnthropicDelta(
stop_reason="stop_sequence",
stop_sequence=stop_sequence,
)
else:
stop_delta = AnthropicDelta(
stop_reason=self.stop_reason_map.get(
finish_reason or "stop"
)
)
chunk = AnthropicStreamEvent(
type="message_delta",
delta=AnthropicDelta(stop_reason=stop_reason),
delta=stop_delta,
usage=_build_anthropic_usage(origin_chunk.usage),
)
data = chunk.model_dump_json(exclude_unset=True)
@@ -838,6 +855,7 @@ class AnthropicServingMessages(OpenAIServingChat):
if origin_chunk.choices[0].finish_reason is not None:
finish_reason = origin_chunk.choices[0].finish_reason
stop_sequence = origin_chunk.choices[0].stop_reason
# continue
# thinking / text content