Skip to main content

llm_output_parser/
lib.rs

1#![deny(missing_docs)]
2#![deny(clippy::all)]
3//! # LLM Output Parser
4//!
5//! Production-grade parser for extracting structured data from LLM responses.
6//! Handles think blocks, markdown fences, malformed JSON, and other real-world
7//! model output patterns without requiring an additional LLM call.
8//!
9//! ## Parsers Available
10//!
11//! | Parser | Use Case |
12//! |--------|----------|
13//! | [`parse_json`] | Extract typed JSON structs |
14//! | [`parse_json_value`] | Extract untyped JSON |
15//! | [`parse_string_list`] | Extract cleaned string lists (tags, items) |
16//! | [`parse_string_list_raw`] | Extract string lists without cleaning |
17//! | [`parse_xml_tag`] | Extract content from an XML tag |
18//! | [`parse_xml_tags`] | Extract content from multiple XML tags |
19//! | [`parse_choice`] | Extract a choice from valid options |
20//! | [`parse_number`] | Extract a numeric value |
21//! | [`parse_number_in_range`] | Extract a bounded numeric value |
22//! | [`parse_text`] | Clean text extraction |
23//! | `parse_yaml` | Extract typed YAML (feature: `yaml`) |
24//!
25//! ## Traced Variants
26//!
27//! | Parser | Use Case |
28//! |--------|----------|
29//! | [`parse_json_with_trace`] | JSON extraction with diagnostic trace |
30//! | [`parse_json_value_with_trace`] | Untyped JSON with diagnostic trace |
31//! | [`parse_string_list_with_trace`] | List extraction with diagnostic trace |
32//! | [`parse_xml_tag_with_trace`] | Single XML tag extraction with trace |
33//! | [`parse_xml_tags_with_trace`] | Multi-tag XML extraction with trace |
34//! | [`parse_choice_with_trace`] | Choice extraction with trace |
35//! | [`parse_number_with_trace`] | Numeric extraction with trace |
36//! | [`parse_number_in_range_with_trace`] | Bounded numeric extraction with trace |
37//! | [`parse_text_with_trace`] | Text cleanup with trace |
38//!
39//! ## Configuration
40//!
41//! | Type | Purpose |
42//! |------|---------|
43//! | [`ParseOptions`] | Safety limits and behavior toggles |
44//! | [`ParseTrace`] | Diagnostic output from traced parse calls |
45//!
46//! ## Shared Utilities
47//!
48//! | Function | Purpose |
49//! |----------|---------|
50//! | [`strip_think_tags`] | Remove `<think>` blocks from text |
51//! | [`try_repair_json`] | Fix common LLM JSON errors |
52
53pub mod choice;
54pub mod error;
55pub mod extract;
56pub mod json;
57pub mod list;
58pub mod number;
59pub mod repair;
60pub mod text;
61pub mod xml;
62
63#[cfg(feature = "yaml")]
64pub mod yaml;
65
66// Re-export all public functions at module level
67pub use choice::{parse_choice, parse_choice_with_trace};
68pub use error::{ParseError, ParseOptions, ParseTrace};
69pub use extract::{preprocess, strip_think_tags};
70pub use json::{parse_json, parse_json_value, parse_json_value_with_trace, parse_json_with_trace};
71pub use list::{parse_string_list, parse_string_list_raw, parse_string_list_with_trace};
72pub use number::{
73    parse_number, parse_number_in_range, parse_number_in_range_with_trace, parse_number_with_trace,
74};
75pub use repair::try_repair_json;
76pub use text::{parse_text, parse_text_with_trace};
77pub use xml::{parse_xml_tag, parse_xml_tag_with_trace, parse_xml_tags, parse_xml_tags_with_trace};
78
79#[cfg(feature = "yaml")]
80pub use yaml::parse_yaml;