Skip to content

fix(streaming): decode SSE lines strictly per WHATWG CR/LF/CRLF rules - #46

Open
wssab314 wants to merge 1 commit into
QoderAI:mainfrom
wssab314:fix/sse-whatwg-line-splitting
Open

wssab314 wants to merge 1 commit into
QoderAI:mainfrom
wssab314:fix/sse-whatwg-line-splitting

Conversation

@wssab314

Copy link
Copy Markdown

Summary

  • Replace response.iter_lines() / aiter_lines() in Stream and AsyncStream (src/qca/common/_streaming.py) with incremental UTF-8 decoding over iter_bytes() / aiter_bytes(), splitting strictly on \r\n, \r, and \n per the WHATWG SSE specification.
  • Handle leading UTF-8 BOM and \r\n / multi-byte UTF-8 sequences split across arbitrary chunk boundaries while preserving _InterruptibleSyncBody sync cancellation.
  • Add sync and async regression tests in tests/test_streaming.py and an [Unreleased] changelog entry.

Fixes #45

Why

httpx's iter_lines() delegates to Python's str.splitlines(), which splits on Unicode line separators such as \u2028, \u2029, and \x85. When agent output includes these valid JSON characters inside an SSE data: payload, the frame was split mid-JSON and raised APIResponseValidationError.

Verification

  • uv run make lint (ruff check . + ruff format --check)
  • uv run make typecheck (mypy src/qca scripts)
  • uv run make test (pytest -q)
  • uv run make docs-check (python scripts/docs-check.py)
  • Example --help smoke checks (no public signatures changed)
  • Account-backed integration tests not required (offline SSE framing fix)

Commands and results:

uv run --extra dev --locked ruff check . -> All checks passed!
uv run --extra dev --locked ruff format --check src tests examples scripts -> 786 files already formatted
uv run --extra dev --locked mypy src/qca scripts -> Success: no issues found in 687 source files
uv run --extra dev --locked pytest -q -> 4558 passed, 73 deselected
uv run --extra dev --locked python scripts/docs-check.py -> docs-check: PASS
python3 .github/scripts/release_notes.py check -> Changelog is valid

Impact

  • Public API or behavior changed (bugfix: SSE payloads with \u2028/\u2029/\x85 no longer fail parsing)
  • User-facing changes added to CHANGELOG.md under Unreleased
  • Synchronous and asynchronous clients remain aligned
  • Generated API documentation updated (verified with docs-check.py, no public API doc drift)
  • Contract fixtures updated (not applicable)
  • Go and TypeScript parity considered (Python-specific str.splitlines() behavior in httpx.LineDecoder)
  • Migration notes added for a breaking change (non-breaking)
  • No credentials, .env.live, logs, or generated test output committed

Decode SSE streams incrementally over iter_bytes()/aiter_bytes() and split strictly on CR, LF, or CRLF instead of relying on httpx iter_lines() (which splits on Unicode line separators such as U+2028, U+2029, and U+0085 inside JSON payloads).

Fixes QoderAI#45
@wssab314
wssab314 requested a review from moonyue-w as a code owner November 24, 2026 05:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant