-
-
Notifications
You must be signed in to change notification settings - Fork 968
Fix Dify TransferEncodingError on large responses #3245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…unked() Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
|
@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.