[GH-ISSUE #3008] [Issue]: A JSON array of tool calls wrapped in a <tool_call> tag is silently dropped by the textual parser #1580

Closed
opened 2026-10-21 05:39:41 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3008

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

When a non-native-function-calling model emits multiple tool calls as a JSON ARRAY inside a single tool-call tag, e.g. <tool_call>[{"name":"roll_dice","arguments":{}},{"name":"get_weather","arguments":{}}]</tool_call>, every call is silently discarded and the turn produces zero tool calls. The tagged-snippet path only ever produces a single payload object per snippet; it has no array branch. snippetToPayload() runs parseJsonishObject() on the snippet, which returns null for arrays; the name{...} regex does not match [...]; and the mari-command fallback also returns null. So the snippet yields null and all calls are lost. The top-level-array recovery branch does not help because the content begins with <, not [.

How to reproduce (try it yourself)

  1. Use a model routed through the OpenAI-compatible provider with no native tool_calls in the response and at least one tool enabled.
  2. Have it return content exactly <tool_call>[{"name":"roll_dice","arguments":{}},{"name":"get_weather","arguments":{}}]</tool_call>.

You'll see: parseTextualToolCalls(content, options.tools) returns [] (0 tool calls); the agent does nothing and the raw tagged text is dropped. Confirmed by line-by-line reproduction of the parser (count 0); the same <|tool_call|> variant is also affected (count 0). Control cases work: a single object in a tag → count 1; a bare top-level array → count 2.

Expected: both tool calls in the array are parsed and executed.

Where it is

packages/server/src/services/llm/textual-tool-call-parser.ts: snippetToPayload at lines 111-134 (single-object only), consumed in the loop at lines 217-225 of parseTextualToolCalls. Reachable from packages/server/src/services/llm/providers/openai.provider.ts:1259 (non-streaming) and :1427 (streaming).

What's happening

snippetToPayload handles only a single object payload or a single name{...} command; it never destructures a top-level JSON array of tool calls. The array-recovery branch in parseTextualToolCalls is gated on trimmed.startsWith('['), so an array nested inside a tag never reaches it.

Suggested fix

Handle the array-in-tag case in the consumer loop of parseTextualToolCalls (mirroring the existing top-level-array branch), so an array of tool calls inside a tag is expanded instead of dropped. Replace the loop at lines 217-225:

parseTaggedSnippets(content).forEach((snippet, index) => {
  const snippetText = snippet.text.includes('<|"|>') ? normalizeGemma4Delimiters(snippet.text) : snippet.text;
  if (snippetText.trim().startsWith("[")) {
    try {
      const arr = JSON.parse(snippetText);
      if (Array.isArray(arr)) {
        arr.forEach((raw) => {
          const call = toolCallFromRaw(raw, calls.length, knownTools, hasBashTool);
          if (call) calls.push(call);
        });
        return;
      }
    } catch { /* not a JSON array — fall through */ }
  }
  const payload = snippetToPayload(snippetText, {
    allowCommandFallback: snippet.allowCommandFallback,
    allowAnonymousJsonPayload: snippet.allowAnonymousJsonPayload,
  });
  const call = toolCallFromRaw(payload, index, knownTools, hasBashTool);
  if (call) calls.push(call);
});

The array check runs before snippetToPayload (which returns null for arrays); elements go straight through toolCallFromRaw so knownTools/bash gating and the existing allowAnonymousJsonPayload semantics are preserved (anonymous array elements are not turned into mari_db calls, matching the top-level-array branch); invalid JSON or non-array snippets fall through to the unchanged single-payload path.

Regression avoided: implementing this in the consumer loop (rather than making snippetToPayload emit multiple payloads) fits the existing one-snippet→one-call wiring; otherwise all-but-one element would still be dropped. Per-element ids use calls.length and toolCallId appends a random suffix, so ids stay unique.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3008 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When a non-native-function-calling model emits multiple tool calls as a JSON ARRAY inside a single tool-call tag, e.g. `<tool_call>[{"name":"roll_dice","arguments":{}},{"name":"get_weather","arguments":{}}]</tool_call>`, every call is silently discarded and the turn produces zero tool calls. The tagged-snippet path only ever produces a single payload object per snippet; it has no array branch. `snippetToPayload()` runs `parseJsonishObject()` on the snippet, which returns null for arrays; the `name{...}` regex does not match `[...]`; and the mari-command fallback also returns null. So the snippet yields null and all calls are lost. The top-level-array recovery branch does not help because the content begins with `<`, not `[`. **How to reproduce (try it yourself)** 1. Use a model routed through the OpenAI-compatible provider with no native `tool_calls` in the response and at least one tool enabled. 2. Have it return content exactly `<tool_call>[{"name":"roll_dice","arguments":{}},{"name":"get_weather","arguments":{}}]</tool_call>`. You'll see: `parseTextualToolCalls(content, options.tools)` returns `[]` (0 tool calls); the agent does nothing and the raw tagged text is dropped. Confirmed by line-by-line reproduction of the parser (count 0); the same `<|tool_call|>` variant is also affected (count 0). Control cases work: a single object in a tag → count 1; a bare top-level array → count 2. Expected: both tool calls in the array are parsed and executed. **Where it is** `packages/server/src/services/llm/textual-tool-call-parser.ts`: `snippetToPayload` at lines 111-134 (single-object only), consumed in the loop at lines 217-225 of `parseTextualToolCalls`. Reachable from `packages/server/src/services/llm/providers/openai.provider.ts:1259` (non-streaming) and `:1427` (streaming). **What's happening** `snippetToPayload` handles only a single object payload or a single `name{...}` command; it never destructures a top-level JSON array of tool calls. The array-recovery branch in `parseTextualToolCalls` is gated on `trimmed.startsWith('[')`, so an array nested inside a tag never reaches it. **Suggested fix** Handle the array-in-tag case in the consumer loop of `parseTextualToolCalls` (mirroring the existing top-level-array branch), so an array of tool calls inside a tag is expanded instead of dropped. Replace the loop at lines 217-225: ```ts parseTaggedSnippets(content).forEach((snippet, index) => { const snippetText = snippet.text.includes('<|"|>') ? normalizeGemma4Delimiters(snippet.text) : snippet.text; if (snippetText.trim().startsWith("[")) { try { const arr = JSON.parse(snippetText); if (Array.isArray(arr)) { arr.forEach((raw) => { const call = toolCallFromRaw(raw, calls.length, knownTools, hasBashTool); if (call) calls.push(call); }); return; } } catch { /* not a JSON array — fall through */ } } const payload = snippetToPayload(snippetText, { allowCommandFallback: snippet.allowCommandFallback, allowAnonymousJsonPayload: snippet.allowAnonymousJsonPayload, }); const call = toolCallFromRaw(payload, index, knownTools, hasBashTool); if (call) calls.push(call); }); ``` The array check runs before `snippetToPayload` (which returns null for arrays); elements go straight through `toolCallFromRaw` so `knownTools`/bash gating and the existing `allowAnonymousJsonPayload` semantics are preserved (anonymous array elements are not turned into `mari_db` calls, matching the top-level-array branch); invalid JSON or non-array snippets fall through to the unchanged single-payload path. *Regression avoided:* implementing this in the consumer loop (rather than making `snippetToPayload` emit multiple payloads) fits the existing one-snippet→one-call wiring; otherwise all-but-one element would still be dropped. Per-element ids use `calls.length` and `toolCallId` appends a random suffix, so ids stay unique. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in merged staging PR #3018.

<!-- gh-comment-id:4833017058 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in merged staging PR #3018.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1580
No description provided.