Fix Dify TransferEncodingError on large responses#3245
Fix Dify TransferEncodingError on large responses#3245
Conversation
…unked() Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a potential TransferEncodingError in the Dify API client by replacing iter_chunked() with iter_any() for processing server-sent events (SSE) streams.
- Changes the chunk iteration method in the SSE stream parser from
iter_chunked(8192)toiter_any()to handle large responses more reliably - Adds documentation explaining the rationale and references the aiohttp issue
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@copilot 但是我后面看:aio-libs/aiohttp#4846 |
You're right that aiohttp PR #4846 fixed the chunked transfer encoding issue. However, the project is already using Since the error still occurred in version 4.3.3 despite using a newer aiohttp, this suggests either:
The change to
Should I revert this change, or would you like me to investigate further why the error persists even with the aiohttp fix? |
|
@copilot 你先不要管这些,先看看还有没有其他可能性 |
I've investigated other possibilities. The root cause is likely improper ClientSession configuration rather than just the Issues found:
Comparison with working Coze client:
The TransferEncodingError likely occurs because long-lived connections without proper timeout/keepalive configuration disconnect prematurely when handling large responses. Should I implement proper ClientSession configuration following the Coze pattern? |
Dify API requests intermittently fail with
TransferEncodingError: 400, message='Not enough data to satisfy transfer length header'when responses contain large payloads (multiple paragraphs, code blocks).Root Cause
The
_stream_sse()function usesresp.content.iter_chunked(8192)which fails to handle chunked transfer-encoding end-of-stream markers correctly, causing premature connection closure. This is a known aiohttp issue: aio-libs/aiohttp#4630Changes
iter_chunked(8192)withiter_any()in SSE stream handleriter_any()yields chunks as received without fixed sizing and properly handles stream terminationFile modified:
astrbot/core/utils/dify_api_client.pyOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.