LocalAI icon indicating copy to clipboard operation
LocalAI copied to clipboard

fix(streaming): prevent glm-4.5 XML auto-detect from emitting Hermes tool_call JSON blob as function name (#9722)

Open Dennisadira opened this issue 6 months ago • 2 comments

Summary

Closes #9722.

When /v1/chat/completions streams a response from a Hermes/NousResearch-format model (output looks like <tool_call>\n{"name":"bash","arguments":{...}}\n</tool_call>), the Go-side XML auto-detector incorrectly picks up the glm-4.5 format and treats the entire JSON blob as the function name.

Root cause: ParseXMLIterative with format=nil tries every XML preset in order. glm-4.5 uses <tool_call> as ToolStart (not a ScopeStart). When it finds <tool_call> but no <arg_key> inside, it falls into the empty-tool-call path that extracts everything between <tool_call> and </tool_call> as the function name. For Hermes output that content is a full JSON object, so Name ends up as '{"name":"bash","arguments":{...}}'. Because the XML branch returned a non-empty result, the JSON fallback (ParseJSONIterative) — which correctly handles Hermes — was suppressed, and the client received a malformed streaming chunk.

Fix

Added filterMalformedXMLToolCalls() that discards any auto-detected FuncCallResult whose Name starts with {. Applied in all three auto-detect branches of ParseXMLIterative:

  1. tryParseXMLFromScopeStart loop (fast path)
  2. TryConsumeXMLToolCalls loop (fallback path)
  3. Partial-exception recovery in the same loop

When all results from a format are filtered, the loop continues to the next format. Hermes output falls through all XML formats and is picked up correctly by ParseJSONIterative.

The user-specified format path (xmlFormat != nil) is intentionally left unfiltered.

Test plan

  • [x] Added ParseXMLIterative regression tests: auto-detect must not return a JSON-blob function name for Hermes-style <tool_call>JSON</tool_call> input (both full and partial streaming).
  • [x] Existing ParseFunctionCall Hermes tests still pass (HaveLen(1), correct name/args).
  • [x] Full pkg/functions test suite: 175/176 specs pass (1 pre-existing pending spec).
  • [x] go build ./core/http/... ./pkg/functions/... — clean.

🤖 Generated with Claude Code

Dennisadira avatar May 22 '26 04:05 Dennisadira

@Dennisadira I'm curious how did you bump into this. That code path is a fallback when autoparser doesn't kick in, or backends do not support chatdeltas via gRPC protocol.

mudler avatar May 22 '26 07:05 mudler

I was tracking down #9722 (the streaming path emitting the same tool_call at multiple index values). While reading through the streaming loop in chat.go to understand which parser fires when, I traced into ParseXMLIterative to map all the auto-detect branches.

To reproduce the Hermes scenario I added a unit test that feeds <tool_call>{"name":"bash","arguments":{...}}</tool_call> directly to ParseXMLIterative with format=nil. The test showed the glm-4.5 preset returning a FuncCallResult with the raw JSON blob as Name — because it matches <tool_call> as ToolStart (not ScopeStart), finds no <arg_key> inside, and falls into the empty-tool-call path.

You're right that in a typical Hermes deployment the C++ autoparser fires first and this XML branch is never reached. But it can trigger when a backend delivers tokens without gRPC chatDeltas (e.g. a custom REST backend), which is the scenario #9722 describes. Fixing it here means the fallback path is also safe.

Dennisadira avatar May 22 '26 07:05 Dennisadira

Thanks for the clear root-cause analysis here — it pinpointed the glm-4.5 auto-detect grabbing a Hermes <tool_call>{json}</tool_call> body as the function name.

We're superseding this with #10059, which generalizes the same idea: instead of dropping only names with a leading {, it validates that an auto-detected tool-call name actually looks like a function name (^[A-Za-z0-9_.\-]+$). Probing ParseXMLIterative on real Hermes-shaped inputs showed the leading-{ check misses a few variants that still mis-extract a garbage name:

<tool_call>…</tool_call> body mis-extracted name leading-{ name-shape
{"name":"bash",…} {"name":…} ✅ ✅
Sure: {…} Sure: {…} ❌ ✅
[{…}] (parallel) [{…}] ❌ ✅
name: bash, … name: bash, … ❌ ✅

Same scope guards as here (auto-detect only; explicitly-forced formats untouched; legitimate glm-4.5 still parses), plus it also covers the non-streaming ParseFunctionCall path. Closing this out in favor of #10059 — credit for the diagnosis is yours.

localai-bot avatar May 29 '26 09:05 localai-bot