Skip to content

vllm.tokenizers.deepseek_v41_encoding

Text encoding adapted from the DeepSeek V4.1 reference encoder.

Differences from the reference: - developer messages are supported and render as user turns. - Images arrive as placeholders; deepseek_v41.py flattens content first. - Tool namespaces (ns::name) are not supported. - A system message's updated_tools renders as deepseek-recipe's "### Updated Tool Schemas" (tool search results).

Message normalization and serving controls live in deepseek_v41.py.

Functions:

_drop_thinking_messages(messages)

Drop reasoning_content and non-essential messages before the last user message. Same as V4, but uses the V4.1 last-user definition (mid systems count).

encode_arguments_to_dsml(tool_call)

Encode tool call arguments into V4.1 DSML parameter format.

encode_messages(messages, thinking_mode, context=None, drop_thinking=True, add_default_bos_token=True, reasoning_effort=None, image_order=None, add_generation_prompt=True, continue_final_message=False)

Encode preprocessed (text-only) messages into the V4.1 prompt format.

find_last_user_index(messages)

Find the index of the last user/developer message.

V4.1 supports mid-conversation system messages, which count as user messages for the purposes of the assistant generation header. User messages containing only tool results do not start a new turn.

merge_tool_messages(messages)

Merge tool messages into the preceding user message using content_blocks format.

DeepSeek-V4.1 does not have a standalone "tool" role; instead, tool results are encoded as blocks within user messages.

parse_tool_call_arguments(arguments)

Parse tool call arguments into the dict the encoder renders.

Tolerates JSON strings, including double-encoded ones, and wraps anything that is not an object as {"arguments": }. The V4.1 tokenizer escapes the keys and strings of the result before rendering, with this same function.

render_message(index, messages, thinking_mode, drop_thinking=True, reasoning_effort=None, image_order=None, add_generation_prompt=True, continue_final_message=False)

Render a single message at the given index into its V4.1 encoded string form.

render_reasoning_effort(index, thinking_mode, effort)

Render the V4.1 numeric reasoning effort prefix (thinking mode, index 0 only).

render_tools(tools)

Render tool schemas into the V4.1 system prompt format.

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.

to_json(value)

Serialize a value to JSON string.

tool_calls_from_openai_format(tool_calls)

Convert OpenAI-format tool calls to internal format.

tools_from_openai_format(tools)

Extract function definitions from OpenAI-format tool list.