Skip to content

feat: multi-tool enhanced passthrough with format:json + content→tool_calls fixup#6

Merged
FScholPer merged 2 commits into
mainfrom
devin/1780694149-multi-tool-json-format
Jun 5, 2026
Merged

feat: multi-tool enhanced passthrough with format:json + content→tool_calls fixup#6
FScholPer merged 2 commits into
mainfrom
devin/1780694149-multi-tool-json-format

Conversation

@FScholPer

Copy link
Copy Markdown
Owner

Summary

Fixes the real-world issue where Ollama returns tool calls as JSON text in content instead of proper tool_calls objects — breaking frameworks like OpenClaw.

When >1 custom tools detected:

  1. Injects format constraint before forwarding to Ollama:

    • Native /api/chat: format: "json"
    • OpenAI /v1/chat/completions: response_format: {type: "json_object"}
  2. Post-processes response via tool_call_fixup.py:

    • If tool_calls already present → no-op
    • If content contains parseable JSON matching {name, arguments} pattern → moves it into tool_calls, sets finish_reason: "tool_calls"
    • Validates parsed tool names against the tools[] from the request
    • Handles: single object, arrays, {tool_calls: [...]} wrapper, markdown fences, prose-prefixed JSON

Routing logic in main.py:

_count_custom_tools(body) > 1  →  proxy_multi_tool_passthrough()
extraction detected             →  instructor pipeline (unchanged)
else                            →  plain passthrough (unchanged)

112 tests pass, ruff clean.

FScholPer and others added 2 commits June 5, 2026 21:21
…_calls fixup

When multiple custom tools are present:
1. Inject format:json (native) / response_format:json_object (OpenAI)
   to force Ollama to produce valid JSON output
2. Post-process response: if model puts tool call JSON in content field
   instead of tool_calls, parse and convert it to proper tool_calls format

This fixes the real-world issue where Ollama returns tool calls as text
in the content field instead of structured tool_calls, which breaks
frameworks like OpenClaw that expect proper tool_calls objects.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… metadata

1. Multi-tool enhanced passthrough now applies regardless of whether
   tool results are present. Previously, follow-up requests (role: tool)
   would skip the fixup and use plain passthrough, so the model's next
   response could still have JSON-in-content instead of tool_calls.

2. Strip __header_schema from request body before forwarding to Ollama
   (internal proxy metadata should not leak to the backend).

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@FScholPer FScholPer merged commit b63fc81 into main Jun 5, 2026
4 checks passed
@FScholPer FScholPer deleted the devin/1780694149-multi-tool-json-format branch June 5, 2026 21:35
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