nemo_rl.data.deepseek_v4_encoding#

DeepSeek-V4 Encoding

A self-contained implementation for encoding/decoding DeepSeek-V4 chat messages with tool calling, thinking mode, and quick instruction task support.

Module Contents#

Functions#

to_json

Serialize a value to JSON string.

tools_from_openai_format

Extract function definitions from OpenAI-format tool list.

tool_calls_from_openai_format

Convert OpenAI-format tool calls to internal format.

tool_calls_to_openai_format

Convert internal tool calls to OpenAI format.

encode_arguments_to_dsml

Encode tool call arguments into DSML parameter format.

decode_dsml_to_arguments

Decode DSML parameters back to a tool call dict.

render_tools

Render tool schemas into the system prompt format.

find_last_user_index

Find the index of the last user/developer message.

render_message

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

merge_tool_messages

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

sort_tool_results_by_call_order

Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message.

encode_messages

Encode a list of messages into the DeepSeek-V4 prompt format.

_drop_thinking_messages

Drop reasoning and non-essential messages before the last user message.

_read_until_stop

Read text from index until one of the stop strings is found.

parse_tool_calls

Parse DSML tool calls from text starting at the given index.

parse_message_from_completion_text

Parse a model completion text into a structured assistant message.

Data#

API#

nemo_rl.data.deepseek_v4_encoding.bos_token: str#

‘<|begin▁of▁sentence|>’

nemo_rl.data.deepseek_v4_encoding.eos_token: str#

‘<|end▁of▁sentence|>’

nemo_rl.data.deepseek_v4_encoding.thinking_start_token: str#

nemo_rl.data.deepseek_v4_encoding.thinking_end_token: str#

‘’

nemo_rl.data.deepseek_v4_encoding.dsml_token: str#

‘|DSML|’

nemo_rl.data.deepseek_v4_encoding.USER_SP_TOKEN#

‘<|User|>’

nemo_rl.data.deepseek_v4_encoding.ASSISTANT_SP_TOKEN#

‘<|Assistant|>’

nemo_rl.data.deepseek_v4_encoding.LATEST_REMINDER_SP_TOKEN#

‘<|latest_reminder|>’

nemo_rl.data.deepseek_v4_encoding.DS_TASK_SP_TOKENS#

None

nemo_rl.data.deepseek_v4_encoding.VALID_TASKS#

‘set(…)’

nemo_rl.data.deepseek_v4_encoding.system_msg_template: str#

‘{content}’

nemo_rl.data.deepseek_v4_encoding.user_msg_template: str#

‘{content}’

nemo_rl.data.deepseek_v4_encoding.latest_reminder_msg_template: str#

‘{content}’

nemo_rl.data.deepseek_v4_encoding.assistant_msg_template: str#

None

nemo_rl.data.deepseek_v4_encoding.assistant_msg_wo_eos_template: str#

‘{reasoning}{content}{tool_calls}’

nemo_rl.data.deepseek_v4_encoding.thinking_template: str#

‘{reasoning}’

nemo_rl.data.deepseek_v4_encoding.response_format_template: str = <Multiline-String>#
nemo_rl.data.deepseek_v4_encoding.tool_call_template: str = <Multiline-String>#
nemo_rl.data.deepseek_v4_encoding.tool_calls_template = <Multiline-String>#
nemo_rl.data.deepseek_v4_encoding.tool_calls_block_name: str#

‘tool_calls’

nemo_rl.data.deepseek_v4_encoding.tool_output_template: str#

‘<tool_result>{content}</tool_result>’

nemo_rl.data.deepseek_v4_encoding.REASONING_EFFORT_MAX = <Multiline-String>#
nemo_rl.data.deepseek_v4_encoding.TOOLS_TEMPLATE = <Multiline-String>#
nemo_rl.data.deepseek_v4_encoding.to_json(value: Any) str#

Serialize a value to JSON string.

nemo_rl.data.deepseek_v4_encoding.tools_from_openai_format(tools)#

Extract function definitions from OpenAI-format tool list.

nemo_rl.data.deepseek_v4_encoding.tool_calls_from_openai_format(tool_calls)#

Convert OpenAI-format tool calls to internal format.

nemo_rl.data.deepseek_v4_encoding.tool_calls_to_openai_format(tool_calls)#

Convert internal tool calls to OpenAI format.

nemo_rl.data.deepseek_v4_encoding.encode_arguments_to_dsml(
tool_call: Dict[str, Any],
) str#

Encode tool call arguments into DSML parameter format.

Parameters:

tool_call – Dict with “name” and “arguments” keys.

