[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:
@@ -271,3 +271,19 @@ def test_encode_messages_preserves_small_chat_prompt(encoding_module):
|
|||||||
"<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>"
|
"<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>"
|
||||||
"Hi<|end▁of▁sentence|>Again<|end▁of▁sentence|>"
|
"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",
|
||||||
|
)
|
||||||
|
|||||||
@@ -113,6 +113,17 @@ def test_deepseek_v4_explicitly_disables_thinking(kwargs):
|
|||||||
assert prompt == ("<|begin▁of▁sentence|><|User|>Hello<|Assistant|></think>")
|
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():
|
def test_deepseek_v4_uses_v4_tool_prompt_from_request_tools():
|
||||||
tools = [
|
tools = [
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ def render_message(
|
|||||||
tool_calls=tool_calls_content,
|
tool_calls=tool_calls_content,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(f"Unknown role: {role}")
|
raise ValueError(f"Invalid role: {role}")
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
|
|||||||
@@ -353,7 +353,7 @@ def render_message(
|
|||||||
tool_calls=tc_content,
|
tool_calls=tc_content,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(f"Unknown role: {role}")
|
raise ValueError(f"Invalid role: {role}")
|
||||||
|
|
||||||
# Append transition tokens based on what follows
|
# Append transition tokens based on what follows
|
||||||
if index + 1 < len(messages) and messages[index + 1].get("role") not in ["assistant", "latest_reminder"]:
|
if index + 1 < len(messages) and messages[index + 1].get("role") not in ["assistant", "latest_reminder"]:
|
||||||
|
|||||||
Reference in New Issue
Block a user