lc_core/output_parsers/
base.rs1use async_trait::async_trait;
2
3#[derive(Debug, Clone, thiserror::Error)]
5pub enum OutputParserError {
6 #[error("Parse error: {0}")]
8 ParseError(String),
9 #[error("JSON error: {0}")]
11 JsonError(String),
12 #[error("Type error: {0}")]
14 TypeError(String),
15 #[error("{0}")]
17 Custom(String),
18}
19
20impl From<serde_json::Error> for OutputParserError {
21 fn from(e: serde_json::Error) -> Self {
22 OutputParserError::JsonError(e.to_string())
23 }
24}
25
26pub type OutputParserResult<T> = Result<T, OutputParserError>;
28
29#[async_trait]
35pub trait BaseOutputParser<Output: Send + Sync + 'static>: Send + Sync {
36 async fn parse(&self, text: &str) -> OutputParserResult<Output>;
38
39 async fn parse_with_retry(
41 &self,
42 text: &str,
43 _max_retries: usize,
44 ) -> OutputParserResult<Output> {
45 self.parse(text).await
46 }
47
48 fn get_format_instructions(&self) -> String {
50 String::new()
51 }
52}