Returns:

DSML-formatted parameter string.

nemo_rl.data.deepseek_v4_encoding.decode_dsml_to_arguments(
tool_name: str,
tool_args: Dict[str, Tuple[str, str]],
) Dict[str, str]#

Decode DSML parameters back to a tool call dict.

Parameters:
  • tool_name – Name of the tool.

  • tool_args – Dict mapping param_name -> (value, is_string_flag).

Returns:

Dict with “name” and “arguments” (JSON string) keys.

nemo_rl.data.deepseek_v4_encoding.render_tools(
tools: List[Dict[str, Union[str, Dict[str, Any]]]],
) str#

Render tool schemas into the system prompt format.

Parameters:

tools – List of tool schema dicts (each with name, description, parameters).

Returns:

Formatted tools section string.

nemo_rl.data.deepseek_v4_encoding.find_last_user_index(
messages: List[Dict[str, Any]],
) int#

Find the index of the last user/developer message.

nemo_rl.data.deepseek_v4_encoding.render_message(
index: int,
messages: List[Dict[str, Any]],
thinking_mode: str,
drop_thinking: bool = True,
reasoning_effort: Optional[str] = None,
) str#

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 – Index of the message to render.

  • messages – Full list of messages in the conversation.

  • thinking_mode – Either “chat” or “thinking”.

  • drop_thinking – Whether to drop reasoning content from earlier turns.

  • reasoning_effort – Optional reasoning effort level (“max”, “high”, or None).

Returns:

Encoded string for this message.

nemo_rl.data.deepseek_v4_encoding.merge_tool_messages(
messages: List[Dict[str, Any]],
) List[Dict[str, Any]]#

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 <tool_result> 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 of message dicts in OpenAI format.

Returns:

Processed message list with tool messages merged into user messages.

nemo_rl.data.deepseek_v4_encoding.sort_tool_results_by_call_order(
messages: List[Dict[str, Any]],
) List[Dict[str, Any]]#

Sort tool_result blocks within user messages by the order of tool_calls in the preceding assistant message.

Parameters:

messages – Preprocessed message list (after merge_tool_messages).

Returns:

Message list with sorted tool result blocks.

nemo_rl.data.deepseek_v4_encoding.encode_messages(
messages: List[Dict[str, Any]],
thinking_mode: str,
context: Optional[List[Dict[str, Any]]] = None,
drop_thinking: bool = True,
add_default_bos_token: bool = True,
reasoning_effort: Optional[str] = None,
) str#

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 of message dicts to encode.

  • thinking_mode – Either “chat” or “thinking”.

  • context – Optional preceding context messages (already encoded prefix).

  • drop_thinking – If True, drop reasoning from earlier assistant turns (only keep reasoning for messages after the last user message).

  • add_default_bos_token – Whether to prepend BOS token at conversation start.

  • reasoning_effort – Optional reasoning effort level (“max”, “high”, or None).

Returns:

The encoded prompt string.

nemo_rl.data.deepseek_v4_encoding._drop_thinking_messages(
messages: List[Dict[str, Any]],
) List[Dict[str, Any]]#

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.

nemo_rl.data.deepseek_v4_encoding._read_until_stop(
index: int,
text: str,
stop: List[str],
) Tuple[int, str, Optional[str]]#

Read text from index until one of the stop strings is found.

Returns:

Tuple of (new_index, content_before_stop, matched_stop_string_or_None).

nemo_rl.data.deepseek_v4_encoding.parse_tool_calls(
index: int,
text: str,
) Tuple[int, Optional[str], List[Dict[str, str]]]#

Parse DSML tool calls from text starting at the given index.

Parameters:
  • index – Starting position in text.

  • text – The full text to parse.

Returns:

Tuple of (new_index, last_stop_token, list_of_tool_call_dicts). Each tool call dict has “name” and “arguments” keys.

nemo_rl.data.deepseek_v4_encoding.parse_message_from_completion_text(
text: str,
thinking_mode: str,
) Dict[str, Any]#

Parse a model completion text into a structured assistant message.

This function takes the raw text output from the model (a single assistant turn) and extracts:

  • reasoning (thinking block)

  • content (summary/response)

  • tool_calls (if any)

NOTE: This function is designed to parse only correctly formatted strings and will raise ValueError for malformed output.

Parameters:
  • text – The raw completion text (including EOS token).

  • thinking_mode – Either “chat” or “thinking”.

Returns:

“role”, “content”, “reasoning”, “tool_calls”. tool_calls are in OpenAI format.

Return type:

Dict with keys