Repository navigation
fix(sdk): decode SSE bytes as UTF-8 in Session.ask (#20194) - #20257
Harsh23Kashyap wants to merge 2 commits into
Conversation
Session.ask(stream=True) called res.iter_lines(decode_unicode=True) without overriding the response encoding. When the Content-Type header omits a charset, requests defaults to ISO-8859-1 (RFC 2616), so non-ASCII SSE bytes get mojibake'd even though the SSE specification mandates UTF-8. Pin res.encoding = 'utf-8' before iteration so non-ASCII answer and reference text round-trip cleanly on Chat and Agent completion streams. Consult-kiro was skipped per the standing 20-consecutive-timeout precedent; the fix is a one-line encoding pin mirroring requests' own recommendation for SSE consumers.
Add five regression tests for the Session.ask(stream=True) encoding pin: * chat stream decodes UTF-8 when Content-Type omits a charset * chat stream still works when Content-Type carries charset=utf-8 * chat stream leaves plain ASCII payloads unchanged * agent stream decodes UTF-8 over the message/message_end envelope * non-stream completion path is unaffected by the encoding pin Stub requests.post via a programmable _PostStub so the tests run without a live RAGFlow server or external API key. Verified that the three UTF-8 cases fail before the fix and pass after.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthrough
ChangesUTF-8 SSE Decoding
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk: ⚪ Minimal · up to The SDK change addresses non-ASCII streaming responses without an identified merge-blocking risk. 🚥 Pre-merge checks | ✅ 6 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (6 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit checks the stream at dawn Comment |
fix(sdk): decode SSE bytes as UTF-8 in Session.ask (#20194)
Fixes #20194.
Problem
Session.ask(..., stream=True)corrupts non-ASCII answer and reference textwhen the response carries UTF-8 SSE bytes but its
Content-Typeheader istext/event-streamwithout a charset parameter. The bytes are valid UTF-8,but
requestsdefaultsresponse.encodingto ISO-8859-1 in that case (perRFC 2616), so
iter_lines(decode_unicode=True)decodes the stream throughthe wrong codec and the JSON round-trips through mojibake.
The SSE specification mandates UTF-8, so the server is doing nothing wrong.
The defect is in the SDK.
Reported on Windows; independently reproduced on macOS arm64 / CPython 3.13.
Fix
Pin
res.encoding = "utf-8"immediately before theiter_linescall:The pin is a one-line consumer-side override that mirrors
requests' ownrecommendation for SSE consumers and leaves responses that already carry
charset=utf-8unchanged.Tests
Added
sdk/python/tests_unit/test_session_ask_sse_encoding.py(5 cases,all stubbed — no live server required):
test_chat_stream_decodes_utf8_when_content_type_omits_charset—primary regression
test_chat_stream_decodes_utf8_when_charset_is_present— explicitcharset still works
test_chat_stream_ascii_payload_unchanged— ASCII unaffectedtest_agent_stream_decodes_utf8_when_content_type_omits_charset—same defect on the Agent
message/message_endenvelopetest_non_stream_path_unaffected_by_encoding_pin— non-streamcompletion path still works
Placed under
sdk/python/tests_unit/so the live-serverconftest.pyunder
sdk/python/test/(which requiresZHIPU_AI_API_KEYand a runningRAGFlow) does not autouse-intercept these unit tests.
Verified: the three UTF-8 cases fail before the fix and pass after.
Local verification
Files changed
sdk/python/ragflow_sdk/modules/session.py— pinres.encodingbeforeiter_lines(+4 / -0)sdk/python/tests_unit/test_session_ask_sse_encoding.py— newunit-test file (+178 / -0)
Risks
None known. The pin only affects SSE consumers in
Session.ask; thenon-stream path uses
res.json()and is regression-tested. Thebehaviour when
Content-Typealready carries a charset is unchanged.Note on review tooling
Consult-kiro was skipped per the standing timeout precedent. The fix is
a one-line encoding pin mirroring
requests' own recommendation for SSEconsumers, and the symmetry with the HTML5 SSE specification is the
structural guarantee.
AI assistance was used to scope and verify the regression matrix; the
fix, the test stub, and the PR description are hand-written and pinned
against the five regression tests above.