Skip to content

feat: Gemma 4 tool call + reasoning parser#250

Closed
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Thump604:feat/gemma4-parsers
Closed

feat: Gemma 4 tool call + reasoning parser#250
Thump604 wants to merge 2 commits intowaybarrios:mainfrom
Thump604:feat/gemma4-parsers

Conversation

@Thump604
Copy link
Copy Markdown
Collaborator

@Thump604 Thump604 commented Apr 3, 2026

Summary

  • Gemma 4 tool call parser: parses <|tool_call>call:name{params}<tool_call|> with <|"|> string escaping
  • Gemma 4 reasoning parser: extracts <|channel>thought...<channel|> blocks into reasoning_content
  • Registered as --tool-call-parser gemma4 and --reasoning-parser gemma4
  • CLI choices updated

Addresses the server-side equivalent of ml-explore/mlx-lm#1096 (Gemma 4 native tool calls not parsed).

Details

Tool parser (tool_parsers/gemma4_tool_parser.py):

  • Regex extracts function name and parameter block from <|tool_call>call:name{...}<tool_call|>
  • Handles <|"|> escaped string values, numeric/boolean coercion
  • Arguments serialized to JSON string for OpenAI API compatibility
  • Streaming support with start-marker detection

Reasoning parser (reasoning/gemma4_parser.py):

  • Splits <|channel>thought\n...reasoning...<channel|> from final answer
  • Non-streaming: returns (reasoning, content) tuple
  • Streaming: tracks channel boundaries, emits reasoning deltas then content deltas

Test plan

  • Non-streaming: tool_calls field populated with correct function name + JSON arguments
  • Non-streaming: reasoning_content separated from content, both present
  • Streaming: SSE chunks flow correctly
  • Concurrent: 3 simultaneous requests with BatchedEngine
  • Thinking + tools: model produces empty think channel then tool call (correct behavior)
  • Parser standalone unit tests pass

Thump604 added 2 commits April 3, 2026 11:19
Tool parser handles Gemma 4's native format:
  <|tool_call>call:function_name{key:<|"|>value<|"|>}<tool_call|>
Parses <|"|>-escaped strings, numeric values, booleans.
Arguments serialized to JSON string for OpenAI API compat.

Reasoning parser handles channel-based thinking:
  <|channel>thought\n...reasoning...<channel|>
  Final answer content

Registered as "gemma4" in both parser registries.
CLI choices updated.

Tested: tool_calls extracted to API field, reasoning_content
separated from content, streaming works, concurrent batching works.
Gajesh2007 added a commit to Gajesh2007/vllm-mlx that referenced this pull request Apr 7, 2026
Port from upstream PRs waybarrios#250 and waybarrios#254:
- gemma4_parser.py: extracts <|channel>thought...<channel|> into reasoning_content
- gemma4_tool_parser.py: parses <|tool_call>call:name{...}<tool_call|> format
- Auto-detection in AutoToolParser for Gemma 4 markers
- Tests for tool parser
@Thump604
Copy link
Copy Markdown
Collaborator Author

Quick merge signal on this one: this is the Gemma 4 parser pair we are already relying on locally on the current Gemma lane (reasoning_parser=gemma4, tool_call_parser=gemma4). No known blockers on our side, branch is mergeable, and CI is green.

If you're doing a fast sweep, this one looks ready.

@janhilgard
Copy link
Copy Markdown
Collaborator

+1 for merge — we've been running this in production on Gemma 4 26B-A4B (mixed 6/8-bit) for several days now with --tool-call-parser gemma4 --reasoning-parser gemma4 and it's been solid. Tool calls parse correctly, reasoning content separates cleanly, and streaming works as expected under concurrent load.

Clean implementation, no issues encountered. Would be great to get this into main. 🙏

@waybarrios
Copy link
Copy Markdown
Owner

Thanks for this one. The reasoning parser landed via #268 and the tool call parser is covered by #269 which we're about to merge. Your review and testing on the Gemma 4 work across multiple PRs made a real difference in getting this shipped. Closing in favor of #269.

@waybarrios waybarrios closed this Apr 10, 2026
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.

3 participants