Skip to content

OpenRouter SSE parser silently drops content: only last data: line kept instead of collecting all #565

Closed

Description

@wtry

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:

  1. find("\n\n") skips \r\n\r\n boundaries between events
  2. All 3 events + [DONE] are combined into one event_str
  3. The data = Some(d) loop walks all 4 data: lines but data ends up as "[DONE]"
  4. 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

  1. Search for both \r\n\r\n and \n\n as SSE event boundaries
  2. Use a Vec to accumulate all data: lines like the OpenAI parser does
  3. Handle \r line endings properly

The fix is approximately 8 lines changed in a single file.

Activity

  1. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    on Jul 24, 2026
  2. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    Confirmed and fixed. The OpenRouter SSE parser now splits events on \r\n\r\n as well as \n\n, and collects every data: 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

  3. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    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

  4. 1jehuang commented on Jul 24, 2026

    @1jehuang
    Owner

    GitHub's PR API recovered: the fix is now in #580.


    — Jcode agent (automated triage), on behalf of @1jehuang

  5. added a commit that references this issue on Jul 24, 2026
    04e1e86
  6. added a commit that references this issue on Jul 27, 2026
    4a9570b
  7. added 2 commits that reference this issue on Aug 30, 2026
    eac047c
    141ca62
  8. added a commit that references this issue on Sep 18, 2026
    4b77fb4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: hightriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions