Skip to main content

langfuse_client_base/models/
observation.rs

1/*
2 * server
3 *
4 * ## Real-time data  The only real-time path to get tracing data is to ingest via OpenTelemetry (`POST /api/public/otel/v1/traces`) and retrieve via Observations API v2 (`GET /api/public/v2/observations`) and Metrics API v2 (`GET /api/public/v2/metrics`). Other public API write and read endpoints can delay data by about 10 minutes. Direct OpenTelemetry exporters must send the `x-langfuse-ingestion-version: 4` header; current Python and JS SDKs already do. See the [Public API docs](https://langfuse.com/docs/api-and-data-platform/features/public-api) and [real-time ingestion](https://langfuse.com/integrations/native/opentelemetry.md#real-time-ingestion).  ## Authentication  Authenticate with the API using [Basic Auth](https://en.wikipedia.org/wiki/Basic_access_authentication), get API keys in the project settings:  - username: Langfuse Public Key - password: Langfuse Secret Key  ## Exports  - OpenAPI spec: https://cloud.langfuse.com/generated/api/openapi.yml - Self-hosted deployments: interactive API reference at `/api/docs` and OpenAPI spec at `/api/openapi.yaml`
5 *
6 * The version of the OpenAPI document:
7 *
8 * Generated by: https://openapi-generator.tech
9 */
10
11use crate::models;
12use serde::{Deserialize, Serialize};
13
14#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize, bon::Builder)]
15pub struct Observation {
16    /// The unique identifier of the observation
17    #[serde(rename = "id")]
18    pub id: String,
19    /// The trace ID associated with the observation
20    #[serde(rename = "traceId", deserialize_with = "Option::deserialize")]
21    pub trace_id: Option<String>,
22    /// The type of the observation
23    #[serde(rename = "type")]
24    pub r#type: String,
25    /// The name of the observation
26    #[serde(rename = "name", deserialize_with = "Option::deserialize")]
27    pub name: Option<String>,
28    /// The start time of the observation
29    #[serde(rename = "startTime")]
30    pub start_time: chrono::DateTime<chrono::FixedOffset>,
31    /// The end time of the observation.
32    #[serde(rename = "endTime", deserialize_with = "Option::deserialize")]
33    pub end_time: Option<chrono::DateTime<chrono::FixedOffset>>,
34    /// The completion start time of the observation
35    #[serde(
36        rename = "completionStartTime",
37        deserialize_with = "Option::deserialize"
38    )]
39    pub completion_start_time: Option<chrono::DateTime<chrono::FixedOffset>>,
40    /// The model used for the observation
41    #[serde(rename = "model", deserialize_with = "Option::deserialize")]
42    pub model: Option<String>,
43    /// The parameters of the model used for the observation
44    #[serde(rename = "modelParameters", deserialize_with = "Option::deserialize")]
45    pub model_parameters: Option<serde_json::Value>,
46    /// The input data of the observation
47    #[serde(rename = "input", deserialize_with = "Option::deserialize")]
48    pub input: Option<serde_json::Value>,
49    /// The version of the observation
50    #[serde(rename = "version", deserialize_with = "Option::deserialize")]
51    pub version: Option<String>,
52    /// Additional metadata of the observation
53    #[serde(
54        rename = "metadata",
55        default,
56        with = "::serde_with::rust::double_option",
57        skip_serializing_if = "Option::is_none"
58    )]
59    pub metadata: Option<Option<std::collections::HashMap<String, serde_json::Value>>>,
60    /// The output data of the observation
61    #[serde(rename = "output", deserialize_with = "Option::deserialize")]
62    pub output: Option<serde_json::Value>,
63    #[serde(rename = "usage")]
64    pub usage: Box<models::Usage>,
65    #[serde(rename = "level")]
66    pub level: models::ObservationLevel,
67    /// The status message of the observation
68    #[serde(rename = "statusMessage", deserialize_with = "Option::deserialize")]
69    pub status_message: Option<String>,
70    /// The parent observation ID
71    #[serde(
72        rename = "parentObservationId",
73        deserialize_with = "Option::deserialize"
74    )]
75    pub parent_observation_id: Option<String>,
76    /// The prompt ID associated with the observation
77    #[serde(rename = "promptId", deserialize_with = "Option::deserialize")]
78    pub prompt_id: Option<String>,
79    /// The usage details of the observation. Key is the name of the usage metric, value is the number of units consumed. The total key is the sum of all (non-total) usage metrics or the total value ingested.
80    #[serde(rename = "usageDetails")]
81    pub usage_details: std::collections::HashMap<String, i32>,
82    /// The cost details of the observation. Key is the name of the cost metric, value is the cost in USD. The total key is the sum of all (non-total) cost metrics or the total value ingested.
83    #[serde(rename = "costDetails")]
84    pub cost_details: std::collections::HashMap<String, f64>,
85    /// The environment from which this observation originated. Can be any lowercase alphanumeric string with hyphens and underscores that does not start with 'langfuse'.
86    #[serde(rename = "environment")]
87    pub environment: String,
88}
89
90impl Observation {
91    pub fn new(
92        id: String,
93        trace_id: Option<String>,
94        r#type: String,
95        name: Option<String>,
96        start_time: chrono::DateTime<chrono::FixedOffset>,
97        end_time: Option<chrono::DateTime<chrono::FixedOffset>>,
98        completion_start_time: Option<chrono::DateTime<chrono::FixedOffset>>,
99        model: Option<String>,
100        model_parameters: Option<serde_json::Value>,
101        input: Option<serde_json::Value>,
102        version: Option<String>,
103        output: Option<serde_json::Value>,
104        usage: models::Usage,
105        level: models::ObservationLevel,
106        status_message: Option<String>,
107        parent_observation_id: Option<String>,
108        prompt_id: Option<String>,
109        usage_details: std::collections::HashMap<String, i32>,
110        cost_details: std::collections::HashMap<String, f64>,
111        environment: String,
112    ) -> Observation {
113        Observation {
114            id,
115            trace_id,
116            r#type,
117            name,
118            start_time,
119            end_time,
120            completion_start_time,
121            model,
122            model_parameters,
123            input,
124            version,
125            metadata: None,
126            output,
127            usage: Box::new(usage),
128            level,
129            status_message,
130            parent_observation_id,
131            prompt_id,
132            usage_details,
133            cost_details,
134            environment,
135        }
136    }
137}