[PR #15314] fix: respect absent thinking field in Anthropic compat endpoint #15112

Open
opened 2026-04-13 01:10:37 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/15314
Author: @rudra717
Created: 4/4/2026
Status: 🔄 Open

Base: mainHead: fix/anthropic-thinking-default


📝 Commits (1)

  • 270fee9 fix: respect absent thinking field in Anthropic compat endpoint

📊 Changes

2 files changed (+50 additions, -0 deletions)

View changed files

📝 anthropic/anthropic.go (+7 -0)
📝 anthropic/anthropic_test.go (+43 -0)

📄 Description

Summary

  • Fixes the Anthropic-compatible /v1/messages endpoint unconditionally enabling thinking for capable models (e.g. qwen3.5) even when the thinking field is absent from the request
  • Explicitly sets Think to false in FromMessagesRequest when the Anthropic request omits the thinking field or sets it to "disabled"

Motivation

Per the Anthropic API spec, omitting the thinking field means thinking is disabled. The native Ollama endpoint correctly respects think: false, but the Anthropic compat layer left Think as nil, which caused the route handler to auto-enable thinking for capable models.

This affected anyone using Ollama as an Anthropic API drop-in replacement (e.g. via misanthropic, Claude Code, or other Anthropic SDKs).

Changes

  • anthropic/anthropic.go: Added else branch in FromMessagesRequest to explicitly set Think = false when thinking is not enabled
  • anthropic/anthropic_test.go: Added two new tests:
    • TestFromMessagesRequest_ThinkingAbsent — verifies Think is false when thinking field is omitted
    • TestFromMessagesRequest_ThinkingDisabled — verifies Think is false when thinking.type is "disabled"

Reproduction

# Before fix — thinking enabled despite missing thinking field
curl http://localhost:11434/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: dummy" \
  -d '{"model": "qwen3.5:9b", "max_tokens": 200, "messages": [{"role": "user", "content": "What is 2+2? One word."}]}'
# Returns: {"content": [{"type": "thinking", "thinking": "..."}]}

# After fix — thinking correctly disabled
# Returns: {"content": [{"type": "text", "text": "Four"}]}

Testing

  • New unit tests cover both the absent and explicitly-disabled cases
  • Existing TestFromMessagesRequest_WithThinking test continues to pass (thinking enabled when requested)
  • TestAnthropicMessagesMiddleware_SetsRelaxThinkingFlag unaffected (tests middleware flag, not conversion)

Fixes #15287


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ollama/ollama/pull/15314 **Author:** [@rudra717](https://github.com/rudra717) **Created:** 4/4/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/anthropic-thinking-default` --- ### 📝 Commits (1) - [`270fee9`](https://github.com/ollama/ollama/commit/270fee9c32ae9e3db489ba733af2efde50536593) fix: respect absent thinking field in Anthropic compat endpoint ### 📊 Changes **2 files changed** (+50 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `anthropic/anthropic.go` (+7 -0) 📝 `anthropic/anthropic_test.go` (+43 -0) </details> ### 📄 Description ## Summary - Fixes the Anthropic-compatible `/v1/messages` endpoint unconditionally enabling thinking for capable models (e.g. `qwen3.5`) even when the `thinking` field is absent from the request - Explicitly sets `Think` to `false` in `FromMessagesRequest` when the Anthropic request omits the `thinking` field or sets it to `"disabled"` ## Motivation Per the [Anthropic API spec](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking), omitting the `thinking` field means thinking is **disabled**. The native Ollama endpoint correctly respects `think: false`, but the Anthropic compat layer left `Think` as `nil`, which caused the route handler to auto-enable thinking for capable models. This affected anyone using Ollama as an Anthropic API drop-in replacement (e.g. via `misanthropic`, Claude Code, or other Anthropic SDKs). ## Changes - **`anthropic/anthropic.go`**: Added `else` branch in `FromMessagesRequest` to explicitly set `Think = false` when thinking is not enabled - **`anthropic/anthropic_test.go`**: Added two new tests: - `TestFromMessagesRequest_ThinkingAbsent` — verifies `Think` is `false` when `thinking` field is omitted - `TestFromMessagesRequest_ThinkingDisabled` — verifies `Think` is `false` when `thinking.type` is `"disabled"` ## Reproduction ```bash # Before fix — thinking enabled despite missing thinking field curl http://localhost:11434/v1/messages \ -H "Content-Type: application/json" \ -H "x-api-key: dummy" \ -d '{"model": "qwen3.5:9b", "max_tokens": 200, "messages": [{"role": "user", "content": "What is 2+2? One word."}]}' # Returns: {"content": [{"type": "thinking", "thinking": "..."}]} # After fix — thinking correctly disabled # Returns: {"content": [{"type": "text", "text": "Four"}]} ``` ## Testing - New unit tests cover both the absent and explicitly-disabled cases - Existing `TestFromMessagesRequest_WithThinking` test continues to pass (thinking enabled when requested) - `TestAnthropicMessagesMiddleware_SetsRelaxThinkingFlag` unaffected (tests middleware flag, not conversion) Fixes #15287 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-13 01:10:37 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#15112