Repository navigation
fix(api): split agent SSE frames only on newline characters - #20263
Jinzhengxu wants to merge 3 commits into
Conversation
canvas_service.completion serializes each frame with json.dumps(..., ensure_ascii=False), so U+2028, U+2029 and U+0085 in answer or reference text stay raw inside the JSON payload. The channel bridge and the non-streaming agentbots completion split the stream with str.splitlines(), which also breaks on those characters and cuts the frame in two. The channel bridge then fails json.loads and replies with the generic error message; agentbots silently drops the frame, losing its content or the final message_end reference. Split on "\n" only, which is how the frames are delimited. Claude-Session: https://claude.ai/code/session_01KZLM8vR86APm11sQxpnnHd
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
6ba3i
left a comment
There was a problem hiding this comment.
the same framing bug remains in the Python SDK. Session.ask(stream=True) still uses iter_lines() without an LF-only delimiter, so U+2028/U+2029/U+0085 can split JSON frames and silently drop streamed messages or references. PR #20257 only fixes UTF-8 decoding, not frame splitting.
Please use delimiter="\n" there and add regression coverage.
|
Thanks — pushed 60d7cfd. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 0.27.x #20263 +/- ##
==========================================
+ Coverage 29.68% 38.06% +8.37%
==========================================
Files 54 54
Lines 15511 15567 +56
Branches 125 125
==========================================
+ Hits 4605 5926 +1321
+ Misses 10892 9613 -1279
- Partials 14 28 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@Jinzhengxu please help to fix CI failure. |
Session.ask now calls iter_lines(decode_unicode=True, delimiter="\n"), so the stub in test_create_session_with_chat_assistant.py raised TypeError in the two streaming tests that use it.
|
@wangq8 Fixed in the latest commit. The two failures came from |
Summary
canvas_service.completionwrites each agent SSE frame as"data:" + json.dumps(ans, ensure_ascii=False) + "\n\n". Withensure_ascii=False, U+2028, U+2029 and U+0085 in the answer, in node outputs or in themessage_endreference (retrieved chunk text) stay raw inside the JSON. Control characters such as\x0band\x0care still escaped. Two in-process consumers split this stream withstr.splitlines(), which also breaks on those three characters, so the frame is cut in the middle of its JSON:api/channels/bootstrap.py(IM bots connected to an Agent) parses every frame, sojson.loadsraises and the bot replies with "抱歉,当前无法处理这条消息,请稍后重试。" instead of the answer. One such character anywhere in a retrieved chunk is enough, because themessage_endframe carries the reference.api/apps/restful_apis/bot_api.py(POST /agentbots/<agent_id>/completionswithstream: false) skips the malformed frame. Its content goes missing from the answer, and if the frame ismessage_end, the reference is lost too.Both call sites now split on
"\n", which is the only delimiter these frames use. #20029 fixed the JSONL parser the same way.Tests
test/unit_test/api/channels/test_agent_channel_bridge.py::test_agent_reply_keeps_frames_with_unicode_line_separatorsruns the channel handler against a stubbed agent completion.test/unit_test/api/apps/restful_apis/test_agentbots_access_control.py::test_completions_keep_frames_with_unicode_line_separatorsreuses that file'sbot_apiloader, which gains aframesoverride. It checks both the content and themessage_endreference.Both tests are parametrized over U+2028, U+2029 and U+0085. All 6 cases fail on
main(313ca90) and pass with this change. The other 9 tests in the two files still pass.ruff checkandruff format --checkpass on the touched files (ruff 0.14.14). Environment: Python 3.13.12, pytest 9.0.2.SDK:
sdk/python/ragflow_sdk/modules/session.pyread the stream withrequests'iter_lines(decode_unicode=True), which also splits onstr.splitlines()boundaries. It now passesdelimiter="\n";test/unit_test/sdk/test_ask_stream_line_separators.pycovers chat and agent streams with each of the three separators.