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