Skip to main content

stream_rs/
lib.rs

1//! # stream-rs
2//!
3//! A zero-dependency, spec-compliant streaming toolkit for LLM responses.
4//!
5//! It gives you the low-level plumbing that sits *underneath* an LLM client —
6//! none of the model, transport, or API-key concerns, just the parsing that
7//! turns a raw byte stream into usable values:
8//!
9//! * [`sse`] — a WHATWG-compliant Server-Sent Events push parser.
10//! * [`incremental_json`] — a byte-level splitter that finds complete top-level
11//!   JSON values in a chunked stream (NDJSON or bare concatenation).
12//! * [`accumulators`] — fold provider-specific streaming deltas
13//!   ([`OpenAI`](accumulators::openai), [Anthropic](accumulators::anthropic),
14//!   and [Gemini](accumulators::gemini)) back into the final message content.
15//!
16//! The core has **no runtime dependencies**. An optional `stream` feature adds
17//! a [`futures_core::Stream`] adapter ([`stream::SseStream`]) for async byte
18//! sources; enable it with `features = ["stream"]`.
19//!
20//! ## `no_std`
21//!
22//! The crate is `#![no_std]` and needs only `alloc`. The default `std` feature
23//! merely adds [`std::error::Error`] impls for the error types; disable it for
24//! embedded / wasm targets:
25//!
26//! ```toml
27//! stream-rs = { version = "0.1", default-features = false }
28//! ```
29//!
30//! ## Quick start
31//!
32//! ```
33//! use stream_rs::sse::SseParser;
34//!
35//! let mut parser = SseParser::new();
36//! let mut events = Vec::new();
37//! parser.feed(b"data: {\"hello\":true}\n\n", &mut events);
38//!
39//! assert_eq!(events[0].data, "{\"hello\":true}");
40//! ```
41
42#![cfg_attr(docsrs, feature(doc_cfg))]
43#![no_std]
44
45extern crate alloc;
46
47#[cfg(feature = "std")]
48extern crate std;
49
50pub mod accumulators;
51pub mod error;
52pub mod incremental_json;
53pub mod sse;
54
55#[cfg(feature = "stream")]
56#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
57pub mod stream;
58
59pub use error::{AccumulateError, MalformedJson, TruncatedJson};
60pub use incremental_json::{FinishError, JsonSplitter};
61pub use sse::{SseEvent, SseParser};