Skip to main content

Crate stopstream

Crate stopstream 

Source
Expand description

stopstream — streaming-safe stop-sequence detector for LLM token streams.

The naive solution is if buffer.contains(stop) { stop_streaming() }. That works only after the entire stop sequence lands inside one chunk. Real providers stream tokens of arbitrary boundaries, so a stop like "</answer>" arrives split across multiple chunks. This crate buffers exactly enough tail to detect the stop without ever emitting a partial match downstream.

§Example

use stopstream::StopDetector;

let mut det = StopDetector::new(["</answer>"]);
let r1 = det.push("Here is the response </ans");
assert!(r1.stopped.is_none());
assert_eq!(r1.safe_text, "Here is the response ");

let r2 = det.push("wer> trailing");
assert_eq!(r2.stopped.as_deref(), Some("</answer>"));
// Anything after the stop is dropped.

Structs§

StopDetector
Watches a token stream for any of a set of stop sequences.
StopResult
What push returned for one chunk.