Skip to content

feat: local model tool call reliability (PRs #19000 + #16531 + nudge recovery)#1

Merged
andrewgwoodruff merged 5 commits into
devfrom
fix/skill-serialization-format-for-local-models
May 1, 2026
Merged

feat: local model tool call reliability (PRs #19000 + #16531 + nudge recovery)#1
andrewgwoodruff merged 5 commits into
devfrom
fix/skill-serialization-format-for-local-models

Conversation

@andrewgwoodruff

Copy link
Copy Markdown
Owner

Summary

  • Fix includeUsage injection — User-configured providers (Ollama, oMLX, etc.) no longer get stream_options: {include_usage: true} injected, which was causing request failures. Only non-config-source providers receive it.
  • Add openai-compatible-compat.ts — New toolParser pipeline with three parser types:
    • json — converts {"name":"...","arguments":{...}} plain-text output into proper tool-call parts
    • raw-function-call — rewrites modern tools API to legacy functions API for models that need it
    • single-tool-text — maps bare text responses to a named tool
  • wrapStream middleware — For config-source providers, intercepts the stream at finish and repairs raw JSON tool-call text into a proper tool-call stream part.
  • Nudge recovery for embedded tool calls — When a model finishes a turn with no tool calls but its text output contains {"name":"...","arguments":...} JSON, injects a synthetic user message telling it to call the tool directly and re-enters the loop (capped at 3 nudges per session).

Configuration

To enable the json tool parser for a local provider, add toolParser to its options in ~/.config/opencode/opencode.json:

{
  "provider": {
    "omlx": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "http://127.0.0.1:8000/v1",
        "apiKey": "local",
        "toolParser": [{"type": "json"}]
      }
    }
  }
}

Test plan

  • Run a two-step task with a local model (e.g. gpt-oss-20b via oMLX): "Find all files in apps/platform/src/api that contain the word 'merchant', then read the first one you find and tell me what it does." — should chain grep → read without stalling
  • Verify Anthropic/cloud providers unaffected (no stream_options regression)
  • Verify includeUsage still injected for bundled/env-source openai-compatible providers

🤖 Generated with Claude Code

The skills system prompt block was always serialized as XML
(`<available_skills>`), a Claude-specific convention. Non-Anthropic
models (Ollama, etc.) interpret dense XML in the system prompt as a
signal to respond in XML format, breaking JSON tool calls.

Controlled testing across 50 runs each showed:
- XML:      68% tool-call success
- Markdown: 74% tool-call success
- JSON:     86% tool-call success

Changes:
- `skill/index.ts`: replace `verbose: boolean` with `format: "xml" | "json" | "markdown"` in `fmt()`; add JSON serialization path
- `session/system.ts`: pass model to `skills()`; default to JSON for non-Anthropic models, XML for Anthropic; read `skills.format` config override
- `config/skills.ts`: add optional `format` field so users can override per-project
- `tool/registry.ts`: update call site to use explicit `format: "markdown"` (tool description path, unchanged behavior)

Fixes anomalyco#24824
- Fix Schema.Union call in config/skills.ts to use array syntax
- Update system.test.ts to pass model argument to skills() and add
  a second test covering JSON output for non-Anthropic models
…havior

Remove the non-Anthropic auto-detection logic. The default remains XML
for all models — the config option is an opt-in escape hatch for users
who need a different format, not an opinionated default change.

Also reverts the model parameter from skills() since it's no longer
needed for format detection.
…ool call reliability

- Fix includeUsage injection to skip user-configured providers (PR anomalyco#19000)
  — Ollama/oMLX reject stream_options.include_usage; only inject for
  non-config-source providers where undefined
- Add wrapStream middleware for config-source providers to detect raw JSON
  tool call text and repair it into proper tool-call stream parts (PR anomalyco#19000)
- Add openai-compatible-compat.ts with toolParser pipeline (PR anomalyco#16531)
  — json parser: converts {"name":"...","arguments":{...}} text to tool_calls
  — raw-function-call parser: rewrites tools API to legacy functions API
  — single-tool-text parser: maps bare text to a named tool
- Integrate toolParsers into provider fetch handler; buffers SSE/JSON
  responses and rewrites them when toolParser config is present

Coding-Agent: OpenCode
Model: gpt-oss-20b via oMLX
…lling it

When a config-source provider's model (e.g. gpt-oss-20b) finishes a turn
with no tool calls but its text output contains {"name":"...","arguments":...}
JSON, inject a synthetic user message telling it to call the tool directly
and re-enter the loop. Caps at 3 nudges per session to prevent infinite loops.

This addresses the failure mode where the model expresses tool-call intent
in its thinking text but emits no actual tool-call stream part.

Coding-Agent: OpenCode
Model: gpt-oss-20b via oMLX
@andrewgwoodruff andrewgwoodruff self-assigned this May 1, 2026
@andrewgwoodruff andrewgwoodruff merged commit f79a11e into dev May 1, 2026
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.

1 participant