Skip to main content

lc_core/
lib.rs

1// lc-core/src/lib.rs
2//! Core abstractions for LangChainRust.
3//!
4//! This crate provides the foundational traits and types:
5//! - `Runnable`: Base execution interface
6//! - `BaseLanguageModel`: LLM abstraction
7//! - `BaseChatModel`: Chat model interface
8//! - `BaseTool`, `Tool`: Tool abstraction
9//! - `RunnableConfig`: Execution configuration with callbacks
10//! - Output parsers, structured output, caching, token counting, batch API
11
12pub mod batch;
13pub mod cache;
14pub mod json_parse;
15pub mod judge;
16pub mod language_models;
17pub mod math;
18pub mod output_parsers;
19pub mod router_llm;
20pub mod runnables;
21pub mod structured_output;
22pub mod token_counter;
23pub mod tools;
24
25// Re-export key types at crate root for convenience
26pub use json_parse::{parse_llm_json, parse_llm_json_with_retry, LlmJsonParseError};
27
28pub use judge::{structured_call, truncate, StructuredJudgeError};
29
30pub use language_models::{BaseChatModel, BaseLanguageModel, MultimodalError, MultimodalModel};
31pub use output_parsers::{
32    BaseOutputParser, CommaSeparatedListOutputParser, JsonOutputParser, OutputParserError,
33    OutputParserResult, StrOutputParser, StructuredOutputParser, TypedOutputParser,
34};
35pub use runnables::{
36    into_runnable_any, CancellationToken, LcelError, LcelStreamEvent, RetryConfig, RetryOn,
37    Runnable, RunnableAny, RunnableAnyWrapper, RunnableAssign, RunnableBinding, RunnableBranch,
38    RunnableConfig, RunnableExt, RunnableLambda, RunnableParallel, RunnablePassthrough,
39    RunnableRetry, RunnableSequence, RunnableWithFallbacks,
40};
41pub use structured_output::{
42    stream_structured_output, with_structured_output, PartialJsonError, PartialJsonParser,
43    StreamingStructuredOutputExt, StructuredOutputError, StructuredOutputExt,
44};
45pub use tools::{
46    BaseTool, FunctionCall, FunctionDefinition, StructuredOutput, Tool, ToolCall, ToolCallResult,
47    ToolDefinition, ToolError, ToolRegistry,
48};
49
50/// Unified error type for the lc-core crate.
51///
52/// Aggregates all core-specific error types so the `?` operator works
53/// seamlessly across core module boundaries.
54#[derive(Debug, thiserror::Error)]
55pub enum CoreError {
56    /// Tool execution error.
57    #[error("Tool error: {0}")]
58    Tool(#[from] ToolError),
59
60    /// JSON parse error from LLM output.
61    #[error("JSON parse error: {0}")]
62    JsonParse(#[from] LlmJsonParseError),
63
64    /// Batch processing error.
65    #[error("Batch error: {0}")]
66    Batch(#[from] batch::BatchError),
67
68    /// Router error.
69    #[error("Router error: {0}")]
70    Router(#[from] router_llm::RouterError),
71
72    /// Structured output extraction error.
73    #[error("Structured output error: {0}")]
74    StructuredOutput(#[from] StructuredOutputError),
75
76    /// Partial JSON parsing error.
77    #[error("Partial JSON error: {0}")]
78    PartialJson(#[from] PartialJsonError),
79
80    /// Output parser error.
81    #[error("Output parser error: {0}")]
82    OutputParser(#[from] OutputParserError),
83
84    /// Math operation error.
85    #[error("Math error: {0}")]
86    Math(#[from] math::MathError),
87
88    /// Any other error (e.g., from providers that haven't been extracted yet).
89    #[error("{0}")]
90    Other(String),
91}
92
93// Allow external error types to convert into CoreError via string wrapping
94impl From<std::convert::Infallible> for CoreError {
95    fn from(_: std::convert::Infallible) -> Self {
96        unreachable!()
97    }
98}
99
100/// Helper to convert any error into `CoreError::Other`.
101/// Use this instead of `?` when the error type is not a known CoreError variant.
102pub fn other_error<E: std::fmt::Display>(err: E) -> CoreError {
103    CoreError::Other(err.to_string())
104}
105
106// Allow CoreError to convert into LcelError for LCEL pipeline compatibility
107impl From<CoreError> for runnables::LcelError {
108    fn from(err: CoreError) -> Self {
109        match &err {
110            CoreError::Tool(_) => runnables::LcelError::Tool(err.to_string()),
111            CoreError::JsonParse(_) => runnables::LcelError::Other(err.to_string()),
112            CoreError::Batch(_) => runnables::LcelError::Other(err.to_string()),
113            CoreError::Router(_) => runnables::LcelError::Other(err.to_string()),
114            CoreError::StructuredOutput(_) => runnables::LcelError::Other(err.to_string()),
115            CoreError::PartialJson(_) => runnables::LcelError::Other(err.to_string()),
116            CoreError::OutputParser(_) => runnables::LcelError::OutputParser(err.to_string()),
117            CoreError::Math(_) => runnables::LcelError::Other(err.to_string()),
118            CoreError::Other(_) => runnables::LcelError::Other(err.to_string()),
119        }
120    }
121}