Skip to content

fix(ollama): make agent + tool calls work against Ollama Cloud - #254

Open
craigamcw wants to merge 1 commit into
OpenCoworkAI:mainfrom
craigamcw:fix/ollama-cloud-compat
Open

fix(ollama): make agent + tool calls work against Ollama Cloud#254
craigamcw wants to merge 1 commit into
OpenCoworkAI:mainfrom
craigamcw:fix/ollama-cloud-compat

Conversation

@craigamcw

@craigamcw craigamcw commented Jun 16, 2026

Copy link
Copy Markdown

Problem

Using Ollama Cloud (https://ollama.com/v1) with the agent fails with 400 invalid message format — but only on the cloud endpoint; local Ollama and other relays accept the same requests. Captured the exact request body Ollama Cloud rejects and traced it to two serialization issues.

Fixes

1. User content serialized as an array instead of a string
For any image-capable model, the openai-completions provider emitted content: [{ "type": "text", "text": "…" }]. Ollama Cloud only accepts a plain string for text content. This flattens text-only content to a string (the standard OpenAI Chat Completions form that OpenAI, OpenRouter, and Ollama all accept), keeping the array form only when there's an actual image.
patches/@mariozechner+pi-ai+0.60.0.patch

2. Non-standard { "type": "thinking" } content block on follow-up turns
DeepSeek-V4 models set requiresThinkingInContent, which serializes the assistant's prior thinking into content[] as { "type": "thinking", … }. Some custom relays require that, but Ollama Cloud rejects it (it's not a valid OpenAI content type), so the second agent turn (after a tool call) 400s. This disables it specifically for the ollama.com endpoint while leaving it on for local Ollama and other relays.
src/main/claude/pi-model-resolution.ts

Tests

  • src/tests/claude/pi-model-resolution.test.ts: new cases asserting requiresThinkingInContent is false for ollama.com and true for other relay endpoints.
  • Full pi-model-resolution / deepseek-thinking-serialization / claude-sdk-one-shot suites pass; tsc --noEmit clean.

Verified end-to-end: a multi-turn, tool-using agent session with deepseek-v4-pro on Ollama Cloud now completes successfully.

🤖 Generated with Claude Code

Two request-serialization incompatibilities caused Ollama Cloud
(https://ollama.com/v1) to reject agent requests with
"400 invalid message format", even though desktop/local endpoints accept them.

1. User message content was serialized as a content-block array
   ([{ "type": "text", ... }]) for any image-capable model. Ollama Cloud
   only accepts a plain string for text. Flatten text-only content to a
   string in the pi-ai openai-completions provider
   (patches/@mariozechner+pi-ai+0.60.0.patch).

2. DeepSeek-V4 models get requiresThinkingInContent, which serializes the
   assistant's thinking as a non-standard { "type": "thinking" } content
   block. Some custom relays require that, but Ollama Cloud rejects it.
   Disable it specifically for the ollama.com endpoint (and keep it for
   local Ollama / other relays) in applyPiModelRuntimeOverrides.

Adds tests covering the Ollama-Cloud-vs-relay branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review mode: initial

Findings

  • [Minor] ollama.com endpoint detection could match unintended subdomains (e.g., https://xollama.com). The check endpoint.includes('ollama.com') is broad; consider using a more precise pattern like endpoint.includes('//ollama.com') or a URL hostname parse to reduce false positives.
    Suggested fix in src/main/claude/pi-model-resolution.ts:337:
    const endpointUrl = new URL(options.customBaseUrl || nextModel.baseUrl || '');
    const isOllamaCloud = endpointUrl.hostname === 'ollama.com' || endpointUrl.hostname.endsWith('.ollama.com');

Summary
No blockers or major issues found. The patch and runtime override correctly address the two serialization incompatibilities with Ollama Cloud. The detection logic for ollama.com is adequate for the known endpoints but could be tightened to avoid false matches. Test coverage is good.

Testing
Existing tests pass (vitest suites for pi-model-resolution). Not run (automation).

Open Cowork Bot

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