stopstream 0.1.0

Streaming-safe stop-sequence detector for LLM token streams. Handles partial matches at chunk boundaries.
Documentation
  • Coverage
  • 100%
    9 out of 9 items documented1 out of 7 items with examples
  • Size
  • Source code size: 23.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 361.29 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 32s Average build duration of successful builds.
  • all releases: 32s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • MukundaKatta/rust-llm-stack
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MukundaKatta

stopstream

Streaming-safe stop-sequence detector for LLM token streams. Buffers exactly enough tail to detect partial matches across chunk boundaries without ever emitting a partial match downstream.

The naive if buffer.contains(stop) { ... } works only when the stop sequence lands fully inside one chunk. Real providers stream arbitrary byte boundaries, so </answer> typically arrives as </ans then wer> and the naive code emits </ans to the user before noticing.

Install

[dependencies]
stopstream = "0.1"

Use

use stopstream::StopDetector;

let mut det = StopDetector::new(["</answer>", "<|endoftext|>"]);

for chunk in stream {
    let r = det.push(&chunk);
    if !r.safe_text.is_empty() {
        emit_to_user(&r.safe_text);
    }
    if r.stopped.is_some() {
        break;
    }
}
let tail = det.flush();
if !tail.is_empty() {
    emit_to_user(&tail);
}

Guarantees

  • Never emits a partial stop match. Holds back at most (longest_stop_len - 1) bytes (snapped to a UTF-8 char boundary).
  • After a stop fires, every subsequent push returns an empty result.
  • Multi-byte UTF-8 chars are never split.
  • Multiple stop sequences supported; the leftmost one in the buffer wins.

License: MIT OR Apache-2.0.