Reassemble Streamed Completions From an OpenAI-Compatible SSE Feed
Last updated: November 29, 2026
Quick Overview
Write an incremental parser that consumes raw server-sent-event text from a chat completions stream, arriving in arbitrary network chunks, and returns the content deltas plus the finish reason. It must handle keep-alive comments, the [DONE] sentinel, and lines split across chunks.
Inception
November 29, 20260
0
0 solved
Write an incremental parser that consumes raw server-sent-event text from a chat completions stream, arriving in arbitrary network chunks, and returns the content deltas plus the finish reason. It must handle keep-alive comments, the [DONE] sentinel, and lines split across chunks.
Inception serves Mercury through an OpenAI-compatible API, so SDKs, playgrounds and customer integrations all parse SSE streams. A diffusion model can emit large deltas in bursts, which makes chunk-boundary bugs easy to hit. The interviewer checks careful buffering and protocol details rather than clever algorithms.
What the Interviewer Expects
- Buffer partial lines instead of assuming one event per network read
- Ignore comment lines and non-data fields without crashing
- Stop cleanly at the [DONE] sentinel and ignore anything after it
- Skip deltas with no content such as the initial role delta
- Test with the same stream split into tiny chunks
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support multiple choices (n > 1) in one stream?
- How do you surface a mid-stream error event to the caller?
- What changes if the server sends multi-line data fields per the SSE spec?
- How would you measure time to first token from the client side?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a sequence of text chunks of any size. Each SSE event is one or more lines; we care about data: lines holding JSON with choices[].delta.content, and data: [DONE] ends the stream. Lines ...
High-Level Design
Keep a string buffer. On every feed, append the chunk and repeatedly split off complete lines at \n, leaving any trailing partial line in the buffer for the next call. Each complete line is classi...