langfuse-client-base 0.16.0

Auto-generated Langfuse API client from OpenAPI specification
Documentation
/*
 * server
 *
 * ## 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`
 *
 * The version of the OpenAPI document:
 *
 * Generated by: https://openapi-generator.tech
 */

use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};

/// struct for typed errors of method [`evaluation_rules_create`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EvaluationRulesCreateError {
    Status400(serde_json::Value),
    Status401(serde_json::Value),
    Status403(serde_json::Value),
    Status404(serde_json::Value),
    Status405(serde_json::Value),
    Status409(models::PublicApiError),
    Status429(models::PublicApiError),
    Status500(models::PublicApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`evaluation_rules_delete`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EvaluationRulesDeleteError {
    Status400(serde_json::Value),
    Status401(serde_json::Value),
    Status403(serde_json::Value),
    Status404(serde_json::Value),
    Status405(serde_json::Value),
    Status429(models::PublicApiError),
    Status500(models::PublicApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`evaluation_rules_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EvaluationRulesGetError {
    Status400(serde_json::Value),
    Status401(serde_json::Value),
    Status403(serde_json::Value),
    Status404(serde_json::Value),
    Status405(serde_json::Value),
    Status429(models::PublicApiError),
    Status500(models::PublicApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`evaluation_rules_list`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EvaluationRulesListError {
    Status400(serde_json::Value),
    Status401(serde_json::Value),
    Status403(serde_json::Value),
    Status404(serde_json::Value),
    Status405(serde_json::Value),
    Status429(models::PublicApiError),
    Status500(models::PublicApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`evaluation_rules_update`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EvaluationRulesUpdateError {
    Status400(serde_json::Value),
    Status401(serde_json::Value),
    Status403(serde_json::Value),
    Status404(serde_json::Value),
    Status405(serde_json::Value),
    Status409(models::PublicApiError),
    Status429(models::PublicApiError),
    Status500(models::PublicApiError),
    UnknownValue(serde_json::Value),
}

/// Create an evaluation rule using stable evaluator identifiers.  An evaluation rule defines **which** incoming observations should be evaluated and how prompt variables should be populated.  Key behavior: - rule names are not identifiers and do not need to be unique - rules always use the latest version of each associated evaluator - an enabled rule requires at least one evaluator assignment - omit `sampling` to evaluate every matching observation - omit `filter` to match every incoming observation - `datasetId` is the public filter name for selecting experiment datasets
#[bon::builder]
pub async fn evaluation_rules_create(
    configuration: &configuration::Configuration,
    create_evaluation_rule_request: models::CreateEvaluationRuleRequest,
) -> Result<models::EvaluationRule, Error<EvaluationRulesCreateError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_body_create_evaluation_rule_request = create_evaluation_rule_request;

    let uri_str = format!("{}/api/public/v2/evaluation-rules", configuration.base_path);
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };
    req_builder = req_builder.json(&p_body_create_evaluation_rule_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EvaluationRule`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::EvaluationRule`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EvaluationRulesCreateError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Delete an evaluation rule.  This removes the live-ingestion rule only. It does not delete associated evaluators or scores already produced by them.  Legacy trace and dataset rules can also be deleted. Their evaluators and previously produced scores are preserved.
#[bon::builder]
pub async fn evaluation_rules_delete(
    configuration: &configuration::Configuration,
    evaluation_rule_id: &str,
) -> Result<models::DeletedEvaluationRule, Error<EvaluationRulesDeleteError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_evaluation_rule_id = evaluation_rule_id;

    let uri_str = format!(
        "{}/api/public/v2/evaluation-rules/{evaluationRuleId}",
        configuration.base_path,
        evaluationRuleId = crate::apis::urlencode(p_path_evaluation_rule_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::DELETE, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeletedEvaluationRule`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DeletedEvaluationRule`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EvaluationRulesDeleteError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Get one evaluation rule, including a legacy trace or dataset rule, by its stable identifier.
#[bon::builder]
pub async fn evaluation_rules_get(
    configuration: &configuration::Configuration,
    evaluation_rule_id: &str,
) -> Result<models::EvaluationRule, Error<EvaluationRulesGetError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_evaluation_rule_id = evaluation_rule_id;

    let uri_str = format!(
        "{}/api/public/v2/evaluation-rules/{evaluationRuleId}",
        configuration.base_path,
        evaluationRuleId = crate::apis::urlencode(p_path_evaluation_rule_id)
    );
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EvaluationRule`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::EvaluationRule`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EvaluationRulesGetError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// List evaluation rules in newest-first creation order.  This includes legacy trace and dataset rules so they can be inspected and migrated. Treat the cursor as opaque and return it unchanged.
#[bon::builder]
pub async fn evaluation_rules_list(
    configuration: &configuration::Configuration,
    limit: Option<i32>,
    cursor: Option<&str>,
) -> Result<models::EvaluationRulesPage, Error<EvaluationRulesListError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_query_limit = limit;
    let p_query_cursor = cursor;

    let uri_str = format!("{}/api/public/v2/evaluation-rules", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_query_limit {
        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_query_cursor {
        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EvaluationRulesPage`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::EvaluationRulesPage`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EvaluationRulesListError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Update an evaluation rule by its stable identifier.  Provide only the fields to change. Providing `evaluatorAssignments` replaces the complete assignment list. Replacing the list with an empty array disables the rule. Setting `enabled=true` is rejected when the resulting assignment list is empty, including when both fields are sent in the same request.  Legacy trace and dataset rules follow the existing lifecycle restrictions: they can be deactivated with `enabled=false`, but their name, filters, sampling, and evaluator assignments cannot be changed.
#[bon::builder]
pub async fn evaluation_rules_update(
    configuration: &configuration::Configuration,
    evaluation_rule_id: &str,
    update_evaluation_rule_request: models::UpdateEvaluationRuleRequest,
) -> Result<models::EvaluationRule, Error<EvaluationRulesUpdateError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_evaluation_rule_id = evaluation_rule_id;
    let p_body_update_evaluation_rule_request = update_evaluation_rule_request;

    let uri_str = format!(
        "{}/api/public/v2/evaluation-rules/{evaluationRuleId}",
        configuration.base_path,
        evaluationRuleId = crate::apis::urlencode(p_path_evaluation_rule_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::PATCH, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref auth_conf) = configuration.basic_auth {
        req_builder = req_builder.basic_auth(auth_conf.0.to_owned(), auth_conf.1.to_owned());
    };
    req_builder = req_builder.json(&p_body_update_evaluation_rule_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::EvaluationRule`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::EvaluationRule`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EvaluationRulesUpdateError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}