Repository navigation
chat : enforce tool argument schemas for Gemma 4 - #31612
jacekpoplawski wants to merge 1 commit into
Conversation
schema-constrained grammar generation for Gemma 4 tool calls
|
@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. |
|
@jacekpoplawski take a look at my current stance on further complicating the grammar implementations, 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. |
|
@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? |
|
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. |
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 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? |
|
@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! |
|
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 .promptShould come out to: |


schema-constrained grammar generation for Gemma 4 tool calls
Overview
wip
Additional information
...
Requirements