Parse a Streaming Server-Sent Events Completion Response

Last updated: November 29, 2026

Quick Overview

Implement an incremental parser that receives arbitrary slices of a server-sent events stream (as produced by a streaming chat completions endpoint) and returns each completed JSON event, ignoring comments and stopping at the `[DONE]` sentinel.

OpenAI
Coding & Algorithms
Software Engineer
OpenAI
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Implement an incremental parser that receives arbitrary slices of a server-sent events stream (as produced by a streaming chat completions endpoint) and returns each completed JSON event, ignoring comments and stopping at the `[DONE]` sentinel.

Streaming responses are how the OpenAI API delivers tokens as they are generated, and SDK code must reassemble events that arrive split across arbitrary network reads. Interviewers use this to check careful buffering, attention to a wire format's details, and whether you test chunk boundaries that fall mid-line.

What the Interviewer Expects
  • Buffer partial lines across feed calls rather than assuming whole events
  • Treat a blank line as the event delimiter and join multi-line data fields with newlines
  • Ignore comment lines beginning with a colon and unknown fields
  • Handle CRLF line endings
  • Stop cleanly on the [DONE] sentinel
  • Write tests where a split lands inside a JSON object
Key Topics to Cover
streaming parsing
buffering
state machines
server-sent events
JSON
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you surface a JSON decode error without losing subsequent events?
  • The bytes may split a multi-byte UTF-8 character: where do you decode?
  • How would you reconstruct a full tool call whose arguments arrive as string deltas across many events?
  • How would you add reconnection using the last event id?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • feed(piece) may be called with any slice of the stream, including half a line.
  • Lines end with \n or \r\n. A blank line ends an event.
  • data: lines accumulate; multiple data lines join wit...
High-Level Design

Keep two pieces of state: a text buffer for the incomplete current line and a list of data lines for the event being built. Each feed appends to the buffer, then repeatedly extracts complete lines. ...


Submit Your Answer
Markdown supported