[PR #13279] [MERGED] fix(api): correct Content-Type header for /api/chat and /api/generate when using cloud models #14146

Closed
opened 2026-04-13 00:46:29 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ollama/ollama/pull/13279
Author: @ZeeeUs
Created: 11/30/2025
Status: Merged
Merged: 12/5/2025
Merged by: @pdevine

Base: mainHead: fix/api-content-type-header


📝 Commits (2)

  • 7cea92a fix(api): correct Content-Type header for /api/chat and /api/generate responses whe use Cloud models
  • 6276399 change default mime-type

📊 Changes

1 file changed (+12 additions, -12 deletions)

View changed files

📝 server/routes.go (+12 -12)

📄 Description

Problem:
When using cloud models, the Content-Type header was not properly set, even though the code at line 1983 (now 1948) attempted to set it.

Cause:
Execution order was the issue:
At line 1968, client.Chat(c, &req, fn) is called.
Inside client.Chat, the callback fn (at line 1950) calls c.Writer.Write() (line 1960) to write data.
On the first Write(), Gin sends HTTP headers if they haven’t been sent yet.

At line 1987, there is an attempt to set the header after data was written — too late.
HTTP headers must be set before the response body is written; after that, headers cannot be changed.
Additionally, if client.Chat returns an error (lines 1976, 1980, 1985, 1988), the header was never set.

Solution:
The header is now set before calling client.Chat (lines 1942–1948), ensuring:
The header is set before writing data.
The header is set regardless of success.
Duplicate code that ran after writing data was removed.

Closes #13258


🔄 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/13279 **Author:** [@ZeeeUs](https://github.com/ZeeeUs) **Created:** 11/30/2025 **Status:** ✅ Merged **Merged:** 12/5/2025 **Merged by:** [@pdevine](https://github.com/pdevine) **Base:** `main` ← **Head:** `fix/api-content-type-header` --- ### 📝 Commits (2) - [`7cea92a`](https://github.com/ollama/ollama/commit/7cea92a3af3f89da88242e062ec3399e07e8fffb) fix(api): correct Content-Type header for /api/chat and /api/generate responses whe use Cloud models - [`6276399`](https://github.com/ollama/ollama/commit/6276399cd2017f5b1c198983867521d829645102) change default mime-type ### 📊 Changes **1 file changed** (+12 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `server/routes.go` (+12 -12) </details> ### 📄 Description Problem: When using cloud models, the Content-Type header was not properly set, even though the code at line 1983 (now 1948) attempted to set it. Cause: Execution order was the issue: At line 1968, client.Chat(c, &req, fn) is called. Inside client.Chat, the callback fn (at line 1950) calls c.Writer.Write() (line 1960) to write data. On the first Write(), Gin sends HTTP headers if they haven’t been sent yet. At line 1987, there is an attempt to set the header after data was written — too late. HTTP headers must be set before the response body is written; after that, headers cannot be changed. Additionally, if client.Chat returns an error (lines 1976, 1980, 1985, 1988), the header was never set. Solution: The header is now set before calling client.Chat (lines 1942–1948), ensuring: The header is set before writing data. The header is set regardless of success. Duplicate code that ran after writing data was removed. Closes #13258 --- <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 00:46:29 -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#14146