Skip to content

Fix/react headless OpenAI sse decoder#338

Open
boffin-dmytro wants to merge 2 commits intothesysdev:mainfrom
boffin-dmytro:fix/react-headless-openai-sse-decoder
Open

Fix/react headless OpenAI sse decoder#338
boffin-dmytro wants to merge 2 commits intothesysdev:mainfrom
boffin-dmytro:fix/react-headless-openai-sse-decoder

Conversation

@boffin-dmytro
Copy link

What’s in this PR

This PR makes OpenAI streaming in @openuidev/react-headless more reliable by introducing a small internal SSE decoder and using it in the OpenAI SSE adapters.

Previously, the OpenAI adapters parsed streams by splitting each decoded network chunk on \n and treating each data: line as complete JSON. In real-world streaming, chunk boundaries are arbitrary (JSON can be split mid-token), and SSE events are framed by blank lines and can include multi-line data: fields.

Changes

  • Added an internal SSE decoder (decodeSSE) that:
    • buffers across chunks
    • supports CRLF (\r\n)
    • supports multiline data: (joined with \n per SSE spec)
    • uses strict SSE framing (does not flush a partial final block at EOF)
  • Migrated:
    • openAIAdapter() (Chat Completions SSE)
    • openAIResponsesAdapter() (Responses API SSE)
      to consume the shared decoder instead of doing ad-hoc line splitting.
  • Added focused tests:
    • unit tests for the SSE decoder
    • regression tests proving OpenAI SSE events can be split across chunks without being dropped

Why it matters

This hardens streaming behavior under normal network conditions and reduces duplicated parsing logic across adapters—making streaming output less flaky and easier to maintain.

Scope note

agUIAdapter() is intentionally unchanged in this PR (it already has robust SSE framing); this change focuses on bringing the OpenAI SSE adapters up to the same standard.

How to test

corepack pnpm --filter @openuidev/react-headless test

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.

1 participant