Skip to main content

cognis_core/output_parsers/
mod.rs

1//! Typed parsers that convert LLM string output into structured values.
2//!
3//! Each parser implements:
4//! - [`OutputParser<T>`] — for direct parsing.
5//! - `Runnable<String, T>` — for chain composition (`prompt | model | parser`).
6//!
7//! Parsers also expose a `format_instructions()` snippet that can be embedded
8//! in a prompt to tell the LLM how to format its output.
9
10pub mod boolean;
11pub mod fixing;
12pub mod json;
13pub mod list;
14pub mod string;
15pub mod structured;
16pub mod xml;
17
18pub use boolean::BooleanParser;
19pub use fixing::{OutputFixingParser, RetryParser};
20pub use json::JsonParser;
21pub use list::{CommaListParser, NumberedListParser};
22pub use string::StringParser;
23pub use structured::{
24    JsonExtraction, JsonExtractor, StructuredOutputConfig, StructuredOutputParser,
25};
26pub use xml::XmlParser;
27
28use crate::Result;
29
30/// Parses LLM string output into a typed value `T`.
31pub trait OutputParser<T>: Send + Sync
32where
33    T: Send + 'static,
34{
35    /// Attempt to parse the LLM output.
36    fn parse(&self, text: &str) -> Result<T>;
37
38    /// Optional human-readable instructions to embed in a prompt to tell
39    /// the LLM how to format its output.
40    fn format_instructions(&self) -> Option<String> {
41        None
42    }
43}