Tool Calling Failures on Ascend NPUs: Troubleshooting Structured Output That Degrades to Plain Text

    This article explains why tool calls sometimes print as plain text instead of structured call objects when deploying open-source LLMs on Ascend NPUs. It walks through the four-stage recognition chain (tool list injection, special token generation, parser matching, result feedback), traces the root cause to non-standard output-format instructions in a custom chat template, and shows the minimal-intervention fix. It also covers regression test cases, adaptation gaps in heterogeneous compute environments, and a reusable checklist for diagnosing silent structured-output failures.

    1. Symptom: Tool Calls Degrade into Ordinary Text

    On a domestic accelerator card (an inference device with roughly 64GB of memory per card), a recent version of an inference framework was used to deploy an open-source large language model with function-calling support. Inference itself worked fine: output was fluent, with no garbled characters and no truncation. However, structured output that should have been recognized as a tool call was printed verbatim as plain text.

    Specifically, the model clearly “wanted” to call a tool. The field names, parameter names, and parameter values were all present, but the outer wrapper was a Markdown code block plus a sentence of natural-language explanation, rather than the structured call signal the framework expects. Upstream business logic therefore never received an executable call object, and the agent workflow broke at the very first step. The logs contained no parsing exception and no error stack trace. This kind of silent failure is harder to diagnose than an outright crash.

    It is worth stressing that this behavior has nothing to do with compute capacity, quantization precision, or memory size. It is a classic output protocol mismatch: the model’s form of expression and the inference framework’s parsing rules are not aligned.

    2. First, Get the Mental Model Right: How Tool Calls Are Actually “Recognized”

    Many investigations take a long detour because they assume tool calling means “the model emits a piece of JSON and the framework runs a JSON parser.” The real path is much longer and typically involves four stages:

    1. Tool list injection: The framework assembles the names, descriptions, and parameter schemas of available tools into the prompt in a specific format, so the model “knows which tools are available.”
    2. Model decision and special token generation: When a call is needed, the model generates a set of special tokens agreed upon during pre-training (usually invisible control sequences) that wrap the function name and arguments.
    3. Parser matching on the framework side: The built-in parser uses a state machine or regular expressions to match those special tokens, “cut” them out of the plain text, and convert them into a structured call object.
    4. Execution and feedback on the business side: The execution result is fed back as a new context fragment and enters the next round of inference.

    The key point: stage 2 and stage 3 must follow the same convention. The model’s native special tokens are fixed during training, while the parser is reverse-engineered by the framework authors from the official template. Replace any link in that chain and it breaks immediately, often without raising an error.

    3. Root Cause: A “Well-Intentioned” Instruction in a Custom Template

    Layer-by-layer investigation traced the problem to a custom chat template. In order to “make the model’s output more standardized,” the template added an explicit instruction requiring the model to “return tool calls in JSON code block format.” That instruction looks safe but is destructive in two ways:

    • It overrides the native control tokens: The model abandons the special sequences fixed during pre-training and instead wraps JSON in a Markdown code block. From the model’s perspective this is obedience; from the parser’s perspective it is unidentified text.
    • It bypasses the parser’s matching path: The built-in parser’s regular expressions only recognize the native token form. Code block fences and natural-language prefixes fall outside the matching scope, so the entire output is passed through as an ordinary answer.

    A minimal illustration makes the mismatch clear:

    1. # Form the parser expects (native control tokens, illustrative)
    2. <tool_call>{"name": "get_weather", "arguments": {"city": "..."}}</tool_call>
    3. # Form induced by the template (what actually happens)
    4. ```json
    5. {"name": "get_weather", "arguments": {"city": "..."}}

    The model says: “Sure, I will call the following tool…”
    ```

    The two are semantically identical but completely non-equivalent at the protocol layer.

    4. Fix: Reduce the Template to “Minimal Intervention”

    The fix is a single step: remove every non-standard instruction about output format from the template, keeping only tool list injection and the necessary role constraints, so the model returns to its native expression path.

    After the change, validation should cover the following categories of cases to avoid “fixing one scenario and breaking another”:

    • Single tool with a single parameter;
    • Single tool with multiple parameters and nested object parameters;
    • Parallel calls to multiple tools;
    • Replying in natural language when no suitable tool exists, instead of forcing a call;
    • Tool result feedback across multiple turns of context;
    • Whether fragment concatenation is complete under streaming output.

    It is also advisable to put the template under version control and run the full case set on every change. Templates are the place in prompt engineering where it is easiest to “add one more line,” and also the hardest source of failures to trace back.

    5. A Deeper Issue: Adaptation Gaps in Heterogeneous Compute Environments

    The template problem is only the surface. The more noteworthy conclusion is that the domestic accelerator card stack still lags in adaptation completeness for open-source models, especially for newly released model architectures and their accompanying templates and parser implementations, which often trail the community mainline.

    This creates three common risks:

    • Missing or mismatched parsers: The framework version supports the model, but the corresponding parsing logic was not shipped in sync, so structured output cannot be recognized;
    • Insufficient operator and kernel coverage: Some operators in the new architecture lack high-performance implementations, and falling back to the generic path causes a substantial performance drop;
    • Tight coupling of dependencies and drivers: The accelerator runtime, communication libraries, and framework are strongly version-coupled, so casual upgrades can introduce unexpected regressions.

    In practice, the safer approach is to build an adaptation image and pin the version combination: package the driver runtime, framework, model template, and parser configuration into an immutable image, distribute it uniformly through a container platform, and collect metrics such as parse failure rate, time to first token, and throughput via logging and monitoring with alerting, forming a baseline that supports continuous regression. If model weights and datasets are involved, object storage can be used for versioned retention to keep the environment reproducible.

    6. A Reusable Checklist for Troubleshooting

    For similar “structured output failure” problems, investigate from the outside in:

    1. Inspect the raw output: Disable the parser and print the model’s complete raw text to determine whether the problem is wrong format or wrong content;
    2. Compare the template: Diff line by line against the official reference template, focusing on format instructions, example snippets, and few-shot samples;
    3. Confirm the parser: Check whether the framework has the corresponding parser enabled for this model and whether the parameter is explicitly specified;
    4. Verify at the token layer: Confirm the special tokens are not split or filtered by the tokenizer and not stripped by post-processing;
    5. Rule out sampling interference: Lower the temperature or switch to greedy decoding to rule out randomness causing intermittent failures;
    6. Regress and pin: After the fix, run the full case set and write the environment into the image baseline.

    7. Conclusion

    Tool calls being emitted as plain text is not a model capability problem; it is a broken protocol contract. When deploying open-source models on heterogeneous compute platforms, the template, parser, framework version, and runtime must be governed as a whole. Keeping the template minimally invasive, pinning the adaptation combination into an image, and guarding the regression baseline with observable metrics is what drives the cost of such silent failures down. Teams that want to reduce the burden of environment setup and operations can also evaluate managed inference services and container platform capabilities in the cloud, leaving adaptation and scaling to the platform side.