Reassemble Streamed Tool-Call Deltas From a Chat Completions Stream
Last updated: November 29, 2026
Quick Overview
Write a client function that consumes server-sent event lines from a streaming chat completion and reconstructs the final message text plus any tool calls, whose JSON arguments arrive as fragments split across chunks and possibly interleaved across calls.
Mistral AI
November 29, 20260
0
0 solved
Write a client function that consumes server-sent event lines from a streaming chat completion and reconstructs the final message text plus any tool calls, whose JSON arguments arrive as fragments split across chunks and possibly interleaved across calls.
La Plateforme and the Mistral SDKs stream completions with function calling, and SDK or Le Chat engineers must turn deltas back into usable objects. The interviewer checks careful protocol parsing, keyed accumulation by tool-call index, and robust handling of truncated or malformed streams.
What the Interviewer Expects
- Ignore non-data SSE lines such as comments and keep-alives
- Stop at the [DONE] sentinel
- Accumulate text deltas and tool-call fragments keyed by index
- Only parse argument JSON after the stream ends
- Surface truncated or invalid JSON instead of crashing
- Capture finish_reason
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you expose partial tool-call arguments to a UI while streaming?
- How do you handle a network drop mid-stream: retry or resume?
- What changes if multiple choices (n > 1) are streamed?
- How would you test this against a flaky server?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Each event is a data: {json} line; the final event is data: [DONE]. Each chunk carries choices[0].delta, which may contain content text or tool_calls entries with an index, optional id a...
High-Level Design
Line filter, then JSON decode per event, then two accumulators: a list for text and a dict index -> {id, name, args fragments} for tools. Fragments are strings, so concatenate and parse once at the ...