lm15 1.0.0-rc.7

One request and response model for every LLM provider (OpenAI, Anthropic, Gemini, xAI, Groq, Bedrock, Azure, Vertex, local servers), with visible adaptations. Implements the lm15 contract.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! [`ResponseStream`]: the assembled stream (playbooks/api-family.md ยง The
//! core loop; the reference's `lm15.result.ResponseStream`).
//!
//! ```ignore
//! let mut rs = ResponseStream::new(lm.stream(&request), &request);
//! while let Some(text) = rs.text_chunks().next().await {
//!     print!("{}", text?);
//! }
//! let response = rs.response().await?;   // what `complete` would return
//! ```
//!
//! Events are teed through the MAP-9 accumulator as they pass. An
//! `error` event becomes the typed error and ends the stream; `response()`
//! drains whatever is left and materializes. A failure is remembered:
//! `response()` after one returns the same error again, never a half
//! response.
//!
//! The stream is held to MAP-3 (contract
//! `changes/2026-09-11-stream-completion-and-error-metadata.md`): a source
//! that runs dry without an `end` event, and one that yields anything
//! after its `end` event, are `StreamAssemblyError` (with `partial`). Once
//! the `end` event has been yielded the Response is complete and is never
//! withheld: a source `Err` after it is reported with `log::warn!` and kept
//! in [`ResponseStream::cleanup_errors`], never returned in its place.

use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::Stream;
use futures_util::StreamExt;

use crate::errors::Lm15Error;
use crate::stream::{error_from_detail, StreamAccumulator};
use crate::types::{Delta, Request, Response, StreamEvent};

/// The assembly engine both [`ResponseStream`] and the blocking mirror
/// step: every source item goes through `step`, which tees it into the
/// MAP-9 accumulator and decides what the consumer sees.
#[derive(Debug)]
pub struct Assembler {
    accumulator: StreamAccumulator,
    response: Option<Response>,
    failure: Option<Lm15Error>,
    done: bool,
    cleanup_errors: Vec<Lm15Error>,
}

impl Assembler {
    pub fn new(request: &Request) -> Self {
        Assembler {
            accumulator: StreamAccumulator::new(request),
            response: None,
            failure: None,
            done: false,
            cleanup_errors: Vec::new(),
        }
    }

    pub fn is_done(&self) -> bool {
        self.done
    }

    /// One source item in (`None`: the source ran dry), what the consumer
    /// sees out (`None`: the assembled stream has ended).
    pub fn step(
        &mut self,
        item: Option<Result<StreamEvent, Lm15Error>>,
    ) -> Option<Result<StreamEvent, Lm15Error>> {
        if self.done {
            return None;
        }
        match item {
            None => {
                self.done = true;
                if self.response.is_none() {
                    return Some(Err(self.fail(incomplete(&self.accumulator))));
                }
                None
            }
            Some(Err(err)) => {
                if self.response.is_some() {
                    // After completion: the connection's afterlife, not the
                    // answer. Reported, recorded, never returned in its place.
                    warn_cleanup(&err);
                    self.cleanup_errors.push(err);
                    self.done = true;
                    return None;
                }
                Some(Err(self.fail(err)))
            }
            Some(Ok(event)) => {
                if let Some(response) = &self.response {
                    return Some(Err(self.fail(trailing(response.clone()))));
                }
                if let StreamEvent::Error(event) = &event {
                    return Some(Err(self.fail(error_from_detail(&event.error))));
                }
                self.accumulator.push(&event);
                if matches!(event, StreamEvent::End(_)) {
                    match self.accumulator.response() {
                        Ok(response) => self.response = Some(response),
                        Err(err) => return Some(Err(self.fail(err))),
                    }
                }
                Some(Ok(event))
            }
        }
    }

    /// The outcome once the stream has ended; a failure is remembered.
    pub fn outcome(&self) -> Result<Response, Lm15Error> {
        match (&self.failure, &self.response) {
            (Some(err), _) => Err(err.clone()),
            (None, Some(response)) => Ok(response.clone()),
            (None, None) => unreachable!("a finished stream has a response or a failure"),
        }
    }

    pub fn failure(&self) -> Option<&Lm15Error> {
        self.failure.as_ref()
    }

    /// Failures that followed the `end` event (the source's afterlife). The
    /// Response is complete regardless; each was also `log::warn!`ed.
    pub fn cleanup_errors(&self) -> &[Lm15Error] {
        &self.cleanup_errors
    }

    fn fail(&mut self, err: Lm15Error) -> Lm15Error {
        self.failure = Some(err.clone());
        self.done = true;
        err
    }
}

/// Exhausted without an `end` event: the finish reason and usage never
/// arrived; the text is not a finished turn (MAP-3).
pub(crate) fn incomplete(accumulator: &StreamAccumulator) -> Lm15Error {
    let partial = match accumulator.response() {
        Ok(response) => Some(response),
        Err(Lm15Error::StreamAssemblyError(err)) => err.partial.map(|p| *p),
        Err(_) => None,
    };
    Lm15Error::stream_assembly(
        "Stream ended without an end event: its finish reason and usage never arrived, \
         so the text is not a finished turn (MAP-3)",
        partial,
        None,
    )
}

/// An event after the `end` event: a source defect (MAP-3), never merged
/// or dropped.
pub(crate) fn trailing(response: Response) -> Lm15Error {
    Lm15Error::stream_assembly(
        "Stream emitted an event after its end event (MAP-3: the end event is final); \
         the source that produced this stream is defective",
        Some(response),
        None,
    )
}

pub(crate) fn warn_cleanup(err: &Lm15Error) {
    log::warn!(
        target: "lm15::stream",
        "stream source failed after the response was complete ({}: {}); the Response is returned unchanged",
        err.class_name(),
        err.message()
    );
}

/// The assembled stream: canonical events out, a `Response` at the end.
/// `S` is usually [`crate::adapter::EventStream`]; any
/// `Stream<Item = Result<StreamEvent, Lm15Error>> + Unpin` works
/// (`Box::pin` one that is not).
pub struct ResponseStream<S> {
    source: S,
    assembler: Assembler,
}

impl<S> ResponseStream<S>
where
    S: Stream<Item = Result<StreamEvent, Lm15Error>> + Unpin,
{
    pub fn new(events: S, request: &Request) -> Self {
        ResponseStream {
            source: events,
            assembler: Assembler::new(request),
        }
    }

    /// The text deltas only, as they arrive.
    pub fn text_chunks(&mut self) -> TextChunks<'_, S> {
        TextChunks { inner: self }
    }

    /// The complete `Response`: drains the stream if it is still open.
    pub async fn response(&mut self) -> Result<Response, Lm15Error> {
        if let Some(err) = self.assembler.failure() {
            return Err(err.clone());
        }
        while !self.assembler.is_done() {
            if let Some(Err(err)) = self.next().await {
                return Err(err);
            }
        }
        self.assembler.outcome()
    }

    /// Whether the stream has ended (the source ran dry after its `end`
    /// event, or a failure).
    pub fn is_done(&self) -> bool {
        self.assembler.is_done()
    }

    /// Failures that followed the `end` event (a read error while the
    /// source drained). The Response is complete regardless; each was also
    /// `log::warn!`ed under target `lm15::stream`.
    pub fn cleanup_errors(&self) -> &[Lm15Error] {
        self.assembler.cleanup_errors()
    }
}

