fix(core_helpers): map Anthropic model_context_window_exceeded stop reason to length

Anthropic returns stop_reason="model_context_window_exceeded" when generation
stops because the context window filled up. _FINISH_REASON_MAP had no entry for
it, so map_finish_reason logged a warning and returned "stop" — clients cannot
distinguish a truncated response from a complete one, on both chat completions
and the Responses API.

Map it to "length" (consistent with max_tokens and the existing compaction
entry) and cover it in the Anthropic parametrized finish-reason tests.

Fixes #43012
This commit is contained in:
JingHao-Leon 2026-09-25 10:12:04 +08:00
parent 118ce3cc91
commit bbc265d912
2 changed files with 2 additions and 0 deletions

View file

@ -199,6 +199,7 @@ _FINISH_REASON_MAP: Final[dict[str, OpenAIChatCompletionFinishReason]] = {
"stop_sequence": "stop",
"end_turn": "stop",
"max_tokens": "length",
"model_context_window_exceeded": "length",
"tool_use": "tool_calls",
"refusal": "content_filter",
"compaction": "length",

View file

@ -178,6 +178,7 @@ class TestMapFinishReasonAnthropic:
("stop_sequence", "stop"),
("end_turn", "stop"),
("max_tokens", "length"),
("model_context_window_exceeded", "length"),
("tool_use", "tool_calls"),
("compaction", "length"),
("content_filtered", "content_filter"),