Skip to main content

lc_core/
lib.rs

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