Skip to content

Fix Gemma4 tool parser: support hyphenated function names and braces in string args#1150

Merged
angeloskath merged 1 commit intoml-explore:mainfrom
AkashKhamkar:fix/gemma4-tool-parser
Apr 21, 2026
Merged

Fix Gemma4 tool parser: support hyphenated function names and braces in string args#1150
angeloskath merged 1 commit intoml-explore:mainfrom
AkashKhamkar:fix/gemma4-tool-parser

Conversation

@AkashKhamkar
Copy link
Copy Markdown
Contributor

Problem

Two bugs in mlx_lm/tool_parsers/gemma4.py caused ValueError: No function provided
when parsing certain valid Gemma 4 tool calls:

  1. Hyphenated function names — the regex used \w+ which doesn't match -,
    so tools named e.g. manim-video would always fail to parse.

  2. Braces inside string arguments — the brace-balancing regex treated {/}
    inside <|"|>...<|"|> string literals as structural braces, breaking the match
    when arguments contained code snippets, markdown, or any text with {}.

Both caused an unhandled exception that crashed the server request.

Fix

  • [\w-]+ instead of \w+ for function name matching
  • Regex is now string-aware: <|"|>...<|"|> literals are matched atomically
    so braces inside them don't affect brace-balance counting
  • ToolFormatter.__call__ in server.py now catches parse errors gracefully
    instead of crashing the request

Tests

Added two test cases to tests/test_tool_parsing.py covering both bugs.

- Allow hyphens in function names ([\w-]+ instead of \w+) so tools like
  manim-video are parsed correctly
- Make brace-balancing regex string-aware so {/} inside <|"|>...<|"|>
  string arguments no longer break the match
- Catch parse errors in ToolFormatter to prevent server crashes on
  malformed or truncated tool calls
- Add tests for both parser bug fixes
@AkashKhamkar
Copy link
Copy Markdown
Contributor Author

@angeloskath would you mind taking a look when you get a chance?

@AkashKhamkar
Copy link
Copy Markdown
Contributor Author

hey @kernelpool can you take a look at this if possible thanks :)

Copy link
Copy Markdown
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great thanks!

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