agent_chain_core/outputs/
run_info.rs

1//! RunInfo class.
2//!
3//! This module contains the `RunInfo` type which holds metadata for a single
4//! execution of a Chain or model.
5//! Mirrors `langchain_core.outputs.run_info`.
6
7use serde::{Deserialize, Serialize};
8use uuid::Uuid;
9
10#[cfg(feature = "specta")]
11use specta::Type;
12
13/// Class that contains metadata for a single execution of a Chain or model.
14///
15/// Defined for backwards compatibility with older versions of langchain_core.
16///
17/// This model will likely be deprecated in the future.
18///
19/// Users can acquire the run_id information from callbacks or via run_id
20/// information present in the astream_event API (depending on the use case).
21#[cfg_attr(feature = "specta", derive(Type))]
22#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
23pub struct RunInfo {
24    /// A unique identifier for the model or chain run.
25    pub run_id: Uuid,
26}
27
28impl RunInfo {
29    /// Create a new RunInfo with the given run_id.
30    pub fn new(run_id: Uuid) -> Self {
31        Self { run_id }
32    }
33
34    /// Create a new RunInfo with a randomly generated run_id.
35    pub fn new_random() -> Self {
36        Self {
37            run_id: Uuid::new_v4(),
38        }
39    }
40}
41
42impl Default for RunInfo {
43    fn default() -> Self {
44        Self::new_random()
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_run_info_new() {
54        let id = Uuid::new_v4();
55        let info = RunInfo::new(id);
56        assert_eq!(info.run_id, id);
57    }
58
59    #[test]
60    fn test_run_info_serialization() {
61        let info = RunInfo::new_random();
62        let json = serde_json::to_string(&info).unwrap();
63        let deserialized: RunInfo = serde_json::from_str(&json).unwrap();
64        assert_eq!(deserialized.run_id, info.run_id);
65    }
66}