From 4e9c75be508a151dc1ed1148e3aec20c21b7b42f Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 9 Aug 2025 23:49:56 +0400 Subject: [PATCH] enh: CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE --- backend/open_webui/env.py | 19 +++++++++++++++++++ backend/open_webui/utils/middleware.py | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index c3f8f637ee..7719ea421a 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -487,6 +487,25 @@ else: MODELS_CACHE_TTL = 1 +#################################### +# CHAT +#################################### + +CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE = os.environ.get( + "CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE", "1" +) + +if CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE == "": + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE = 1 +else: + try: + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE = int( + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE + ) + except Exception: + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE = 1 + + #################################### # WEBSOCKET SUPPORT #################################### diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 7ef911ea42..a433ef3a0c 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -95,6 +95,7 @@ from open_webui.config import ( from open_webui.env import ( SRC_LOG_LEVELS, GLOBAL_LOG_LEVEL, + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE, BYPASS_MODEL_ACCESS_CONTROL, ENABLE_REALTIME_CHAT_SAVE, ) @@ -1819,7 +1820,7 @@ async def process_chat_response( delta_count = 0 delta_chunk_size = max( - 1, + CHAT_RESPONSE_STREAM_DELTA_CHUNK_SIZE, int( metadata.get("params", {}).get("stream_delta_chunk_size") or 1