fix: normalize content_filtered finish_reason (#23564)

Map provider finish_reason "content_filtered" to the OpenAI-compatible "content_filter" and extend core_helpers tests to cover this case.

Made-with: Cursor
This commit is contained in:
milan-berri
2026-03-14 19:50:33 +02:00
committed by GitHub
parent b793eee245
commit d29287c1c3
2 changed files with 15 additions and 14 deletions
@@ -96,6 +96,8 @@ _FINISH_REASON_MAP: dict[str, OpenAIChatCompletionFinishReason] = {
"tool_calls": "tool_calls",
"function_call": "function_call",
"content_filter": "content_filter",
# Anthropic Sonnet 4
"content_filtered": "content_filter",
}
@@ -59,20 +59,19 @@ VALID_OPENAI_FINISH_REASONS = {"stop", "length", "tool_calls", "function_call",
class TestMapFinishReasonAnthropic:
def test_stop_sequence(self):
assert map_finish_reason("stop_sequence") == "stop"
def test_end_turn(self):
assert map_finish_reason("end_turn") == "stop"
def test_max_tokens(self):
assert map_finish_reason("max_tokens") == "length"
def test_tool_use(self):
assert map_finish_reason("tool_use") == "tool_calls"
def test_compaction(self):
assert map_finish_reason("compaction") == "length"
@pytest.mark.parametrize(
"provider_reason,expected",
[
("stop_sequence", "stop"),
("end_turn", "stop"),
("max_tokens", "length"),
("tool_use", "tool_calls"),
("compaction", "length"),
("content_filtered", "content_filter"),
],
)
def test_anthropic_finish_reasons(self, provider_reason: str, expected: str) -> None:
assert map_finish_reason(provider_reason) == expected
class TestMapFinishReasonGemini: