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