@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / f61c9da711
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 9, 18:15 UTC

[BugFix] deepseek_v32_encoding: Replace asserts with proper exceptions (#32884)

Signed-off-by: RishabhSaini <rishabhsaini01@gmail.com>

Rishabh Saini committed Jan 22, 2026 at 22:44 UTC f61c9da711d846220cb59738d10c0a937ffb80e2
1 file changed +39 -28
vllm/tokenizers/deepseek_v32_encoding.py
+39 -28
@@ -154,10 +154,12 @@ def find_last_user_index(messages: list[dict[str, Any]]) -> int:
154 def render_message(
155 index: int, messages: list[dict[str, Any]], thinking_mode: str
156 ) -> str:
157 - assert 0 <= index < len(messages)
158 - assert thinking_mode in ["chat", "thinking"], (
159 - f"Invalid thinking_mode `{thinking_mode}`"
160 - )
157 + if not (0 <= index < len(messages)):
158 + raise ValueError(
159 + f"Index {index} out of range for messages list of length {len(messages)}"
160 + )
161 + if thinking_mode not in ["chat", "thinking"]:
162 + raise ValueError(f"Invalid thinking_mode `{thinking_mode}`")
163
164 prompt = ""
165 msg = messages[index]
@@ -187,7 +189,8 @@ def render_message(
189 )
190
191 elif role == "developer":
190 - assert content, f"Invalid message for role `{role}`: {msg}"
192 + if not content:
193 + raise ValueError(f"Invalid message for role `{role}`: {msg}")
194 content_developer = ""
195 if tools:
196 content_developer += "\n\n" + render_tools(tools)
@@ -220,17 +223,17 @@ def render_message(
223 prev_assistant_idx -= 1
224 assistant_msg = messages[prev_assistant_idx]
225
223 - assert (
226 + if not (
227 index == 0
228 or prev_assistant_idx >= 0
229 and assistant_msg.get("role") == "assistant"
227 - ), f"Invalid messages at {index}:\n{assistant_msg}"
230 + ):
231 + raise ValueError(f"Invalid messages at {index}:\n{assistant_msg}")
232
233 tool_call_order = index - prev_assistant_idx
234 assistant_tool_calls = assistant_msg.get("tool_calls")
231 - assert assistant_tool_calls and len(assistant_tool_calls) >= tool_call_order, (
232 - "No tool calls but found tool output"
233 - )
235 + if not (assistant_tool_calls and len(assistant_tool_calls) >= tool_call_order):
236 + raise ValueError("No tool calls but found tool output")
237
238 if tool_call_order == 1:
239 prompt += "\n\n<function_results>"
@@ -266,9 +269,10 @@ def render_message(
269 summary_content = content or ""
270
271 if thinking_mode == "thinking" and index > last_user_idx:
269 - assert reasoning_content or tool_calls, (
270 - f"ThinkingMode: {thinking_mode}, invalid message without reasoning_content/tool_calls `{msg}` after last user message"
271 - )
272 + if not (reasoning_content or tool_calls):
273 + raise ValueError(
274 + f"ThinkingMode: {thinking_mode}, invalid message without reasoning_content/tool_calls `{msg}` after last user message"
275 + )
276 thinking_part = (
277 thinking_template.format(reasoning_content=reasoning_content or "")
278 + thinking_end_token
@@ -362,12 +366,14 @@ def parse_tool_calls(index: int, text: str):
366 index, _, stop_token = _read_until_stop(
367 index, text, [f"<{dsml_token}invoke", tool_calls_end_token]
368 )
365 - assert _ == ">\n", "Tool call format error"
369 + if _ != ">\n":
370 + raise RuntimeError("Tool call format error")
371
372 if stop_token == tool_calls_end_token:
373 break
374
370 - assert stop_token is not None, "Missing special token"
375 + if stop_token is None:
376 + raise RuntimeError("Missing special token")
377
378 index, tool_name_content, stop_token = _read_until_stop(
379 index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]
@@ -376,7 +382,8 @@ def parse_tool_calls(index: int, text: str):
382 p_tool_name = re.findall(
383 r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL
384 )
379 - assert len(p_tool_name) == 1, "Tool name format error"
385 + if len(p_tool_name) != 1:
386 + raise RuntimeError("Tool name format error")
387 tool_name = p_tool_name[0]
388
389 tool_args: dict[str, tuple[str, str]] = {}
@@ -390,16 +397,19 @@ def parse_tool_calls(index: int, text: str):
397 param_content,
398 flags=re.DOTALL,
399 )
393 - assert len(param_kv) == 1, "Parameter format error"
400 + if len(param_kv) != 1:
401 + raise RuntimeError("Parameter format error")
402 param_name, string, param_value = param_kv[0]
403
396 - assert param_name not in tool_args, "Duplicate parameter name"
404 + if param_name in tool_args:
405 + raise RuntimeError("Duplicate parameter name")
406 tool_args[param_name] = (param_value, string)
407
408 index, content, stop_token = _read_until_stop(
409 index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]
410 )
402 - assert content == ">\n", "Parameter format error"
411 + if content != ">\n":
412 + raise RuntimeError("Parameter format error")
413
414 tool_call = decode_dsml_to_arguments(tool_name=tool_name, tool_args=tool_args)
415 tool_calls.append(tool_call)
@@ -422,7 +432,8 @@ def parse_message_from_completion_text(text: str, thinking_mode: str):
432 index, text, [thinking_end_token, tool_calls_start_token]
433 )
434 reasoning_content = content_delta
425 - assert stop_token == thinking_end_token, "Invalid thinking format"
435 + if stop_token != thinking_end_token:
436 + raise RuntimeError("Invalid thinking format")
437
438 index, content_delta, stop_token = _read_until_stop(
439 index, text, [eos_token, tool_calls_start_token]
@@ -431,17 +442,18 @@ def parse_message_from_completion_text(text: str, thinking_mode: str):
442 if stop_token == tool_calls_start_token:
443 is_tool_calling = True
444 else:
434 - assert stop_token == eos_token, "Invalid summary format"
445 + if stop_token != eos_token:
446 + raise RuntimeError("Invalid summary format")
447
448 if is_tool_calling:
449 index, stop_token, tool_calls = parse_tool_calls(index, text)
450
451 index, tool_ends_text, stop_token = _read_until_stop(index, text, [eos_token])
440 - assert not tool_ends_text, "Unexpected content after tool calls"
452 + if tool_ends_text:
453 + raise RuntimeError("Unexpected content after tool calls")
454
442 - assert len(text) == index and stop_token in [eos_token, None], (
443 - "Unexpected content at end"
444 - )
455 + if not (len(text) == index and stop_token in [eos_token, None]):
456 + raise RuntimeError("Unexpected content at end")
457
458 for sp_token in [
459 bos_token,
@@ -450,9 +462,8 @@ def parse_message_from_completion_text(text: str, thinking_mode: str):
462 thinking_end_token,
463 dsml_token,
464 ]:
453 - assert sp_token not in summary_content and sp_token not in reasoning_content, (
454 - "Unexpected special token in content"
455 - )
465 + if sp_token in summary_content or sp_token in reasoning_content:
466 + raise RuntimeError("Unexpected special token in content")
467
468 return {
469 "role": "assistant",