Repository navigation
Description
Summary
The OpenRouterStream SSE parser in crates/jcode-provider-openrouter/src/stream.rs has two related bugs that cause silent content loss and ~295 spurious "JSON parse failed" warnings per day.
Text characters get (**, `, \n), SQL syntax, code delimiters. This is not a model problem; it is an SSE parsing defect.
Bug 1: Only the last data: line is kept (content loss)
File: crates/jcode-provider-openrouter/src/stream.rs, lines 206–211
// Current (broken) code:
let mut data = None;
for line in event_str.lines() {
if let Some(d) = jcode_core::util::sse_data_line(line) {
data = Some(d); // OVERWRITES instead of accumulating!
}
}When the upstream sends events separated by \r\n\r\n (the standard HTTP/SSE delimiter), the parser does not split them because it only searches for \n\n (line 196). Multiple events pile up in the buffer. When [DONE]\n\n finally arrives, all accumulated events are parsed as one event_str — and only the last data: line survives.
Contrast with the OpenAI parser (crates/jcode-provider-openai/src/stream.rs, lines 791–802) which correctly accumulates all data lines:
// Correct pattern used by OpenAI parser:
let mut data_lines = Vec::new();
for line in event_str.lines() {
if let Some(data) = sse_data_line(line) {
data_lines.push(data);
}
}
let data = data_lines.join("\n");Simulation
When 3 events with content "hello", " world", "!" arrive followed by [DONE]\n\n:
find("\n\n")skips\r\n\r\nboundaries between events- All 3 events +
[DONE]are combined into oneevent_str - The
data = Some(d)loop walks all 4data:lines butdataends up as"[DONE]" - Message content
"hello world!"is silently dropped with no warning
Bug 2: Missing \n before data: prefix (JSON parse failures)
In the same parser, when a data: line lacks a leading \n separator from the previous line, the data: prefix of the next event is concatenated directly to the first JSON payload. This produces invalid JSON like:
{"id":"e70a2a07-d742-: {"id":"e70a2a07-d742-413c-98a4-0b70a6e0c1e0",...
These fail JSON parse and produce the 295/day "SSE JSON parse failed" warnings.
Impact
- Random text loss in LLM output: markdown markers (
**,`,\n), parentheses, code, Chinese characters silently disappear - Syntax errors in generated code, SQL, markdown because boundary characters are lost
- 295 spurious log warnings per day from parse failures
- Many more bytes silently dropped without any warning
Suggested Fix
- Search for both
\r\n\r\nand\n\nas SSE event boundaries - Use a
Vecto accumulate alldata:lines like the OpenAI parser does - Handle
\rline endings properly
The fix is approximately 8 lines changed in a single file.
Activity
1jehuang commentedon Jul 24, 2026
Confirmed and fixed. The OpenRouter SSE parser now splits events on
\r\n\r\nas well as\n\n, and collects everydata:line instead of keeping only the last one, so batched events no longer silently drop content. Added two regression tests (take_sse_event_splits_crlf_delimited_events,parse_next_event_keeps_all_content_across_crlf_batched_events) plus one for multiple data lines in a single event; cargo test -p jcode-provider-openrouter: 20 passed. Thanks for the detailed diagnosis and the comparison with the OpenAI parser.— Jcode agent (automated triage), on behalf of @1jehuang
1jehuang commentedon Jul 24, 2026
Note: the fix is committed on branch
triage/2026-07-24-batch(pushed). GitHub's pull-request creation API is currently returning 500s repo-wide, so the PR link will follow once that clears.— Jcode agent (automated triage), on behalf of @1jehuang
1jehuang commentedon Jul 24, 2026
GitHub's PR API recovered: the fix is now in #580.
— Jcode agent (automated triage), on behalf of @1jehuang
fix(openrouter): collect all SSE data lines and split CRLF event boun…
fix(openrouter): collect all SSE data lines and split CRLF event boun…
fix(openrouter): collect all SSE data lines and split CRLF event boun…
fix(openrouter): collect all SSE data lines and split CRLF event boun…
fix(openrouter): collect all SSE data lines and split CRLF event boun…