[PR #24838] fix(images): accept non-standard Content-Type in image generation responses #131550

Open
opened 2026-05-21 17:11:21 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/24838
Author: @first-it-consulting
Created: 5/17/2026
Status: 🔄 Open

Base: devHead: fix/image-gen-content-type-ndjson-v2


📝 Commits (1)

  • dbb90ed fix(images): pass content_type=None to r.json() to accept non-standard MIME types

📊 Changes

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

View changed files

📝 backend/open_webui/routers/images.py (+5 -5)

📄 Description

Pull Request Checklist

  • Linked Issue/Discussion: Relates to [PR #1948] [CLOSED] chore(deps): bump the python-packages group across 1 directory with 8 updates (#20848)
  • Target branch: This PR targets dev.
  • Description: Provided below.
  • Changelog: Added below.
  • Dependencies: No new dependencies.
  • Testing: Manually tested against Ollama v0.21.2 with a Flux image generation model — see details below.
  • Agentic AI Code: This fix has gone through human review and manual end-to-end testing (image successfully generated and returned).
  • Code review: Self-reviewed.
  • Git Hygiene: Single atomic commit.
  • Title Prefix: fix

Changelog Entry

Description

aiohttp's ClientResponse.json() validates the Content-Type response header against "application/json" by default and raises ContentTypeError for any other value. This silently breaks image generation for backends that return valid JSON with a non-standard MIME type.

The immediate trigger: Ollama v0.21.x returns Content-Type: application/x-ndjson from its OpenAI-compatible /v1/images/generations endpoint, even though the response body is a single, fully-formed JSON object. Every image generation request via Ollama therefore fails with:

aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: application/x-ndjson'

The same issue can affect any backend returning valid JSON with a non-standard MIME type (e.g. text/plain, application/x-ndjson).

Fixed

  • Image generation and editing no longer fail with ContentTypeError when a backend returns valid JSON with a non-standard Content-Type header (e.g. Ollama returning application/x-ndjson). Fix covers the openai, gemini, and automatic1111 engines in both image_generations and image_edits.

Technical Details

aiohttp.ClientResponse.json() exposes a content_type parameter (default "application/json") for exactly this case. Passing content_type=None skips the header check while keeping all other parsing behaviour identical — this is the documented aiohttp pattern for lenient content-type handling.

Diff: 5 lines changed (one per affected r.json() call site).

Reproduction steps (before fix):

  1. Configure Open WebUI image generation engine to openai, point api_base_url at a local Ollama instance.
  2. Select an image generation model (e.g. x/flux2-klein:latest).
  3. Request an image — generation completes on the Ollama side but Open WebUI returns an error.
  4. In logs: ContentTypeError: Attempt to decode JSON with unexpected mimetype: application/x-ndjson

Verification (after fix):
Tested against Ollama v0.21.2 with model x/flux2-klein:latest. The response body ({"created": ..., "data": [{"b64_json": "..."}]}) is parsed correctly and the generated image is returned to the client.

Additional Information

  • Related issue: #20848 (Ollama image generation connection)
  • Related closed issue: #4915 (Ollama sends Content-Type: application/x-ndjson)

Contributor License Agreement


🔄 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/open-webui/open-webui/pull/24838 **Author:** [@first-it-consulting](https://github.com/first-it-consulting) **Created:** 5/17/2026 **Status:** 🔄 Open **Base:** `dev` ← **Head:** `fix/image-gen-content-type-ndjson-v2` --- ### 📝 Commits (1) - [`dbb90ed`](https://github.com/open-webui/open-webui/commit/dbb90ed4448204dd0dc0c22577992f3dbd1be6e8) fix(images): pass content_type=None to r.json() to accept non-standard MIME types ### 📊 Changes **1 file changed** (+5 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/routers/images.py` (+5 -5) </details> ### 📄 Description <!-- ⚠️ CRITICAL CHECKS FOR CONTRIBUTORS (READ, DON'T DELETE) ⚠️ 1. Target the `dev` branch. PRs targeting `main` will be automatically closed. 2. Do NOT delete the CLA section at the bottom. It is required for the bot to accept your PR. --> # Pull Request Checklist - [x] **Linked Issue/Discussion:** Relates to #20848 - [x] **Target branch:** This PR targets `dev`. - [x] **Description:** Provided below. - [x] **Changelog:** Added below. - [x] **Dependencies:** No new dependencies. - [x] **Testing:** Manually tested against Ollama v0.21.2 with a Flux image generation model — see details below. - [x] **Agentic AI Code:** This fix has gone through human review and manual end-to-end testing (image successfully generated and returned). - [x] **Code review:** Self-reviewed. - [x] **Git Hygiene:** Single atomic commit. - [x] **Title Prefix:** `fix` --- # Changelog Entry ### Description `aiohttp`'s `ClientResponse.json()` validates the `Content-Type` response header against `"application/json"` by default and raises `ContentTypeError` for any other value. This silently breaks image generation for backends that return valid JSON with a non-standard MIME type. The immediate trigger: **Ollama v0.21.x returns `Content-Type: application/x-ndjson`** from its OpenAI-compatible `/v1/images/generations` endpoint, even though the response body is a single, fully-formed JSON object. Every image generation request via Ollama therefore fails with: ``` aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: application/x-ndjson' ``` The same issue can affect any backend returning valid JSON with a non-standard MIME type (e.g. `text/plain`, `application/x-ndjson`). ### Fixed - Image generation and editing no longer fail with `ContentTypeError` when a backend returns valid JSON with a non-standard `Content-Type` header (e.g. Ollama returning `application/x-ndjson`). Fix covers the `openai`, `gemini`, and `automatic1111` engines in both `image_generations` and `image_edits`. --- ### Technical Details `aiohttp.ClientResponse.json()` exposes a `content_type` parameter (default `"application/json"`) for exactly this case. Passing `content_type=None` skips the header check while keeping all other parsing behaviour identical — this is the [documented aiohttp pattern](https://docs.aiohttp.org/en/stable/client_reference.html#aiohttp.ClientResponse.json) for lenient content-type handling. **Diff:** 5 lines changed (one per affected `r.json()` call site). **Reproduction steps (before fix):** 1. Configure Open WebUI image generation engine to `openai`, point `api_base_url` at a local Ollama instance. 2. Select an image generation model (e.g. `x/flux2-klein:latest`). 3. Request an image — generation completes on the Ollama side but Open WebUI returns an error. 4. In logs: `ContentTypeError: Attempt to decode JSON with unexpected mimetype: application/x-ndjson` **Verification (after fix):** Tested against Ollama v0.21.2 with model `x/flux2-klein:latest`. The response body (`{"created": ..., "data": [{"b64_json": "..."}]}`) is parsed correctly and the generated image is returned to the client. ### Additional Information - Related issue: #20848 (Ollama image generation connection) - Related closed issue: #4915 (Ollama sends `Content-Type: application/x-ndjson`) --- ### Contributor License Agreement - [x] By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <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-05-21 17:11:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#131550