lc_core/tools/
structured_output.rs1use crate::language_models::LLMResult;
5use schemars::JsonSchema;
6use serde::de::DeserializeOwned;
7use std::marker::PhantomData;
8
9pub struct StructuredOutput<T> {
11 result: LLMResult,
12 schema: serde_json::Value,
13 _phantom: PhantomData<T>,
14}
15
16impl<T: DeserializeOwned + JsonSchema> StructuredOutput<T> {
17 pub fn new(result: LLMResult) -> Self {
19 use schemars::schema_for;
20 let schema = serde_json::to_value(schema_for!(T)).unwrap_or(serde_json::Value::Null);
21 Self {
22 result,
23 schema,
24 _phantom: PhantomData,
25 }
26 }
27
28 pub fn parse(&self) -> Result<T, serde_json::Error> {
33 let content = self.result.content.trim();
34
35 let json_str = if content.starts_with("```") {
37 let after_fence = if let Some(newline_pos) = content.find('\n') {
39 &content[newline_pos + 1..]
40 } else {
41 content
42 };
43 if let Some(end_pos) = after_fence.find("```") {
45 after_fence[..end_pos].trim()
46 } else {
47 after_fence.trim()
48 }
49 } else {
50 content
51 };
52
53 serde_json::from_str(json_str)
54 }
55
56 pub fn raw_content(&self) -> &str {
58 &self.result.content
59 }
60
61 pub fn schema(&self) -> &serde_json::Value {
63 &self.schema
64 }
65}