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