impl<S> Stream for ResponseStream<S>
where
    S: Stream<Item = Result<StreamEvent, Lm15Error>> + Unpin,
{
    type Item = Result<StreamEvent, Lm15Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        if this.assembler.is_done() {
            return Poll::Ready(None);
        }
        match Pin::new(&mut this.source).poll_next(cx) {
            Poll::Pending => Poll::Pending,
            Poll::Ready(item) => Poll::Ready(this.assembler.step(item)),
        }
    }
}

/// The text deltas of a [`ResponseStream`].
pub struct TextChunks<'a, S> {
    inner: &'a mut ResponseStream<S>,
}

impl<S> Stream for TextChunks<'_, S>
where
    S: Stream<Item = Result<StreamEvent, Lm15Error>> + Unpin,
{
    type Item = Result<String, Lm15Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        loop {
            match Pin::new(&mut *this.inner).poll_next(cx) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(None) => return Poll::Ready(None),
                Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(err))),
                Poll::Ready(Some(Ok(StreamEvent::Delta(delta)))) => {
                    if let Delta::Text(text) = delta.delta {
                        return Poll::Ready(Some(Ok(text.text)));
                    }
                }
                Poll::Ready(Some(Ok(_))) => {}
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::errors::ErrorCode;
    use crate::types::{
        ErrorDetail, FinishReason, Message, StreamDeltaEvent, StreamEndEvent, StreamErrorEvent,
        StreamStartEvent, TextDelta,
    };

    fn request() -> Request {
        Request::new("m", vec![Message::user("hi").unwrap()]).unwrap()
    }

    fn text(index: u64, text: &str) -> StreamEvent {
        StreamEvent::Delta(StreamDeltaEvent {
            delta: Delta::Text(TextDelta {
                part_index: index,
                text: text.to_string(),
                ..Default::default()
            }),
        })
    }

    fn events(
        items: Vec<Result<StreamEvent, Lm15Error>>,
    ) -> impl Stream<Item = Result<StreamEvent, Lm15Error>> + Unpin {
        futures_util::stream::iter(items)
    }

    #[tokio::test]
    async fn text_then_response_stops_at_end() {
        let request = request();
        let source = events(vec![
            Ok(StreamEvent::Start(StreamStartEvent {
                adaptations: Vec::new(),
                id: Some("r1".into()),
                model: Some("m-2".into()),
            })),
            Ok(text(0, "Hel")),
            Ok(text(0, "lo")),
            Ok(StreamEvent::End(StreamEndEvent {
                finish_reason: Some(FinishReason::Stop),
                ..Default::default()
            })),
            // A source failure AFTER `end` is the connection's afterlife: it
            // never withholds the complete Response (contract 2026-09-11 ยง 2).
            Err(Lm15Error::TransportError(crate::errors::ErrorMeta::new(
                "late",
            ))),
        ]);
        let mut rs = ResponseStream::new(source, &request);
        let mut got = String::new();
        while let Some(chunk) = rs.text_chunks().next().await {
            got.push_str(&chunk.unwrap());
        }
        assert_eq!(got, "Hello");
        assert!(rs.is_done());
        let response = rs.response().await.unwrap();
        assert_eq!(response.text().as_deref(), Some("Hello"));
        assert_eq!(response.model, "m-2");
        assert_eq!(response.finish_reason, FinishReason::Stop);
        assert_eq!(rs.cleanup_errors().len(), 1);
        assert_eq!(rs.cleanup_errors()[0].class_name(), "TransportError");
        // Idempotent.
        assert_eq!(rs.response().await.unwrap(), response);
        assert_eq!(rs.cleanup_errors().len(), 1);
    }

    #[tokio::test]
    async fn an_event_after_end_is_refused_never_merged_or_dropped() {
        let request = request();
        let source = events(vec![
            Ok(text(0, "ok")),
            Ok(StreamEvent::End(StreamEndEvent {
                finish_reason: Some(FinishReason::Stop),
                ..Default::default()
            })),
            Ok(text(0, "late")),
        ]);
        let mut rs = ResponseStream::new(source, &request);
        let err = rs.response().await.unwrap_err();
        assert_eq!(err.class_name(), "StreamAssemblyError");
        assert!(err.message().contains("after its end event"), "{err}");
        let Lm15Error::StreamAssemblyError(inner) = &err else {
            unreachable!()
        };
        assert_eq!(
            inner.partial.as_ref().unwrap().text().as_deref(),
            Some("ok")
        );
        assert_eq!(
            rs.response().await.unwrap_err().class_name(),
            "StreamAssemblyError"
        );
    }

    #[tokio::test]
    async fn error_event_is_the_typed_error_and_sticks() {
        let request = request();
        let source = events(vec![
            Ok(text(0, "partial")),
            Ok(StreamEvent::Error(StreamErrorEvent {
                error: ErrorDetail::new(ErrorCode::RateLimit, "slow down"),
            })),
            Ok(text(0, " more")),
        ]);
        let mut rs = ResponseStream::new(source, &request);
        let first = rs.next().await.unwrap().unwrap();
        assert!(matches!(first, StreamEvent::Delta(_)));
        let err = rs.next().await.unwrap().unwrap_err();
        assert_eq!(err.class_name(), "RateLimitError");
        assert!(rs.next().await.is_none());
        let again = rs.response().await.unwrap_err();
        assert_eq!(again.class_name(), "RateLimitError");
    }

    #[tokio::test]
    async fn transport_failure_mid_stream_is_the_stream_error() {
        let request = request();
        let source = events(vec![
            Ok(text(0, "a")),
            Err(Lm15Error::TransportError(crate::errors::ErrorMeta::new(
                "reset",
            ))),
        ]);
        let mut rs = ResponseStream::new(source, &request);
        let err = rs.response().await.unwrap_err();
        assert_eq!(err.class_name(), "TransportError");
    }

    #[tokio::test]
    async fn source_ending_without_end_event_is_not_a_response() {
        // Before 2026-09-11 this materialized as success with no finish
        // reason and no usage: a dropped connection looked like a finished
        // turn. The text is salvageable from `partial`.
        let request = request();
        let mut rs = ResponseStream::new(events(vec![Ok(text(0, "x"))]), &request);
        let err = rs.response().await.unwrap_err();
        assert_eq!(err.class_name(), "StreamAssemblyError");
        assert!(err.message().contains("without an end event"), "{err}");
        let Lm15Error::StreamAssemblyError(inner) = &err else {
            unreachable!()
        };
        assert_eq!(inner.partial.as_ref().unwrap().text().as_deref(), Some("x"));
        assert!(rs.cleanup_errors().is_empty());

        // The one-shot materializer holds the same line.
        let err = crate::stream::materialize_response([&text(0, "x")], &request).unwrap_err();
        assert_eq!(err.class_name(), "StreamAssemblyError");
        let end = StreamEvent::End(StreamEndEvent {
            finish_reason: Some(FinishReason::Stop),
            ..Default::default()
        });
        let err =
            crate::stream::materialize_response([&text(0, "x"), &end, &text(0, "late")], &request)
                .unwrap_err();
        assert!(err.message().contains("after its end event"), "{err}");
        let ok = crate::stream::materialize_response([&text(0, "x"), &end], &request).unwrap();
        assert_eq!(ok.text().as_deref(), Some("x"));
    }
}