From e173b74a37074fb772d2bf4fa309072fd813ff93 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 1 Nov 2025 17:59:04 +0000 Subject: [PATCH 1/3] Initial plan From 66ed76b4248f0b21d45d22188e5cf4d0be45db36 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 1 Nov 2025 18:04:08 +0000 Subject: [PATCH 2/3] Fix Dify TransferEncodingError by using iter_any() instead of iter_chunked() Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> --- astrbot/core/utils/dify_api_client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/astrbot/core/utils/dify_api_client.py b/astrbot/core/utils/dify_api_client.py index 2500e69a5a..2e45dffb82 100644 --- a/astrbot/core/utils/dify_api_client.py +++ b/astrbot/core/utils/dify_api_client.py @@ -11,7 +11,10 @@ async def _stream_sse(resp: ClientResponse) -> AsyncGenerator[dict, None]: decoder = codecs.getincrementaldecoder("utf-8")() buffer = "" - async for chunk in resp.content.iter_chunked(8192): + # Use iter_any() instead of iter_chunked() to avoid TransferEncodingError + # when handling large responses with chunked transfer encoding + # See: https://github.com/aio-libs/aiohttp/issues/4630 + async for chunk in resp.content.iter_any(): buffer += decoder.decode(chunk) while "\n\n" in buffer: block, buffer = buffer.split("\n\n", 1) From 6de45f58840ae08d6eee1eda1b6cf28e65756f21 Mon Sep 17 00:00:00 2001 From: LIghtJUNction Date: Sun, 2 Nov 2025 02:08:36 +0800 Subject: [PATCH 3/3] Update astrbot/core/utils/dify_api_client.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- astrbot/core/utils/dify_api_client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/astrbot/core/utils/dify_api_client.py b/astrbot/core/utils/dify_api_client.py index 2e45dffb82..336107cf93 100644 --- a/astrbot/core/utils/dify_api_client.py +++ b/astrbot/core/utils/dify_api_client.py @@ -12,8 +12,14 @@ async def _stream_sse(resp: ClientResponse) -> AsyncGenerator[dict, None]: decoder = codecs.getincrementaldecoder("utf-8")() buffer = "" # Use iter_any() instead of iter_chunked() to avoid TransferEncodingError - # when handling large responses with chunked transfer encoding + # when handling large responses with chunked transfer encoding. # See: https://github.com/aio-libs/aiohttp/issues/4630 + # Note: iter_any() yields chunks as soon as they arrive, which may be much smaller + # than the fixed 8KB chunks from iter_chunked(8192). This can result in processing + # many small chunks, potentially impacting performance for large responses. + # The trade-off is improved reliability (avoiding TransferEncodingError) at the cost + # of possible performance overhead. If performance issues are observed in production, + # consider monitoring chunk sizes and throughput, and revisit this implementation. async for chunk in resp.content.iter_any(): buffer += decoder.decode(chunk) while "\n\n" in buffer: