Skip to content

chat : enforce tool argument schemas for Gemma 4 - #31612

Closed
jacekpoplawski wants to merge 1 commit into
ggml-org:masterfrom
jacekpoplawski:gemma4-schema-constrained-tool-calling
Closed

jacekpoplawski wants to merge 1 commit into
ggml-org:masterfrom
jacekpoplawski:gemma4-schema-constrained-tool-calling

Conversation

@jacekpoplawski

Copy link
Copy Markdown
Contributor

schema-constrained grammar generation for Gemma 4 tool calls

Overview

wip

Additional information

...

Requirements

schema-constrained grammar generation for Gemma 4 tool calls
@jacekpoplawski
jacekpoplawski requested a review from a team as a code owner November 3, 2026 15:24
@jacekpoplawski
jacekpoplawski marked this pull request as draft November 3, 2026 15:25
@jacekpoplawski

Copy link
Copy Markdown
Contributor Author

@aldehir Could you take a look and let me know if this approach is heading in the right direction? I can add tests as well.

@aldehir

aldehir commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

@jacekpoplawski take a look at my current stance on further complicating the grammar implementations, especially wrt unordered objects : #30193 (comment)

It's not mutually exclusive to supporting grammar constrained decoding for Gemma 4, which would still be used for strict tools.

Also take a look at #23247 which aims to solve the same problem. Ideally, #29307 lands first so we can use tokens in the grammar for Gemma 4's string delimiter. That would influence the json schema to grammar implementation, possibly by refactoring it to produce the grammar via the PEG parsers DSL.

@jacekpoplawski

Copy link
Copy Markdown
Contributor Author

@aldehir I'm trying to improve (fix) the agentic coding on Gemma 4. Since its release, I've never been able to get the edit tool working correctly in Pi. Last week I started investigating the issue, and with my current changes I'm now getting zero tool errors, even with Gemma 4 26B QAT.

Is there anything I can do to help move this forward faster? Would it make sense to merge some smaller pieces first?

I wasn't aware of the other PRs. Should I rebase onto #30476 or #29307?

@aldehir

aldehir commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What's the primary failure? Hallucinating argument keys?

You say fix, but I have driven Gemma 4 with Pi extensively. The only failure I noticed was resuming thinking within a tool call, which cannot be fixed with constraining the grammar.

I need to better understand why we're having drastically different experiences.

@jacekpoplawski

Copy link
Copy Markdown
Contributor Author

What's the primary failure? Hallucinating argument keys?

I'll try to collect some detailed examples later today. Gemma seems to recognize its failures, but sometimes can't fix them even after multiple attempts. Eventually, it gives up and switches to the write tool.

@jacekpoplawski

Copy link
Copy Markdown
Contributor Author

@aldehir two quick examples:

Screenshot From 2026-11-04 15-09-01 Screenshot From 2026-11-04 15-07-56

version: 0.8.0-dev (build 12002, commit 418f905)

this happens with both the 31B and 26B models, but never with any non-Gemma models

@aldehir

aldehir commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

@jacekpoplawski can you test out aldehir#38?

@jacekpoplawski

Copy link
Copy Markdown
Contributor Author

@jacekpoplawski can you test out aldehir#38?

Unfortunately, your PR seems to work perfectly, zero tool calling errors so far! ;)

What still puzzles me is why you previously mentioned having no issues with pi. What have I been doing wrong for the past 7 months?

@aldehir

aldehir commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@jacekpoplawski glad to hear it works. It seems guiding argument keys helps Gemma 4 quite a bit. I'll simplify and submit a PR upstream this weekend.

It is puzzling that I don't experiment many issues, but I wrote a comprehensive series of tests and was able to reproduce. I mainly use ggml-org models, which don't use an imatrix. Not sure if that is a contributing factor.

Anyway, thanks for bringing this to my attention!

@aldehir

aldehir commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Just out of curiosity, can you run this against your server to make sure the edit tool's schema is populated in the prompt:

curl -sS http://127.0.0.1:8080/apply-template -d '{
  "messages": [{"role": "user", "content": "Rename the function foo to bar in src/main.c"}],
  "tools": [{
    "type": "function",
    "function": {
      "name": "edit",
      "description": "Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.",
      "parameters": {
        "type": "object",
        "properties": {
          "path": {"type": "string", "description": "Path to the file to edit (relative or absolute)"},
          "edits": {
            "type": "array",
            "description": "One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead.",
            "items": {
              "type": "object",
              "properties": {
                "oldText": {"type": "string", "description": "Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call."},
                "newText": {"type": "string", "description": "Replacement text for this targeted edit."}
              },
              "required": ["oldText", "newText"]
            }
          }
        },
        "required": ["path", "edits"]
      }
    }
  }]
}' | jq -r .prompt

Should come out to:

<|turn>system
<|think|>
<|tool>declaration:edit{description:<|"|>Edit a single file using exact text replacement. Every edits[].oldText must match a unique, non-overlapping region of the original file. If two changes affect the same block or nearby lines, merge them into one edit instead of emitting overlapping edits. Do not include large unchanged regions just to connect distant changes.<|"|>,parameters:{properties:{edits:{description:<|"|>One or more targeted replacements. Each edit is matched against the original file, not incrementally. Do not include overlapping or nested edits. If two changes touch the same block or nearby lines, merge them into one edit instead.<|"|>,items:{properties:{newText:{description:<|"|>Replacement text for this targeted edit.<|"|>,type:<|"|>STRING<|"|>},oldText:{description:<|"|>Exact text for one targeted replacement. It must be unique in the original file and must not overlap with any other edits[].oldText in the same call.<|"|>,type:<|"|>STRING<|"|>}},required:[<|"|>oldText<|"|>,<|"|>newText<|"|>],type:<|"|>OBJECT<|"|>},type:<|"|>ARRAY<|"|>},path:{description:<|"|>Path to the file to edit (relative or absolute)<|"|>,type:<|"|>STRING<|"|>}},required:[<|"|>path<|"|>,<|"|>edits<|"|>],type:<|"|>OBJECT<|"|>}}<tool|><turn|>
<|turn>user
Rename the function foo to bar in src/main.c<turn|>
<|turn>model

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