Repository navigation
Preserve Gemma 4 tool definitions and add Minja upper filter - #1129
Open
Joel Stidley (joelst) wants to merge 4 commits into
Open
Joel Stidley (joelst) wants to merge 4 commits into
Joel Stidley (joelst) wants to merge 4 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Joel Stidley (joelst)
force-pushed
the
fix/gemma4-raw-tool-templates
branch
from
October 4, 2026 07:09
b7d872c to
f55c13a
Compare
This was referenced Oct 4, 2026
Recognize Gemma's function declaration macro so tool definitions retain their nested OpenAI shape. Add Minja's ASCII-safe upper filter and regression tests for tool rendering and false-positive dispatch. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4ca47b75-18b7-4168-b67d-aae8529ba961
Recognize tool_data only as a formal parameter of Gemma's function declaration macro. Ignore raw-block content, parse nested defaults safely, and cover false-positive and whitespace-control cases. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4ca47b75-18b7-4168-b67d-aae8529ba961
Joel Stidley (joelst)
force-pushed
the
fix/gemma4-raw-tool-templates
branch
from
October 4, 2026 07:16
f55c13a to
a76762a
Compare
Joel Stidley (joelst)
marked this pull request as ready for review
October 4, 2026 07:24
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The detector mishandles raw tags using the supported ~ control marker.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Preserves Gemma 4’s nested tool schema and adds Minja uppercase filtering.
Changes:
- Detects Gemma’s tool-definition macro to bypass lossy normalization.
- Adds ASCII-only
upper, including mapped usage. - Adds detector, rendering, and UTF-8 regression tests.
| File | Description |
|---|---|
shared/api/chat_template.cc |
Selects raw tools for Gemma templates. |
shared/api/chat_template_utils.h |
Implements Gemma macro detection. |
shared/api/minja.hpp |
Adds the upper filter. |
test/pp_api_test/test_tokenizer_chat.cc |
Adds regression coverage. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 4ca47b75-18b7-4168-b67d-aae8529ba961
Collaborator
|
/azp run onnxruntime-extensions.ci |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
Author
|
/azp run onnxruntime-extensions.ci |
|
Commenter does not have sufficient privileges for PR 1129 in repo microsoft/onnxruntime-extensions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #1128.
Companion to #1130 (Minja macro scope), which fixes the related extra-
<|turn>modelsymptom. The PRs are independent and can merge in either order.Problem
NormalizeTools()unwraps OpenAI function tools into a flat Phi-style shape. Gemma 4'sformat_function_declaration(tool_data)macro readstool_data['function']['name'], so rendering a tool currently fails withTrying to access property 'name' on null!. Once the nested object is preserved, Gemma also uses Minja's missingupperfilter (including throughmap('upper')) to format JSON Schema types.Change scope
shared/api/chat_template.ccandshared/api/chat_template_utils.h: preserve raw OpenAI tools when the exact Gemma function-declaration macro hastool_dataas a formal parameter. Default expressions are scanned with nested delimiter and quote handling, so references totool_datainside defaults do not count. Comments and Jinja raw-block contents are ignored. Existingtool.functionandtojsondetection is unchanged.shared/api/minja.hpp: add anupperfilter next tolower. It uppercases ASCIIa-zand preserves non-ASCII bytes, so UTF-8 text is not corrupted. This is not Unicode-aware case conversion.test/pp_api_test/test_tokenizer_chat.cc: cover direct Gemma rendering, macro signature/detector boundaries, raw-block lookalikes, direct and mappedupper, false-positive dispatch from tool-call history/comments/strings, and continued normalization when the Gemma macro is absent.No changes are made to tool normalization itself, model templates, Foundry Local, or image decoding. Templates without an existing raw-tool marker or the Gemma macro keep the prior normalized-tool behavior. A downstream package must include a build of onnxruntime-extensions containing this change for the fix to take effect.
Reviewer questions before merge
tool_datato be a formal parameter, but a future template that renames the macro may need an update.upperthe intended Minja behavior for this change? It also uses Minja string conversion for non-string values (including Gemma's array-valued schema types); it does not implement full Unicode casing.{% raw %}tags.Validation
Real-runtime check: I built ONNX Runtime GenAI
rel-0.17.1(the version Foundry Local pins) with ORT 1.30.0, pointingFETCHCONTENT_SOURCE_DIR_ONNXRUNTIME_EXTENSIONSat this branch. The base build reproduces Gemma 4 tool calls fail because tool definitions are normalized to the wrong shape #1128 exactly (property 'name' on null). With this PR plus Give each Minja macro call its own variable scope #1130, the published Gemma 4 E2B template renders byte-identical to Jinja2 for user+tools, no-tools, and tool-call history.All
OrtxTokenizerTest.*tests pass (92/92) on macOS arm64; with Give each Minja macro call its own variable scope #1130 merged in, 94/94.Built
pp_api_testsuccessfully.Latest full tokenizer suite:
OrtxTokenizerTest.*, 92/92 passed, including all Gemma, Phi-4, Qwen, and GPT-OSS tests.Full
pp_api_teston the preceding detector revision passed 180/181;ImageDecoderTest.TestJpegEncoderDecoderfailed on a macOS pixel expectation in unchanged image-decoder code (tracked separately in Make JPEG decoder test reliable across supported developer platforms #1127).Security review found no vulnerabilities in the change.