Skip to content

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:

_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.

encode_arguments_to_dsml(tool_call)

Encode tool call arguments into DSML parameter format.

Parameters:

  • tool_call

    (Dict[str, Any]) –

    Dict with "name" and "arguments" keys.

Returns:

  • str –

    DSML-formatted parameter string.

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 ) –

    Reasoning effort level, one of "low", "high", "max". Only takes effect in thinking mode. None is treated as "low".

Returns:

  • str –

    The encoded prompt string.

find_last_user_index(messages)

Find the index of the last user/developer message.

flatten_content_blocks(content)

Flatten OpenAI-style content blocks to plain text.

Image blocks are inlined as IMAGE_PLACEHOLDER at their position. The image data itself travels out of band (multi_modal_data) and is matched to placeholders by order. Plain-string content passes through unchanged.

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 blocks within user messages.

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:

  • messages

    (List[Dict[str, Any]]) –

    List of message dicts in OpenAI format.

Returns:

  • List[Dict[str, Any]] –

    Processed message list with tool messages merged into user messages.

render_message(index, messages, thinking_mode, drop_thinking=True, reasoning_effort=None, last_user_idx=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 ) –

    Reasoning effort level, one of "low", "high", "max". None is treated as "low".

  • last_user_idx

    (Optional[int], default: None ) –

    Cached index of the last user/developer message.

Returns:

  • str –

    Encoded string for this message.

render_tools(tools)

Render tool schemas into the system prompt format.

Parameters:

Returns:

  • str –

    Formatted tools section string.

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:

  • messages

    (List[Dict[str, Any]]) –

    Preprocessed message list (after merge_tool_messages).

Returns:

  • List[Dict[str, Any]] –

    Message list with sorted tool result blocks.

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.