vllm.tokenizers.deepseek_v4_encoding ¶
DeepSeek-V4 Encoding
A self-contained implementation for encoding DeepSeek-V4 chat messages with tool calling, thinking mode, and quick instruction task support.
Functions:
-
encode_arguments_to_dsml–Encode tool call arguments into DSML parameter format.
-
encode_messages–Encode a list of messages into the DeepSeek-V4 prompt format.
-
find_last_user_index–Find the index of the last user/developer message.
-
merge_tool_messages–Merge tool messages into the preceding user message using content_blocks format.
-
render_message–Render a single message at the given index into its encoded string form.
-
render_tools–Render tool schemas into the system prompt format.
-
sort_tool_results_by_call_order–Sort tool_result blocks within user messages by the order of tool_calls
-
to_json–Serialize a value to JSON string.
-
tool_calls_from_openai_format–Convert OpenAI-format tool calls to internal format.
-
tools_from_openai_format–Extract function definitions from OpenAI-format tool list.
_drop_thinking_messages(messages) ¶
Drop reasoning and non-essential messages before the last user message.
Behavior: - Messages with role in ["user", "system", "tool", "latest_reminder"] are always kept. - Messages at or after the last user index are always kept. - Assistant messages before the last user get reasoning removed. - Developer messages before the last user are dropped entirely.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
encode_arguments_to_dsml(tool_call) ¶
Encode tool call arguments into DSML parameter format.
Parameters:
Returns:
-
str–DSML-formatted parameter string.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
encode_messages(messages, thinking_mode, context=None, drop_thinking=True, add_default_bos_token=True, reasoning_effort=None) ¶
Encode a list of messages into the DeepSeek-V4 prompt format.
This is the main entry point for encoding conversations. It handles: - BOS token insertion - Thinking mode with optional reasoning content dropping - Tool message merging into user messages - Multi-turn conversation context
Parameters:
-
(messages¶List[Dict[str, Any]]) –List of message dicts to encode.
-
(thinking_mode¶str) –Either "chat" or "thinking".
-
(context¶Optional[List[Dict[str, Any]]], default:None) –Optional preceding context messages (already encoded prefix).
-
(drop_thinking¶bool, default:True) –If True, drop reasoning from earlier assistant turns (only keep reasoning for messages after the last user message).
-
(add_default_bos_token¶bool, default:True) –Whether to prepend BOS token at conversation start.
-
(reasoning_effort¶Optional[str], default:None) –Optional reasoning effort level ("max", "high", or None).
Returns:
-
str–The encoded prompt string.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
find_last_user_index(messages) ¶
Find the index of the last user/developer message.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
merge_tool_messages(messages) ¶
Merge tool messages into the preceding user message using content_blocks format.
DeepSeek-V4 does not have a standalone "tool" role; instead, tool results are encoded as
This function converts a standard OpenAI-format conversation (with separate "tool" role messages) into V4 format where tool results are merged into user messages.
Parameters:
Returns:
Source code in vllm/tokenizers/deepseek_v4_encoding.py
render_message(index, messages, thinking_mode, drop_thinking=True, reasoning_effort=None) ¶
Render a single message at the given index into its encoded string form.
This is the core function that converts each message in the conversation into the DeepSeek-V4 format.
Parameters:
-
(index¶int) –Index of the message to render.
-
(messages¶List[Dict[str, Any]]) –Full list of messages in the conversation.
-
(thinking_mode¶str) –Either "chat" or "thinking".
-
(drop_thinking¶bool, default:True) –Whether to drop reasoning content from earlier turns.
-
(reasoning_effort¶Optional[str], default:None) –Optional reasoning effort level ("max", "high", or None).
Returns:
-
str–Encoded string for this message.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
render_tools(tools) ¶
Render tool schemas into the system prompt format.
Parameters:
-
(tools¶List[Dict[str, Union[str, Dict[str, Any]]]]) –List of tool schema dicts (each with name, description, parameters).
Returns:
-
str–Formatted tools section string.
Source code in vllm/tokenizers/deepseek_v4_encoding.py
sort_tool_results_by_call_order(messages) ¶
Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message.
Parameters:
Returns:
Source code in vllm/tokenizers/deepseek_v4_encoding.py
to_json(value) ¶
tool_calls_from_openai_format(tool_calls) ¶
Convert OpenAI-format tool calls to internal format.