Skip to main content

lc_core/structured_output/
mod.rs

1// src/core/structured_output/mod.rs
2//! Structured output utilities for extracting typed data from LLM responses.
3//!
4//! This module provides a provider-agnostic `with_structured_output` function
5//! that works with any `BaseChatModel` implementation. It injects a JSON schema
6//! into the prompt, calls the LLM, and parses the JSON response into the
7//! target type `T`.
8//!
9//! It also provides streaming support via `stream_structured_output`, which
10//! returns a stream of partial `T` values as the model generates tokens,
11//! using `PartialJsonParser` to incrementally parse incomplete JSON.
12//!
13//! # Strategy
14//!
15//! The default (generic) strategy uses **prompt injection**: the JSON schema
16//! and format instructions are embedded in the system prompt, and the
17//! `JsonOutputParser` is used to extract JSON from the response.
18//!
19//! Provider-specific implementations (OpenAI function calling, Ollama JSON mode)
20//! are available on the concrete types directly (e.g., `OpenAIChat::with_structured_output`).
21//!
22//! # Example
23//!
24//! ```ignore
25//! use serde::{Deserialize, Serialize};
26//! use langchainrust::core::structured_output::{with_structured_output, StructuredOutputError};
27//! use langchainrust::{OpenAIChat, OpenAIConfig, Message};
28//!
29//! #[derive(Debug, Deserialize, Serialize)]
30//! struct Person {
31//!     name: String,
32//!     age: u32,
33//! }
34//!
35//! let llm = OpenAIChat::new(OpenAIConfig::default());
36//! let schema = serde_json::json!({
37//!     "type": "object",
38//!     "properties": {
39//!         "name": {"type": "string"},
40//!         "age": {"type": "integer"}
41//!     },
42//!     "required": ["name", "age"]
43//! });
44//!
45//! let person: Person = with_structured_output(&llm, schema, "Tell me about Alice who is 30").await?;
46//! ```
47
48pub mod extract;
49pub mod parser;
50pub mod streaming;
51
52pub use extract::{with_structured_output, StructuredOutputError, StructuredOutputExt};
53pub use parser::{PartialJsonError, PartialJsonParser};
54pub use streaming::{stream_structured_output, StreamingStructuredOutputExt};
55
56#[cfg(test)]
57mod tests;