[Bugfix] Avoid leaking Pydantic repr in tool_choice error message (#47028)
Signed-off-by: muhammadfawaz1 <135441198+muhammadfawaz1@users.noreply.github.com> Co-authored-by: Mahad Durrani <114791389+mahadrehmann@users.noreply.github.com>
Muhammad Fawaz committed
Jul 8, 2026 at 00:00 UTC
d35eba302f7c4c7c995a2b0ef172d5171d4de13d
2 files changed
+13
-1
tests/entrypoints/openai/chat_completion/test_serving_chat.py
+7
@@ -2124,6 +2124,13 @@ async def test_tool_choice_validation_without_parser():
2124
assert isinstance(response_named, ErrorResponse)
2125
assert "tool_choice" in response_named.error.message
2126
assert "--tool-call-parser" in response_named.error.message
2127
+ # The function name should appear in a clean, readable form -
2128
+ # guards against leaking Pydantic's internal repr of the
2129
+ # ChatCompletionNamedToolChoiceParam/ChatCompletionNamedFunction
2130
+ # objects directly into the client-facing error message.
2131
+ assert "get_weather" in response_named.error.message
2132
+ assert "ChatCompletionNamedFunction" not in response_named.error.message
2133
+ assert "ChatCompletionNamedToolChoiceParam" not in response_named.error.message
2134
2135
2136
@pytest.mark.asyncio
vllm/renderers/online_renderer.py
+6
-1
@@ -12,6 +12,7 @@ from vllm.entrypoints.chat_utils import (
12
ConversationMessage,
13
)
14
from vllm.entrypoints.openai.chat_completion.protocol import (
15
+ ChatCompletionNamedToolChoiceParam,
16
ChatCompletionRequest,
17
)
18
from vllm.entrypoints.openai.completion.protocol import (
@@ -134,8 +135,12 @@ class OnlineRenderer:
135
)
136
elif request.tool_choice != "auto":
137
# "required" or named tool requires tool parser
138
+ if isinstance(request.tool_choice, ChatCompletionNamedToolChoiceParam):
139
+ tool_choice_desc = f'function "{request.tool_choice.function.name}"'
140
+ else:
141
+ tool_choice_desc = f'"{request.tool_choice}"'
142
return self.create_error_response(
138
- f'tool_choice="{request.tool_choice}" requires '
143
+ f"tool_choice={tool_choice_desc} requires "
144
"--tool-call-parser to be set"
145
)
146