DeepSeek model output sometimes contains DSML tool-call markup

#209
by Sube126 - opened

DeepSeek model output sometimes contains DSML tool-call markup, but the API/client does not parse it into structured tool_calls. Instead, the whole assistant message is returned as plain text in message.content.

Example returned content:

I will search several relevant directions first.<||DSML||tool_calls><||DSML||invoke name="doc_knowlegebase"><||DSML||parameter name="query" string="true">bond detail fields display</||DSML||parameter></||DSML||invoke></||DSML||tool_calls>

Expected behavior:

The DSML block should be parsed into a structured tool call, for example:

{
  "tool_calls": [
    {
      "type": "function",
      "function": {
        "name": "doc_knowlegebase",
        "arguments": "{\"query\":\"bond detail fields display\"}"
      }
    }
  ]
}

Actual behavior:

The DSML block is returned inside normal assistant text content, so downstream code receives it as a plain string and must manually parse it.

Possible issue:

The emitted DSML token appears degraded from the documented full-width format:

<|DSML|tool_calls>

to an ASCII double-pipe-like form:

<||DSML||tool_calls>

Also, the markup may be emitted without the newlines expected by the reference parser. This likely prevents the tool-call parser from recognizing the DSML block.

Suggested fix:

The official parser/client should either:

  1. Normalize DSML token variants such as <||DSML||...> back to <|DSML|...>, and tolerate compact markup without newlines.
  2. Or ensure the model/API always emits the documented canonical DSML format before tool-call parsing.

This is how I handled it.

import json
import re
import sys
from pathlib import Path

sys.path.insert(0, str(Path("encoding").resolve()))
from encoding_dsv4 import eos_token, parse_message_from_completion_text

DSML_TOKEN = "|DSML|"


def normalize_dsml_token_variants(text: str) -> str:
    """Convert degraded DSML tokens like <||DSML||tag> to canonical DSML tokens."""
    double_pipe = r"(?:\|\s*\|||\s*|)"

    text = re.sub(
        rf"<\s*(/?)\s*{double_pipe}\s*DSML\s*{double_pipe}\s*([A-Za-z_][\w]*)\s*>",
        lambda match: f"<{match.group(1)}{DSML_TOKEN}{match.group(2)}>",
        text,
    )
    text = re.sub(
        rf"<\s*(/?)\s*{double_pipe}\s*DSML\s*{double_pipe}\s*([A-Za-z_][\w]*)\s+",
        lambda match: f"<{match.group(1)}{DSML_TOKEN}{match.group(2)} ",
        text,
    )
    return text


def normalize_dsml_message(text: str) -> str:
    """Normalize compact DSML tool calls into the strict parser format."""
    text = normalize_dsml_token_variants(text)

    tool_calls_start = f"<{DSML_TOKEN}tool_calls>"
    tool_calls_end = f"</{DSML_TOKEN}tool_calls>"

    start_index = text.find(tool_calls_start)
    if start_index == -1:
        return text if text.endswith(eos_token) else f"{text}{eos_token}"

    end_index = text.find(tool_calls_end, start_index)
    if end_index == -1:
        raise ValueError("Missing DSML tool_calls closing tag")

    end_index += len(tool_calls_end)
    content = text[:start_index].rstrip()
    tool_calls = text[start_index:end_index].strip()
    tail = text[end_index:].strip()

    if tail and tail != eos_token:
        raise ValueError(f"Unexpected content after DSML tool calls: {tail}")

    escaped_dsml_token = re.escape(DSML_TOKEN)
    tool_calls = re.sub(rf"(<{escaped_dsml_token}tool_calls>)\s*", r"\1\n", tool_calls)
    tool_calls = re.sub(rf">\s*<{escaped_dsml_token}invoke", f">\n<{DSML_TOKEN}invoke", tool_calls)
    tool_calls = re.sub(rf">\s*<{escaped_dsml_token}parameter", f">\n<{DSML_TOKEN}parameter", tool_calls)
    tool_calls = re.sub(
        rf"</{escaped_dsml_token}parameter>\s*<{escaped_dsml_token}parameter",
        f"</{DSML_TOKEN}parameter>\n<{DSML_TOKEN}parameter",
        tool_calls,
    )
    tool_calls = re.sub(
        rf"</{escaped_dsml_token}parameter>\s*</{escaped_dsml_token}invoke>",
        f"</{DSML_TOKEN}parameter>\n</{DSML_TOKEN}invoke>",
        tool_calls,
    )
    tool_calls = re.sub(
        rf"</{escaped_dsml_token}invoke>\s*<{escaped_dsml_token}invoke",
        f"</{DSML_TOKEN}invoke>\n<{DSML_TOKEN}invoke",
        tool_calls,
    )
    tool_calls = re.sub(
        rf"</{escaped_dsml_token}invoke>\s*</{escaped_dsml_token}tool_calls>",
        f"</{DSML_TOKEN}invoke>\n</{DSML_TOKEN}tool_calls>",
        tool_calls,
    )

    return f"{content}\n\n{tool_calls}{eos_token}"


def parse_deepseek_message(text: str) -> dict:
    normalized_text = normalize_dsml_message(text)
    return parse_message_from_completion_text(normalized_text, thinking_mode="chat")


if __name__ == "__main__":
    completion = """我先并行搜索几个关键方向:SKILL文件、六扇门/七巧板相关需求文档、公司债券要素以及定期报告提醒相关内容。<||DSML||tool_calls><||DSML||invoke name="doc_knowlegebase"><||DSML||parameter name="query" string="true">七巧板 需求 债券 详情 字段 展示</||DSML||parameter></||DSML||invoke></||DSML||tool_calls>"""

    parsed = parse_deepseek_message(completion)

    tool_calls = parsed["tool_calls"]
    print(json.dumps(tool_calls, ensure_ascii=False, indent=2))

    args = json.loads(tool_calls[0]["function"]["arguments"])
    print(args)

Sign up or log in to comment