synaptic_parsers/
structured_parser.rs1use std::marker::PhantomData;
2
3use async_trait::async_trait;
4use serde::de::DeserializeOwned;
5use synaptic_core::{RunnableConfig, SynapticError};
6use synaptic_runnables::Runnable;
7
8use crate::FormatInstructions;
9
10pub struct StructuredOutputParser<T> {
12 _phantom: PhantomData<T>,
13}
14
15impl<T> StructuredOutputParser<T> {
16 pub fn new() -> Self {
17 Self {
18 _phantom: PhantomData,
19 }
20 }
21}
22
23impl<T> Default for StructuredOutputParser<T> {
24 fn default() -> Self {
25 Self::new()
26 }
27}
28
29impl<T> FormatInstructions for StructuredOutputParser<T> {
30 fn get_format_instructions(&self) -> String {
31 "Your response should be a valid JSON object matching the expected schema.".to_string()
32 }
33}
34
35#[async_trait]
36impl<T> Runnable<String, T> for StructuredOutputParser<T>
37where
38 T: DeserializeOwned + Send + Sync + 'static,
39{
40 async fn invoke(&self, input: String, _config: &RunnableConfig) -> Result<T, SynapticError> {
41 serde_json::from_str(&input)
42 .map_err(|e| SynapticError::Parsing(format!("structured parse error: {e}")))
43 }
44}