Skip to content

Add multi-format tool call parser (bracket, raw JSON)#15

Merged
waybarrios merged 4 commits intowaybarrios:mainfrom
janhilgard:feature/qwen3-bracket-parser
Jan 22, 2026
Merged

Add multi-format tool call parser (bracket, raw JSON)#15
waybarrios merged 4 commits intowaybarrios:mainfrom
janhilgard:feature/qwen3-bracket-parser

Conversation

@janhilgard
Copy link
Copy Markdown
Collaborator

@janhilgard janhilgard commented Jan 20, 2026

Summary

Add support for multiple tool calling formats that models output:

  • Qwen3 bracket: [Calling tool: function_name({"arg": "value"})]
  • Raw JSON (fallback): {"name": "...", "arguments": {...}} - single or multiple

Changes

  • New regex parser for Qwen3 bracket format (highest priority)
  • New _parse_raw_json_tool_calls() helper for raw JSON parsing (lowest priority fallback)
  • Handles JSON arrays and multiple JSON objects with balanced brace detection
  • Properly extracts function name and JSON arguments
  • Cleans parsed content from response text

Why Raw JSON Fallback?

Some models (including Qwen3) may output plain JSON tool calls without wrapper tags. The raw JSON parser activates only when no other format matches, preventing false positives.

Testing

Tested with Qwen3-30B-A3B-Instruct-2507-MLX-6bit model:

curl -X POST http://localhost:1239/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "...",
    "messages": [{"role": "user", "content": "Search for SaaS pricing"}],
    "tools": [{"type": "function", "function": {"name": "searchReddit", "parameters": {...}}}]
  }' | jq '.choices[0].message'

# Result:
{
  "role": "assistant",
  "content": null,
  "tool_calls": [{"id": "call_...", "type": "function", "function": {"name": "searchReddit", "arguments": "{...}"}}]
}

🤖 Generated with Claude Code

janhilgard and others added 2 commits January 20, 2026 22:46
Support for Qwen3's bracket format: [Calling tool: name({...})]

This format is used by Qwen3 models with Hermes-style tool prompting.
Added as highest priority parser before Nemotron/Qwen XML/Llama patterns.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Some models (like Qwen3 MLX) output tool calls as raw JSON objects
without wrapper tags. This adds a fallback parser that handles single
objects, multiple comma-separated objects, and JSON arrays.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@janhilgard janhilgard changed the title Add Qwen3 bracket-style tool call parser Add multi-format tool call parser (bracket, raw JSON) Jan 20, 2026
@waybarrios
Copy link
Copy Markdown
Owner

Thanks for the contribution! Added a small fix for black formatting.

@waybarrios waybarrios merged commit 926892b into waybarrios:main Jan 22, 2026
3 checks passed
waybarrios pushed a commit that referenced this pull request Jan 26, 2026
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants