Skip to content

Gemma 3/4 tool calling support - #215

Closed
BRVWL wants to merge 2 commits into
ml-explore:mainfrom
BRVWL:gemma4-tool-calling
Closed

BRVWL wants to merge 2 commits into
ml-explore:mainfrom
BRVWL:gemma4-tool-calling

Conversation

@BRVWL

@BRVWL BRVWL commented Apr 15, 2026

Copy link
Copy Markdown
  • Add dual format support in GemmaFunctionParser for both Gemma 3 (<start_function_call>/<end_function_call>) and Gemma 4 (<|tool_call>/<tool_call|>) formats
  • Change ToolCallFormat.infer() to use hasPrefix("gemma") instead of exact match, enabling tool calling for gemma2, gemma3, gemma3n, gemma4, gemma4_text model variants
  • Add comprehensive tests for Gemma 4 format parsing, escaped strings, and auto-detection

BRVWL added 2 commits April 16, 2026 00:07
- GemmaFunctionParser: support both Gemma 3 (<start_function_call>)
  and Gemma 4 (<|tool_call>) tag formats with correct escape markers
- ToolCallFormat: use hasPrefix("gemma") to match all gemma variants
- Update test to use Gemma 4 format tags
public let startTag: String? = "<start_function_call>"
public let endTag: String? = "<end_function_call>"
// Gemma 4 format tags (primary, as Gemma 4 is the current model)
public let startTag: String? = "<|tool_call>"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changing startTag to only the Gemma 4 marker breaks streamed Gemma 3 tool calls. ToolCallProcessor uses parser.startTag as the sole trigger, so <start_function_call>...<end_function_call> output will now pass through as regular text even though parse(content:) can handle it directly. Since this PR also maps gemma3/gemma3n to .gemma, please either keep streaming detection compatible with both start tags or split Gemma 3 and Gemma 4 into distinct parser formats.

var stripped = text
stripped = stripped.replacingOccurrences(of: Self.gemma3StartTag, with: "")
stripped = stripped.replacingOccurrences(of: Self.gemma3EndTag, with: "")
return (false, stripped)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This fallback mis-detects unterminated Gemma 4 calls during processEOS(). The default ToolCallParser.parseEOS splits the buffer on startTag before calling parse, so a buffered value like <|tool_call>call:search{query:<|"|>hello, world<|"|>} reaches detectFormat without either Gemma 4 wrapper tag. It then falls back to the Gemma 3 escape marker and splits the escaped string at the comma. Please override parseEOS for Gemma or otherwise preserve the detected Gemma 4 format after splitting.

@aleroot aleroot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this. I’m interested in Gemma 4 tool calling support as well, and the general direction looks good. I think this needs a couple of adjustments before merging. Once those are covered with processor-level regression tests for both formats, this should be merged.

@DePasqualeOrg

Copy link
Copy Markdown
Contributor

I've addressed the parsing problems in this library in Swift LM Response Parser, which parses reasoning and tool-use output from all models implemented in MLX Swift LM and emits a stream of Open Responses events. I created a demo of it in this PR in MLX Swift Examples and submitted a proposal for how it could be used with MLX Swift LM.

If you're able to test it for your use case, please post your feedback here or in an issue in the repo.

@davidkoski

Copy link
Copy Markdown
Member

Closing as dup of #183. See also #360 which is in the same area.

@davidkoski davidkoski closed this Jun 29, 2026
davidtai added a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
Integrate an isolated non-serving model correction with its regression assertions. Preserve concurrent main changes and promote the resolved recording checks to ordinary assertions. No deployment or serving dependency update.
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.

4 participants