[Bugfix] Return HTTP 400 instead of 501 for unknown chat roles in DeepSeek encoders (#53071)

Signed-off-by: JC-ut0 <809602657@qq.com>
This commit is contained in:
JC-ut0
2026-08-20 09:23:41 +00:00
committed by GitHub
parent c8de519917
commit 38e9cefdef
4 changed files with 29 additions and 2 deletions
+16
View File
@@ -271,3 +271,19 @@ def test_encode_messages_preserves_small_chat_prompt(encoding_module):
"<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>"
"Hi<|end▁of▁sentence|>Again<|end▁of▁sentence|>"
)
@pytest.mark.parametrize(
"encoding_module",
ENCODING_MODULES,
ids=["deepseek_v32", "deepseek_v4"],
)
def test_encode_messages_unknown_role_raises_value_error(encoding_module):
# An invalid role (e.g. uppercase "SYSTEM") is a client error and must be
# raised as ValueError so the OpenAI serving layer maps it to HTTP 400
# instead of NotImplementedError, which would map to HTTP 501.
with pytest.raises(ValueError, match="Invalid role: SYSTEM"):
encoding_module.encode_messages(
[{"role": "SYSTEM", "content": "Hello"}],
thinking_mode="chat",
)
+11
View File
@@ -113,6 +113,17 @@ def test_deepseek_v4_explicitly_disables_thinking(kwargs):
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>")
def test_deepseek_v4_unknown_role_raises_value_error():
# Invalid roles are client errors: they must surface as ValueError
# (mapped to HTTP 400 by the OpenAI serving layer), not
# NotImplementedError (mapped to HTTP 501).
with pytest.raises(ValueError, match="Invalid role: SYSTEM"):
_tokenizer().apply_chat_template(
[{"role": "SYSTEM", "content": "Hello"}],
tokenize=False,
)
def test_deepseek_v4_uses_v4_tool_prompt_from_request_tools():
tools = [
{
+1 -1
View File
@@ -258,7 +258,7 @@ def render_message(
tool_calls=tool_calls_content,
)
else:
raise NotImplementedError(f"Unknown role: {role}")
raise ValueError(f"Invalid role: {role}")
return prompt
+1 -1
View File
@@ -353,7 +353,7 @@ def render_message(
tool_calls=tc_content,
)
else:
raise NotImplementedError(f"Unknown role: {role}")
raise ValueError(f"Invalid role: {role}")
# Append transition tokens based on what follows
if index + 1 < len(messages) and messages[index + 1].get("role") not in ["assistant", "latest_reminder"]: