sgl-project/sglang · error · DS32EncodingError

Tool call format error

Error message

Tool call format error

What it means

While parsing model output, the text after a '<<dsml>>invoke' token must start with '>\n'. parse_tool_calls raises DS32EncodingError('Tool call format error') when the invocation opening is malformed.

Source

Thrown at python/sglang/srt/entrypoints/openai/encoding_dsv32.py:370

    if matched_stop:
        content = text[index:min_pos]
        return min_pos + len(matched_stop), content, matched_stop
    else:
        content = text[index:]
        return len(text), content, None


def parse_tool_calls(index: int, text: str):
    tool_calls: List[Dict[str, Any]] = []
    stop_token = None
    tool_calls_end_token = f"</{dsml_token}function_calls>"

    while index < len(text):
        index, _, stop_token = _read_until_stop(
            index, text, [f"<{dsml_token}invoke", tool_calls_end_token]
        )
        if _ != ">\n":
            raise DS32EncodingError("Tool call format error")

        if stop_token == tool_calls_end_token:
            break

        if stop_token is None:
            raise DS32EncodingError("Missing special token")

        index, tool_name_content, stop_token = _read_until_stop(
            index, text, [f"<{dsml_token}parameter", f"</{dsml_token}invoke"]
        )

        p_tool_name = re.findall(
            r'^\s*name="(.*?)">\n$', tool_name_content, flags=re.DOTALL
        )
        if len(p_tool_name) != 1:
            raise DS32EncodingError("Tool name format error")
        tool_name = p_tool_name[0]

View on GitHub (pinned to 0132848349)

Solutions

  1. Check that the chat template/tool tokens match the deployed model version.
  2. Increase max_tokens so invocations are not truncated.
  3. Retry or treat the completion as plain text if no valid tool call is present.
Defensive patterns

Strategy: try-catch

Try / catch

from sglang.srt.entrypoints.openai.encoding_dsv32 import DS32EncodingError
try:
    calls=parse_tool_calls(text)
except DS32EncodingError:
    calls=None  # treat completion as plain text

Prevention

When it happens

Trigger: Completion text contains a malformed tool invocation, e.g. '<|tool_calls_begin|>invokefunc<|dsml|>...' where the char(s) after 'invoke' are not '>\n' — typically from model hallucination or truncated generation.

Common situations: Model outputs broken DSML when tool schemas confuse it; max_tokens cuts mid-invocation; prompt/template drift between model version and parser tokens.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/f7956ac271e74f76. Report an issue: GitHub.