From 0f54aee859b322f9e13b34e0a2b0c87d57ea7708 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:18:51 +0100 Subject: [PATCH] perf: large profile pictures and knowledge exports load much faster (#34598) Large profile pictures of users, models and channel webhooks (animated ones, or ones set through the API) were sent out in thousands of small pieces, each handled separately, and the knowledge base export sent its zip file the same way. A 2 MB picture took around 200 to 400 ms to load, 20 loads of it at once, as on a page full of avatars, took 9 to 15 seconds, and an 8 MB export spent close to a second just sending the file. Both are now sent as one complete response with the same content type and headers, plus a size header in place of chunked sending. | Picture | One load before | One load after | 20 at once before | 20 at once after | | --- | --- | --- | --- | --- | | Model, 2 MB | 207 ms | 15 ms | 9.3 s | 0.48 s | | User, 2 MB | 379 ms | 15 ms | 15.0 s | 0.34 s | | Model, 5 MB | 743 ms | 39 ms | 33.6 s | 0.63 s | | User, 5 MB | 627 ms | 25 ms | 30.7 s | 0.54 s | Fixes #34597 --- backend/open_webui/routers/channels.py | 8 +++----- backend/open_webui/routers/knowledge.py | 8 +++----- backend/open_webui/routers/models.py | 7 +++---- backend/open_webui/routers/users.py | 8 +++----- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/backend/open_webui/routers/channels.py b/backend/open_webui/routers/channels.py index 91731c920c..62388bd3e9 100644 --- a/backend/open_webui/routers/channels.py +++ b/backend/open_webui/routers/channels.py @@ -1,12 +1,11 @@ import base64 -import io import logging import time import uuid from typing import Optional from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Request, status -from fastapi.responses import FileResponse, Response, StreamingResponse +from fastapi.responses import FileResponse, Response from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT from open_webui.constants import ERROR_MESSAGES from open_webui.env import ENABLE_PROFILE_IMAGE_URL_FORWARDING, STATIC_DIR @@ -2099,11 +2098,10 @@ async def get_webhook_profile_image( try: header, base64_data = webhook.profile_image_url.split(',', 1) image_data = base64.b64decode(base64_data) - image_buffer = io.BytesIO(image_data) media_type = header.split(';')[0].lstrip('data:') - return StreamingResponse( - image_buffer, + return Response( + content=image_data, media_type=media_type, headers={'Content-Disposition': 'inline'}, ) diff --git a/backend/open_webui/routers/knowledge.py b/backend/open_webui/routers/knowledge.py index 2c57490b83..fde3a282df 100644 --- a/backend/open_webui/routers/knowledge.py +++ b/backend/open_webui/routers/knowledge.py @@ -10,7 +10,7 @@ from typing import List, Optional from urllib.parse import quote from fastapi import APIRouter, Depends, HTTPException, Query, Request, status -from fastapi.responses import StreamingResponse +from fastapi.responses import Response, StreamingResponse from open_webui.config import ( BYPASS_ADMIN_ACCESS_CONTROL, ENABLE_KNOWLEDGE_FILE_RETENTION, @@ -2311,14 +2311,12 @@ async def export_knowledge_by_id(id: str, user=Depends(get_admin_user), db: Asyn filename = f'{filename}.txt' zf.writestr(filename, content) - zip_buffer.seek(0) - # Sanitize knowledge name for filename safe_name = ''.join(c if c.isalnum() or c in ' -_' else '_' for c in knowledge.name) zip_filename = f'{safe_name}.zip' - return StreamingResponse( - zip_buffer, + return Response( + content=zip_buffer.getvalue(), media_type='application/zip', headers={'Content-Disposition': f"attachment; filename*=UTF-8''{quote(zip_filename, safe='')}"}, ) diff --git a/backend/open_webui/routers/models.py b/backend/open_webui/routers/models.py index 918700b290..2ce450eab2 100644 --- a/backend/open_webui/routers/models.py +++ b/backend/open_webui/routers/models.py @@ -18,7 +18,7 @@ from fastapi import ( Response, status, ) -from fastapi.responses import RedirectResponse, StreamingResponse +from fastapi.responses import RedirectResponse from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL from open_webui.constants import ERROR_MESSAGES from open_webui.env import ( @@ -1020,7 +1020,6 @@ async def get_model_profile_image( try: header, base64_data = profile_image_url.split(',', 1) image_data = base64.b64decode(base64_data) - image_buffer = io.BytesIO(image_data) media_type = header.split(';')[0].lstrip('data:').lower() # only serve known-safe raster types inline; reject SVG/unknown (can run script on our origin) @@ -1040,8 +1039,8 @@ async def get_model_profile_image( if updated_at: headers['ETag'] = f'"{updated_at}"' - return StreamingResponse( - image_buffer, + return Response( + content=image_data, media_type=media_type, headers=headers, ) diff --git a/backend/open_webui/routers/users.py b/backend/open_webui/routers/users.py index 3d33d56e71..74516d0e2c 100644 --- a/backend/open_webui/routers/users.py +++ b/backend/open_webui/routers/users.py @@ -1,7 +1,6 @@ from __future__ import annotations import base64 -import io import logging import time from collections import Counter @@ -10,7 +9,7 @@ from datetime import datetime, timedelta from typing import Optional from fastapi import APIRouter, Depends, HTTPException, Query, Request, status -from fastapi.responses import FileResponse, Response, StreamingResponse +from fastapi.responses import FileResponse, Response from open_webui.config import DEFAULT_USER_PERMISSIONS from open_webui.constants import ERROR_MESSAGES from open_webui.events import EVENTS, publish_event @@ -850,14 +849,13 @@ async def get_user_profile_image_by_id(user_id: str, user=Depends(get_verified_u try: header, base64_data = user.profile_image_url.split(',', 1) image_data = base64.b64decode(base64_data) - image_buffer = io.BytesIO(image_data) media_type = header.split(';')[0].lstrip('data:').lower() if media_type not in PROFILE_IMAGE_ALLOWED_MIME_TYPES: return FileResponse(f'{STATIC_DIR}/user.png') - return StreamingResponse( - image_buffer, + return Response( + content=image_data, media_type=media_type, headers={ 'Content-Disposition': 'inline',