Intermediate
Assemble a streamed reply
Write assemble(events). events is a list of data strings: JSON chunks, and possibly "[DONE]". Return a dictionary {"text": ..., "finish_reason": ..., "usage": ...}.
text is all delta.content pieces joined (some deltas have no content or null). finish_reason is the first non-null one (or None). usage is the usage object of whichever chunk has it (or None). Some chunks have an empty choices list. Stop at "[DONE]" and ignore anything after it.
What Check tests
- Joins the content pieces
- Reads the finish reason from the stop chunk
- Reads usage from a chunk with no choices
- Usage inside a chunk that also has choices
- Null or missing content is fine
- Stops at [DONE]
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.