Fix raw XML tool calls from Ollama models in OpenCode
When you see raw text like <function=explore> or XML tags printed in OpenCode, the model is outputting plain text instead of returning a structured JSON tool call.
This happens with Ollama and Qwen models because Ollama defaults to a 2,048 or 4,096 token context window (num_ctx). OpenCode sends large system prompts and tool schemas; when the context window fills up, Ollama silently truncates the prompt. As a result, Qwen loses the system instructions for tool formatting and falls back to printing raw function text.
Step 1: create a model variant with an extended context
Create a custom model variant in Ollama that expands the context length to 16,384 or 32,768 tokens.
- Create a file named
Modelfilein your terminal:
FROM qwen3-coder
PARAMETER num_ctx 32768
PARAMETER temperature 0.2
Replace qwen3-coder with your exact base model name if different, such as qwen2.5-coder:7b. For a broader overview of how Ollama models and Modelfiles are configured, see our complete Ollama guide.
- Build the updated model variant:
ollama create qwen3-coder-32k -f Modelfile
Run `ollama list` to see your installed models and use the exact name of the Qwen model you already pulled.
Step 2: enable tools in OpenCode
Open or create your OpenCode configuration file at ~/.config/opencode/opencode.json (or opencode.jsonc) and ensure "tools": true is explicitly declared for your new model:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"ollama": {
"npm": "@ai-sdk/openai-compatible",
"name": "Ollama local",
"options": {
"baseURL": "http://localhost:11434/v1"
},
"models": {
"qwen3-coder-32k": {
"name": "Qwen Coder 32K",
"tools": true
}
}
}
}
}
Step 3: clear the OpenCode cache and relaunch
Clear any cached provider sessions that might be holding onto truncated system prompts:
rm -rf ~/.cache/opencode
Launch OpenCode with Ollama:
ollama launch opencode --model qwen3-coder-32k
or run opencode directly from your project folder.
Alternative: force system prompt instructions
If the model still outputs raw XML tags after increasing the context length, add explicit system instructions to your Modelfile to force adherence to standard tool schemas:
FROM qwen3-coder
PARAMETER num_ctx 32768
PARAMETER temperature 0.2
SYSTEM """
You are an AI programming assistant.
When executing environment tools (such as explore, edit, or bash), ALWAYS use the exact tool call schema provided in the system prompt.
Do not output raw XML tags like <function=explore> or plain text function calls. Execute tool calls directly.
"""
Rebuild the model with ollama create qwen3-coder-32k -f Modelfile and restart OpenCode.
Conclusion
The root cause is almost always a truncated context window. Extending num_ctx restores the tool instructions that the model needs to return structured JSON instead of raw text.