EasyStreamingPython 3
Streaming Token Assembler
Assemble interleaved streaming token deltas into complete message text.
25m3 sample tests5 hidden tests
Assemble partial streaming deltas into completed message text.
Requirements
- Define
TokenAssemblerwith instance state for partial parts and finalized IDs. apply(event)processes one event dict.- Each event has
id, optionaldelta, and optionalfinal(finalis a boolean; treat missing as false). - Append
deltatext to that message ID when the key is present (including empty-string deltas). - If
finalis true, mark the message complete and return its full text. - If not final, return
None. get(message_id)returns current assembled text orNone.- Ignore deltas for IDs already finalized.
- If
finalis true on an already-finalized id, ignore any new delta and still return the completed text (idempotent finalize). - Non-final
applyafter finalize returnsNone, leaving text unchanged. - Well-formed events are assumed: required
idpresent;deltais a string when present.
Example
python
1a = TokenAssembler()
2assert a.apply({"id": "m", "delta": "hel"}) is None
3assert a.apply({"id": "m", "delta": "lo", "final": True}) == "hello"
4assert a.apply({"id": "m", "delta": "ignored", "final": True}) == "hello"Constraints
- Support interleaved message IDs.
- Don't drop empty-string deltas.
- Finalizing twice should be idempotent.
Editor