Skip to content

fix(streaming): SSE stream crashes on Unicode line separators (\u2028, \u2029, \x85) inside JSON payloads #45

Backlog

Description

@wssab314

Summary

Both Stream and AsyncStream fail with APIResponseValidationError whenever an SSE data: JSON payload contains unescaped Unicode line separators such as U+2028 (Line Separator), U+2029 (Paragraph Separator), or U+0085 (NEL) — characters that are valid inside RFC 8259 JSON strings and frequently appear in web, PDF, or JS content produced by agents.

Root Cause

Stream._iter_events() and AsyncStream._iter_events() (src/qca/common/_streaming.py) rely on httpx.Response.iter_lines() / aiter_lines(). httpx's LineDecoder splits using Python's str.splitlines(), which treats \u2028, \u2029, \x85, \x0b, \x0c, etc. as line breaks. Per the WHATWG Server-Sent Events specification, SSE lines may only be delimited by \r\n, \n, or \r.

Minimal Repro

Feeding data: {"content":[{"type":"text","text":"before\u2028after"}]}\n\n into Stream splits the JSON across two lines and raises APIResponseValidationError.

Proposed Fix

  • Decode SSE lines incrementally over iter_bytes() / aiter_bytes() using codecs.getincrementaldecoder, splitting strictly on \r\n, \r, or \n (preserving split CRLF across chunks, leading UTF-8 BOM handling, and _InterruptibleSyncBody sync cancellation).
  • Add sync and async regression tests covering \u2028, \u2029, \x85, control characters in comments, standalone \r, leading BOM, and 1-byte chunk boundaries.

I have a verified fix ready and will open a PR referencing this issue shortly.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions