Tool Calling Failure on a Domestic AI Accelerator: Debugging a Model That Returns Plain Text Instead of Structured Calls

    This article walks through a real debugging session in which a tool-capable open-source model deployed on a domestic AI accelerator stopped producing structured tool calls and returned plain text instead. It explains the two independent chains that tool calling depends on—prompt construction through the chat template and output parsing in the inference framework—and shows how a custom template instruction that asked for JSON code blocks conflicted with the model's native special-token contract. The article covers the root-cause analysis, the minimal-change fix that restored structured output, and the deeper issues of adaptation maturity, image layering, contract testing, observability, and canary rollback, closing with a practical checklist for keeping agent capabilities stable on non-mainstream compute platforms.

    1. Symptom: Tool Calling Degrades to Plain Text

    In an environment built around a domestic AI accelerator, a team tried to deploy an open-source model with tool-calling support. The service started normally, the inference endpoint returned results, and general question-answering quality showed no clear regression. But as soon as a request included a tool list, the model’s output turned into ordinary text: the portion that the upper-layer framework should have recognized as a structured call was passed straight through to the business side as natural language.

    The behavior boiled down to three points:

    • No structured fields were generated; the business side received a string rather than a parseable object.
    • The output often carried code block fences, explanatory sentences, and even prefixes or suffixes such as “I will call the following tool.”
    • The same prompt behaved correctly on another compute environment (a general-purpose GPU), which indicated that the model weights themselves were not corrupted.

    This symptom is easy to misdiagnose as “insufficient model capability” or a “quantization precision problem.” Comparison experiments, however, point to something else: the contract between the chat template and the inference framework’s built-in parsing logic had been broken.

    2. Key Concept: Tool Calling Relies on Two Independent Chains

    To understand the problem, it helps to split tool calling into two chains:

    1. Prompt construction chain: handled by the chat template, which concatenates the system prompt, the tool list (names, descriptions, parameter schemas), and message history into an input sequence the model can understand.
    2. Output parsing chain: handled by the tool parser in the inference framework, which uses predefined markers or regex rules to extract the tool name and arguments from the generated text.

    The two chains must “speak the same language.” A native model typically relies on a set of special tokens to wrap tool calls, and the parser matches against those tokens. Once the template changes the output convention, the parser can no longer understand the output, and the structured intent is degraded to plain text.

    3. Root Cause: An Out-of-Bounds Instruction in a Custom Template

    The investigation found that the custom chat template in use added an explicit constraint requiring the model to “output tool calls in a json code block.” This pattern is common in pure prompt engineering. The problem is that it conflicts with the native special-token mechanism:

    1. # Problematic template fragment (illustrative)
    2. If you need to use a tool, output ```json
    3. {"name": "...", "arguments": {...}}
    1. The parser's matching rule, by contrast, targets the native marker sequence, as shown below:
    2. ```python
    3. # Parser matching logic (illustrative, not a real API)
    4. pattern = r"<tool_call>\s*(\{.*?\})\s*</tool_call>"
    5. match = re.search(pattern, text, re.S)
    6. if not match:
    7. return None # Parsing failed; the entire text is treated as a normal reply

    The regex does not match → an empty result is returned → the upper-layer framework handles the output as plain text. That is the direct cause of the “tool calling failure,” not an inference error on the compute platform itself.

    4. The Fix: Converging the Template on the Standard Contract

    The fix followed the principle of “minimal changes, restore capability first, optimize later”:

    1. Remove all explicit instructions about output format from the template, including code block fences, field naming requirements, and prefix/suffix phrasing.
    2. Keep only the tool list injection, ensuring that tool descriptions and parameter schemas are passed in completely.
    3. Keep special tokens from being escaped into text (avoid escaping them into visible characters during template rendering).
    4. Run a regression check with a minimal test case.

    Illustrative test case:

    1. req = {
    2. "messages": [{"role": "user", "content": "Query tomorrow's weather"}],
    3. "tools": [{"type": "function",
    4. "function": {"name": "get_weather",
    5. "parameters": {"type": "object",
    6. "properties": {"city": {"type": "string"}}}}}]
    7. }
    8. # Expected: a structured tool call object, not plain text containing code block fences

    After the change, the structured call was extracted correctly and normal orchestration resumed on the business side.

    5. Deeper Cause: Adaptation Layers and Image Governance

    On the surface this was a template problem. Underneath, it was a question of adaptation maturity: support for open-source model ecosystems on domestic compute versions often lags behind model iteration, and the inference framework version, operator libraries, template, and parser need to be matched as a set. Patching things piecemeal makes the environment irreproducible, so the following practices are recommended:

    • Image layering: separate the base operator layer, the inference framework layer, and the model and template layer, with versions locked layer by layer.
    • Contract testing: make “tool calls are parseable” a mandatory pre-release check, covering both streaming and non-streaming paths.
    • Observability: monitor and alert on parse failure rates and empty parse results instead of waiting for business complaints.
    • Canary and rollback: roll out a new image on a single node first, and keep the previous image version for fast rollback.

    6. Practical Checklist

    • Templates should only concatenate; they should not lecture on formatting.
    • Manage special tokens and parsing rules as a pair; if you change one, test the other.
    • Cross-environment comparison is the fastest way to locate adaptation problems.
    • Turn environment differences into images and documentation rather than individual experience.

    In short, when tool calling is emitted as plain text, the model has usually not become “dumber”—the template and the parsing contract have failed to match. Converging the template and adding contract testing and image governance, as described above, can deliver usable agent capabilities in a stable way on domestic compute platforms.