Skip to content

fix(api): split agent SSE frames only on newline characters - #20263

Open
Jinzhengxu wants to merge 3 commits into
infiniflow:0.27.xfrom
Jinzhengxu:fix/sse-frames-unicode-line-separators
Open

Jinzhengxu wants to merge 3 commits into
infiniflow:0.27.xfrom
Jinzhengxu:fix/sse-frames-unicode-line-separators

Conversation

@Jinzhengxu

@Jinzhengxu Jinzhengxu commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

canvas_service.completion writes each agent SSE frame as "data:" + json.dumps(ans, ensure_ascii=False) + "\n\n". With ensure_ascii=False, U+2028, U+2029 and U+0085 in the answer, in node outputs or in the message_end reference (retrieved chunk text) stay raw inside the JSON. Control characters such as \x0b and \x0c are still escaped. Two in-process consumers split this stream with str.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, so json.loads raises and the bot replies with "抱歉,当前无法处理这条消息,请稍后重试。" instead of the answer. One such character anywhere in a retrieved chunk is enough, because the message_end frame carries the reference.
  • api/apps/restful_apis/bot_api.py (POST /agentbots/<agent_id>/completions with stream: false) skips the malformed frame. Its content goes missing from the answer, and if the frame is message_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_separators runs 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_separators reuses that file's bot_api loader, which gains a frames override. It checks both the content and the message_end reference.

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 check and ruff format --check pass on the touched files (ruff 0.14.14). Environment: Python 3.13.12, pytest 9.0.2.

SDK: sdk/python/ragflow_sdk/modules/session.py read the stream with requests' iter_lines(decode_unicode=True), which also splits on str.splitlines() boundaries. It now passes delimiter="\n"; test/unit_test/sdk/test_ask_stream_line_separators.py covers chat and agent streams with each of the three separators.

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
@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Walkthrough

The agent-bot and channel completion parsers now split SSE input only on newline characters. Tests verify that U+2028, U+2029, and U+0085 remain in serialized message content and references.

Changes: SSE parsing

Layer / File(s) Summary
Newline-only frame parsing
api/apps/restful_apis/bot_api.py, api/channels/bootstrap.py
Both parsers now split input on "\n" instead of splitlines().
Unicode separator coverage
test/unit_test/api/apps/restful_apis/test_agentbots_access_control.py, test/unit_test/api/channels/test_agent_channel_bridge.py
Parameterized tests check that completion content, references, and channel replies preserve U+2028, U+2029, and U+0085.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: wangq8, lynn-inf

Merge Risk: Minimal · up to 43a97

The change preserves Unicode separators in agent completions and channel replies. No merge-blocking issue is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly and concisely describes the primary change: agent SSE frames now split only on newline characters.
Description check Passed The PR description is thorough and matches the intent of the change. It gives a Summary-level background, the root cause (ensure_ascii=False leaves U+2028, U+2029, and U+0085 raw, and str.splitlines()…
Full details: Description check

Explanation

The PR description is thorough and matches the intent of the change. It gives a Summary-level background, the root cause (ensure_ascii=False leaves U+2028, U+2029, and U+0085 raw, and str.splitlines() splits on them), the affected call sites (api/channels/bootstrap.py and api/apps/restful_apis/bot_api.py), the user-visible impact, the tests added, the test results on main and on the branch, lint results, and an SDK note. The template asks only for a Summary section that explains the purpose and background, and the description provides that and more. Minor note: the description contains an HTML-escaped entity (&lt;agent_id&gt;) that renders as a literal tag in some Markdown viewers, which is cosmetic.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

I’m a rabbit, checking each line,
These separators stay in the sign.
The frames split where newlines appear,
My carrots and content stay clear.
I hop through the tests, then away,
With Unicode safe for the day.

ℹ️ Recent review info

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 2dc0f2b5-97bd-4025-adb0-668f6443360e

📥 Commits

Reviewing files that changed from the base of the PR and between 313ca90 and 43a97cf.

📒 Files selected for processing (4)

  • api/apps/restful_apis/bot_api.py
  • api/channels/bootstrap.py
  • test/unit_test/api/apps/restful_apis/test_agentbots_access_control.py
  • test/unit_test/api/channels/test_agent_channel_bridge.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@JinHai-CN
JinHai-CN changed the base branch from main to 0.27.x September 28, 2026 09:32
@wangq8
wangq8 requested a review from 6ba3i October 19, 2026 10:06

@6ba3i 6ba3i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wangq8 wangq8 added the ci Continue Integration label Oct 20, 2026
@Jinzhengxu

Copy link
Copy Markdown
Contributor Author

Thanks — pushed 60d7cfd. Session.ask(stream=True) now calls iter_lines(decode_unicode=True, delimiter="\n"), and test/unit_test/sdk/test_ask_stream_line_separators.py streams chat and agent SSE with raw U+2028/U+2029/U+0085 in the message text and reference chunks (6 cases: fail before the change, pass after; the rest of test/unit_test/sdk still passes).

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 20.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 38.06%. Comparing base (313ca90) to head (97219da).
⚠️ Report is 130 commits behind head on 0.27.x.

Files with missing lines Patch % Lines
api/apps/restful_apis/bot_api.py 0.00% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wangq8

wangq8 commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

@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.
@Jinzhengxu

Copy link
Copy Markdown
Contributor Author

@wangq8 Fixed in the latest commit. The two failures came from _DummyStreamResponse.iter_lines() in test_create_session_with_chat_assistant.py, which did not accept the new delimiter argument. The stub now accepts it; production code is unchanged.

@yuzhichang
yuzhichang requested a review from wangq8 November 18, 2026 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants