lc-core 0.17.0

Core abstractions for langchainrust — Runnable, BaseTool, BaseChatModel, etc.
Documentation
#![warn(missing_docs)]
// lc-core/src/lib.rs
//! Core abstractions for LangChainRust.
//!
//! This crate provides the foundational traits and types:
//! - `Runnable`: Base execution interface
//! - `BaseLanguageModel`: LLM abstraction
//! - `BaseChatModel`: Chat model interface
//! - `BaseTool`, `Tool`: Tool abstraction
//! - `RunnableConfig`: Execution configuration with callbacks
//! - Output parsers, structured output, caching, token counting, batch API

pub mod batch;
pub mod cache;
pub mod json_parse;
pub mod judge;
pub mod language_models;
pub mod math;
pub mod output_parsers;
pub mod router_llm;
pub mod runnables;
pub mod structured_output;
pub mod token_counter;
pub mod tools;

// Re-export key types at crate root for convenience
pub use json_parse::{parse_llm_json, parse_llm_json_with_retry, LlmJsonParseError};

pub use judge::{structured_call, truncate, StructuredJudgeError};

pub use language_models::{
    predict_tools, BaseChatModel, BaseLanguageModel, MultimodalError, MultimodalModel,
    PredictToolsError,
};
pub use output_parsers::{
    BaseOutputParser, CommaSeparatedListOutputParser, JsonOutputParser, OutputParserError,
    OutputParserResult, StrOutputParser, StructuredOutputParser, TypedOutputParser,
};
pub use runnables::{
    into_runnable_any, CancellationToken, LcelError, RetryConfig, RetryOn, Runnable, RunnableAny,
    RunnableAnyWrapper, RunnableAssign, RunnableBinding, RunnableBranch, RunnableConfig,
    RunnableConfigurable, RunnableConfigurableFields, RunnableExt, RunnableLambda,
    RunnableParallel, RunnablePassthrough, RunnablePick, RunnableRetry, RunnableSequence,
    RunnableWithFallbacks,
};
pub use structured_output::{
    stream_structured_output, with_structured_output, PartialJsonError, PartialJsonParser,
    StreamingStructuredOutputExt, StructuredOutputError, StructuredOutputExt,
};
pub use tools::{
    BaseTool, FunctionCall, FunctionDefinition, StructuredOutput, Tool, ToolCall, ToolCallBuilder,
    ToolCallResult, ToolDefinition, ToolError, ToolRegistry,
};

/// Unified error type for the lc-core crate.
///
/// Aggregates all core-specific error types so the `?` operator works
/// seamlessly across core module boundaries.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CoreError {
    /// Tool execution error.
    #[error("Tool error: {0}")]
    Tool(#[from] ToolError),

    /// JSON parse error from LLM output.
    #[error("JSON parse error: {0}")]
    JsonParse(#[from] LlmJsonParseError),

    /// Batch processing error.
    #[error("Batch error: {0}")]
    Batch(#[from] batch::BatchError),

    /// Router error.
    #[error("Router error: {0}")]
    Router(#[from] router_llm::RouterError),

    /// Structured output extraction error.
    #[error("Structured output error: {0}")]
    StructuredOutput(#[from] StructuredOutputError),

    /// Partial JSON parsing error.
    #[error("Partial JSON error: {0}")]
    PartialJson(#[from] PartialJsonError),

    /// Output parser error.
    #[error("Output parser error: {0}")]
    OutputParser(#[from] OutputParserError),

    /// Math operation error.
    #[error("Math error: {0}")]
    Math(#[from] math::MathError),

    /// Token counting error.
    #[error("Token counting error: {0}")]
    TokenCount(#[from] token_counter::TokenCounterError),

    /// Any other error (e.g., from providers that haven't been extracted yet).
    #[error("{0}")]
    Other(String),
}

// Allow external error types to convert into CoreError via string wrapping
impl From<std::convert::Infallible> for CoreError {
    fn from(_: std::convert::Infallible) -> Self {
        unreachable!()
    }
}

/// Helper to convert any error into `CoreError::Other`.
/// Use this instead of `?` when the error type is not a known CoreError variant.
pub fn other_error<E: std::fmt::Display>(err: E) -> CoreError {
    CoreError::Other(err.to_string())
}

// Allow CoreError to convert into LcelError for LCEL pipeline compatibility
impl From<CoreError> for runnables::LcelError {
    fn from(err: CoreError) -> Self {
        match &err {
            CoreError::Tool(_) => runnables::LcelError::Tool(err.to_string()),
            CoreError::JsonParse(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::Batch(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::Router(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::StructuredOutput(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::PartialJson(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::OutputParser(_) => runnables::LcelError::OutputParser(err.to_string()),
            CoreError::Math(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::TokenCount(_) => runnables::LcelError::Other(err.to_string()),
            CoreError::Other(_) => runnables::LcelError::Other(err.to_string()),
        }
    }
}