Repository navigation
Description
Description
Gemma 4 (e4b) tool calling fails via Ollama OpenAI-compatible API — streaming tool_calls not recognized
Summary
When using Gemma 4 (e4b) via Ollama (ollama/gemma4:e4b) as a provider, the model correctly returns tool_calls in the response, but OpenCode does not recognize them and the model responds with "I do not have the capability to execute system commands" instead of calling tools.
Reproduction
- Configure Ollama as a provider in
opencode.json:
{
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama",
"options": {
"baseURL": "http://localhost:11434/v1"
},
"models": {
"gemma4:e4b": {
"name": "Gemma 4 e4b",
"tools": true,
"limit": {
"context": 128000,
"output": 8192
}
}
}
}
}
}- Run
opencode -m ollama/gemma4:e4b - Ask: "list files in /tmp"
Expected behavior
The model should call the bash tool with ls /tmp.
Actual behavior
The model responds with: "I do not have the capability to execute system commands like listing directory contents."
Root cause analysis
I investigated and found that:
- Ollama API works correctly — both native (
/api/chat) and OpenAI-compatible (/v1/chat/completions) endpoints return propertool_callswithfinish_reason: "tool_calls"for gemma4. - Streaming also works at the API level — The streaming response includes
tool_callsin the delta and ends withfinish_reason: "tool_calls". - The issue is on the opencode side — The AI SDK (
@ai-sdk/openai-compatible) or opencode is not properly parsing/recognizing the tool calls from gemma4 in streaming mode.
API verification
Non-streaming (works):
curl -s http://localhost:11434/v1/chat/completions -d '{
"model": "gemma4:e4b",
"messages": [{"role": "user", "content": "list files in /tmp"}],
"stream": false,
"tools": [{"type": "function", "function": {"name": "bash", "description": "Execute a shell command", "parameters": {"type": "object", "required": ["command"], "properties": {"command": {"type": "string"}}}}}]
}'
# Returns: finish_reason: "tool_calls", tool_calls: [{function: {name: "bash", arguments: "{\"command\":\"ls /tmp\"}"}}]Streaming (also works at API level):
# Streaming includes tool_calls in delta and finish_reason: "tool_calls" in final chunkEnvironment
- OpenCode version: 1.3.13
- Ollama version: 0.20.2 (Docker)
- Model: gemma4:e4b
- OS: Linux
Workaround
Using qwen3.5:9b works correctly with tool calling. The issue is specific to gemma4.
Related
- ollama/ollama#15241 — Gemma4 tool call parsing fixed in Ollama 0.20.2
- PR #16531 — OpenAI-compatible custom tool compat layer (unmerged)
- Issue #20669 — Default agent brittle against local tool-call quirks
- PR #15306 — Ollama-side tool calling rework for Gemma4
Suggested fix
The toolParser compat layer from PR #16531 should be merged to handle gemma4 tool call format differences, particularly in streaming mod
Plugins
No response
OpenCode version
No response
Steps to reproduce
No response
Screenshot and/or share link
No response
Operating System
No response
Terminal
No response
Activity
LohanGuedes commentedon Apr 5, 2026
Somehow this actually works with gemma4:e4b if you use @ then explicitly say "this current folder"
I don't know if this is something regarding the model, but seems to me that gemma4 is just dumb...
EDIT: fun enough qwen3.5:9b didn't work in my case.
noxgle commentedon Apr 5, 2026
This model gemma 4:e4b is for edge devices, but such simply job like "show files " should work.
A-Risoli commentedon Apr 5, 2026
On Ubuntu :
ollama launch claude --model gemma4:26b
/init
● I apologize, but I cannot execute system commands directly through a bash tool as it is not available in my current environment.
Same scenario using Codex :
ollama launch codex --model gemma4:26b
/init
I am sorry, but I cannot execute shell commands directly. I am an AI model designed to process and generate text and code.
If you have a specific command you are trying to run or a script you need help writing, please provide the details, and I will be happy to help you debug, explain, or write the code for it!
jazzista88 commentedon Apr 5, 2026
Same issue here with opencode, i would use gemma4 as coder executor, no thinking or planning.
Is there already a fix on this ?
ErcinDedeoglu commentedon Apr 6, 2026
Can confirm this issue on Apple M5 Max (128GB) with Ollama v0.20.2 and OpenCode v1.3.16. Tested with
gemma4:31b,aratan/gemma-4-31B-it-uncensores, andjuilpark/gemma-4-31B-it-uncensored-heretic— all exhibit the same behavior.Root cause identified
The issue is specifically caused by Gemma 4's thinking/reasoning mode in streaming responses via the
/v1/chat/completionsendpoint.When Gemma 4 streams with thinking enabled (default), the delta chunks include a non-standard
reasoningfield:{"delta":{"role":"assistant","content":"","reasoning":"The user wants to..."}}@ai-sdk/openai-compatibledoesn't recognize thereasoningfield — it only sees emptycontentstrings across ~30+ chunks, then misses thetool_callsthat arrive in the final chunk. The model's tool intent is correctly expressed at the API level but never reaches OpenCode's tool execution pipeline.Workaround:
reasoning_effort: "none"Sending
"reasoning_effort":"none"in the request body disables Gemma 4's thinking mode on the/v1endpoint and produces fully standard-compliant streaming:With
reasoning_effort: "none":reasoningfield chunksfinish_reason: "tool_calls"properly setcontentfield (not buried inreasoning)Suggested fix
Either:
reasoningfieldreasoning_effort) for custom providers — this would let users work around thinking-mode models without code changesreasoning_effort: "none"when the model doesn't need extended thinking for tool callsNote: Ollama's tool call parsing for Gemma 4 was fixed in PR ollama/ollama#15306 (merged into v0.20.1/v0.20.2), so the Ollama side is resolved — this is purely an OpenCode/AI SDK streaming parsing issue now.
metcalfdaniel-dotcom commentedon Apr 8, 2026
Subscribing to this issue - same problem with gemma4:e2b on local Ollama. Tool calling works via direct API but not through OpenCode.
noxgle commentedon Apr 8, 2026
Temporary solution: Running OpenCode with Gemma 4: https://gist.github.com/daniel-farina/87dc1c394b94e45bb700d27e9ea03193
brunostc commentedon Apr 8, 2026
Can we get some attention here?
BrutchsamaJeanLouis commentedon Apr 8, 2026
YES 31b have been same looping issue
there is also issue 16531 that blocks gemma4 support
jj15warrior commentedon Apr 10, 2026
also subscribed, having the same issue with gemma4:e4b and huihui_ai/gemma-4-abliterated:e4b. the abiliterated version did try to run nonexistent tools in opencode tho.
MominAlZaraa commentedon Apr 12, 2026
The same on UBUNUTU, local LLM gemma4:31b, other gemma4 as well, don't work. Error not finding directory, although ran/openned OpenCode within directory also in Cursor with the same problem. On Opencode Zen though they work normally using API free options.
appetrosyan commentedon Apr 12, 2026
OpenCode isn't the only one that's affected. I tried running
gemma4with Claude Code and it failed in the exact same way.18 remaining items
Bazmundi commentedon Jun 8, 2026
using 1.16.2 with gemma4:latest and still doesn't work.
rkuo2000 commentedon Jun 8, 2026
you can try LM Studio, it has more detail settings, including context length, multiple GPUs on/off, ...
X-Ryl669 commentedon Jun 8, 2026
Seems like the gemma model itself has issues depending on which one you have downloaded. Updates to the chat_template.jinja in the model should fix it (not tested)
TomLucidor commentedon Jul 8, 2026
Are there similar issues with tool-calling with E4B in general? Hermes got the same issues NousResearch/hermes-agent#6626 (comment) NousResearch/hermes-agent#19887
ToeiRei commentedon Jul 8, 2026
as said in my previous comment, I assume this is an issue with gemma4
djmaxus commentedon Jul 9, 2026
If this is the case, how come gemma4 works great via ollama + Pi?
ToeiRei commentedon Jul 9, 2026
As to my understanding it's about thinking tags and stuff that engines like ollama have patched in their latest versions and some agent frameworks do still trip up on these quirks
X-Ryl669 commentedon Jul 9, 2026
You dont need to patch the engine, only the jinja chat template.
TomLucidor commentedon Jul 10, 2026
@raullenchai could you help out here checking the messages by @ToeiRei so that Rapid-MLX can have stability? I do not know who else to call with this one
mlzl1426 commentedon Jul 10, 2026
我遇到了同样的问题。根本原因:httpx 默认启用 HTTP/2,而 Ollama 不支持它。解决方法是 http2=False。详情复现脚本:https://github.com/mlzl1426/fix-ollama-httpx-http2
I ran into the same issue. Root cause: httpx enables HTTP/2 by default and Ollama doesn't support it. Fix is http2=False. Details + repro script: https://github.com/mlzl1426/fix-ollama-httpx-http2
github-actions commentedon Sep 9, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.
djmaxus commentedon Oct 4, 2026
But the devs were the ones who were inactive...
TomLucidor commentedon Oct 5, 2026
@djmaxus do a reopen, and then cite all the other related issues. Carry the torch
djmaxus commentedon Oct 5, 2026
@TomLucidor fair enough