Skip to content

Preserve Gemma 4 tool definitions and add Minja upper filter - #1129

Open
Joel Stidley (joelst) wants to merge 4 commits into
microsoft:mainfrom
joelst:fix/gemma4-raw-tool-templates
Open

Joel Stidley (joelst) wants to merge 4 commits into
microsoft:mainfrom
joelst:fix/gemma4-raw-tool-templates

Conversation

@joelst

@joelst Joel Stidley (joelst) commented Oct 4, 2026 •

Copy link
Copy Markdown

Fixes #1128.

Companion to #1130 (Minja macro scope), which fixes the related extra-<|turn>model symptom. The PRs are independent and can merge in either order.

Problem

NormalizeTools() unwraps OpenAI function tools into a flat Phi-style shape. Gemma 4's format_function_declaration(tool_data) macro reads tool_data['function']['name'], so rendering a tool currently fails with Trying to access property 'name' on null!. Once the nested object is preserved, Gemma also uses Minja's missing upper filter (including through map('upper')) to format JSON Schema types.

Change scope

  • shared/api/chat_template.cc and shared/api/chat_template_utils.h: preserve raw OpenAI tools when the exact Gemma function-declaration macro has tool_data as a formal parameter. Default expressions are scanned with nested delimiter and quote handling, so references to tool_data inside defaults do not count. Comments and Jinja raw-block contents are ignored. Existing tool.function and tojson detection is unchanged.
  • shared/api/minja.hpp: add an upper filter next to lower. It uppercases ASCII a-z and 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 mapped upper, 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

  1. Is the exact macro name a reasonable compatibility boundary for Gemma? The detector now requires tool_data to be a formal parameter, but a future template that renames the macro may need an update.
  2. Is ASCII-only upper the 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.
  3. Are there supported Gemma template variants or downstream integration tests we should add? Current rendering coverage uses the repository fixture. The detector's raw-block handling is unit-tested directly; Minja itself does not currently render {% raw %} tags.

Validation

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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
@joelst
Joel Stidley (joelst) force-pushed the fix/gemma4-raw-tool-templates branch from f55c13a to a76762a Compare October 4, 2026 07:16
@joelst
Joel Stidley (joelst) marked this pull request as ready for review October 4, 2026 07:24
@joelst
Joel Stidley (joelst) requested a review from a team as a code owner October 4, 2026 07:24
Copilot AI balanced review requested due to automatic review settings October 4, 2026 07:24

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The detector mishandles raw tags using the supported ~ control marker.

Review effort: Balanced
Findings: 1 Medium severity

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.

Comment thread shared/api/chat_template_utils.h Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4ca47b75-18b7-4168-b67d-aae8529ba961

Copilot AI 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.

Copilot review overview

🟢 Approval recommended

The implementation is scoped, preserves existing dispatch behavior, and includes strong boundary and integration coverage.

Review effort: Balanced
Findings: None

Resolved since last review (1)

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

The new custom template scanner affects tool-shape dispatch and warrants final human review despite strong regression coverage.

Review effort: Balanced
Findings: None

@sayanshaw24

Copy link
Copy Markdown
Collaborator

/azp run onnxruntime-extensions.ci

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI 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.

🟢 Approval recommended

No blocking issues remain in the scoped detector, filter implementation, or regression coverage.

0 open findings

🧠 Review effort: Balanced

@joelst

Copy link
Copy Markdown
Author

/azp run onnxruntime-extensions.ci

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 1129 in repo microsoft/onnxruntime-extensions

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.

Gemma 4 tool calls fail because tool definitions are normalized to the wrong shape

3 participants