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