pi-gemma-tools0.2.2
Working tool calls for Google Gemma 4 in Pi: turns Gemma's native call syntax into real tool calls and keeps codemode results.
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
Working tool calls for Google Gemma 4 in Pi.
pi install npm:pi-gemma-tools
What it fixes · Models · How it works · Development · Licence
The problem
Gemma 4 writes tool calls in its own syntax:
<|tool_call>call:read{path:<|"|>README.md<|"|>}<tool_call|>
Most backends turn this into a normal tool call. Some, OpenRouter among them, now and then pass it on as plain text instead. Pi then sees an answer with no tool call: the turn ends, the raw markup lands in the chat, and you have to nudge the model to go on.
Gemma 4 also tends to write codemode scripts such as
await tools.mcp__calculator__calculate({ expression: "3 * sqrt(3)" })
without return. The script runs, but its result never reaches the model, which then
guesses or tries again.
What it fixes
- Text calls become real calls. Calls Gemma wrote as text are taken out of the
answer, turned into proper tool calls, and the stop reason becomes
toolUse. Pi runs them like any other call, and the result goes back to the model. - Made-up tool names are mapped. When Gemma invents a name such as
mcp__calculator.calculatorand exactly one offered tool clearly matches (mcp__calculator__calculate), the call goes to that tool. Names that match nothing are passed on unchanged, so Pi reports them and the model can correct itself. - Codemode results are kept. For every Gemma 4 model, whichever provider serves it,
a codemode script with no top-level
returnwhose last statement is a value gets that value returned. Scripts that return, print or end in anything else are left alone.
Answers that already contain proper tool calls pass through untouched, and so does everything else in the stream: text, thinking, usage and errors.
Models
After installing, pick a model with /model or --model:
| Model | Backend |
|---|---|
gemma/openrouter/gemma-4-26b-a4b-it |
OpenRouter google/gemma-4-26b-a4b-it, using the OpenRouter key Pi already has |
gemma/openrouter/gemma-4-31b-it |
OpenRouter google/gemma-4-31b-it |
gemma/lmstudio/<id> |
every Gemma 4 chat model LM Studio lists when Pi starts |
pi --model gemma/openrouter/gemma-4-26b-a4b-it
The codemode fix also applies when you run a Gemma 4 model through another provider.
LM Studio
LM Studio is looked up at http://127.0.0.1:1234/v1 when Pi starts. Set
PI_GEMMA_LMSTUDIO_URL to use another address, or to off to skip the lookup. When LM
Studio is not running, its entries are simply absent. Vision projector files (mmproj)
are left out, and each model's context length is read from LM Studio.
How it works
The package registers a Pi provider named gemma. Each request goes through Pi's own
OpenAI-compatible implementation, so authentication, streaming, thinking and usage work
as they do for any other model. The package only watches the response:
- Text is streamed through until it reaches something that could be the start of
Gemma's call markup (
<|). From there it is held back until the text block ends. - At the end of the block the held text is parsed. The parser is strict: arguments in
Gemma's syntax (
<|"|>strings, bare keys, numbers, booleans,null, lists and nested objects) are read exactly, and anything that does not match stays text. - Each call found becomes a regular tool call with a fresh id, its name resolved
against the tools Pi offered. If the backend said
stop, the stop reason becomestoolUseso Pi runs the calls.
Input is bounded: at most 64 calls per answer, 1 MiB of arguments and a nesting depth of 32. The codemode fix uses acorn to find the last statement, and leaves a script alone when it does not parse.
Development
npm install
npm test # unit tests plus end-to-end tests (needs `pi` on PATH, runs no model)
npm run typecheck
npm run lint
The end-to-end tests start Pi in RPC mode against a fake OpenAI-compatible server that
replays recorded Gemma 4 output, then check that the tool really ran and its result went
back to the model. The recorded outputs are in tests/cases/gemma-calls.json, each with
its source. PI_GEMMA_TEST_TMP sets where the end-to-end tests put their temporary
files (default: the system temp folder).
To work on a checkout, install it from its folder; Pi loads it in place, so a
git pull and a /reload pick up changes:
pi install ./pi-gemma-tools
Licence
Copyright (c) 2026 Marc Mironescu (@rustytensor).
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.