Practice in Lab: Stop token decoding across chunk boundaries →
Stop token decoding across chunk boundaries
Network chunks are transport boundaries, not token or stop-sequence boundaries. A multi-token delimiter can begin in one chunk and finish in the next. This decoder preserves accumulated token identities, gives a complete stop precedence over the budget check on the same token and reports what it consumed. The hard budget deliberately ends decoding even when the last token could start a longer delimiter. Production streaming adapters must decide whether to buffer such prefixes and how to count provider usage; this exercise makes that unresolved policy visible.
output = []
for chunk in [[1,7],[8,9]]:
for token in chunk:
output.append(token)
if output[-2:]==[7,8]:
print(output[:-2])
break
else:
continue
breakReview the design
Review overlapping stops, chunk splitting, budget precedence and incomplete prefixes. Token identities are fixtures; no model tokenizer or network stream is claimed to run here.
Your task
chunks and stops are lists of nonnegative integer-token lists; stop sequences cannot be empty. Consume chunks incrementally, retaining output across chunk boundaries. After each token, first check complete stop suffixes and remove the longest matching stop; otherwise stop when output length reaches integer max_tokens>=0. Return tokens, reason (stop,budget,eof), consumed. Stop tokens count as consumed. A hard budget may expose an incomplete stop prefix; this contract does not promise prefix buffering.
The explanation opens here once every check passes.