Skip to main content

langgraph_core_rs/
stream.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value as JsonValue;
3use crate::types::StreamMode;
4
5/// A single chunk emitted by the graph's streaming interface (v2 format).
6///
7/// Discriminated on the `mode` field. The `ns` field carries the namespace
8/// path (empty for top-level graphs). The `data` field carries the payload.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct StreamPart {
11    /// The stream mode that produced this chunk.
12    pub mode: StreamMode,
13    /// Namespace path (empty for top-level graph).
14    pub ns: Vec<String>,
15    /// The payload data.
16    pub data: JsonValue,
17}
18
19impl StreamPart {
20    pub fn values(ns: Vec<String>, data: JsonValue) -> Self {
21        Self { mode: StreamMode::Values, ns, data }
22    }
23
24    pub fn updates(ns: Vec<String>, data: JsonValue) -> Self {
25        Self { mode: StreamMode::Updates, ns, data }
26    }
27
28    pub fn messages(ns: Vec<String>, data: JsonValue) -> Self {
29        Self { mode: StreamMode::Messages, ns, data }
30    }
31
32    pub fn custom(ns: Vec<String>, data: JsonValue) -> Self {
33        Self { mode: StreamMode::Custom, ns, data }
34    }
35
36    pub fn tasks(ns: Vec<String>, data: JsonValue) -> Self {
37        Self { mode: StreamMode::Tasks, ns, data }
38    }
39
40    pub fn checkpoints(ns: Vec<String>, data: JsonValue) -> Self {
41        Self { mode: StreamMode::Checkpoints, ns, data }
42    }
43
44    pub fn debug(ns: Vec<String>, data: JsonValue) -> Self {
45        Self { mode: StreamMode::Debug, ns, data }
46    }
47}