Skip to main content

synaptic_parsers/
json_parser.rs

1use async_trait::async_trait;
2use serde_json::Value;
3use synaptic_core::{RunnableConfig, SynapticError};
4use synaptic_runnables::Runnable;
5
6use crate::FormatInstructions;
7
8/// Parses a string as JSON, returning a `serde_json::Value`.
9pub struct JsonOutputParser;
10
11impl FormatInstructions for JsonOutputParser {
12    fn get_format_instructions(&self) -> String {
13        "Your response should be a valid JSON object.".to_string()
14    }
15}
16
17#[async_trait]
18impl Runnable<String, Value> for JsonOutputParser {
19    async fn invoke(
20        &self,
21        input: String,
22        _config: &RunnableConfig,
23    ) -> Result<Value, SynapticError> {
24        serde_json::from_str(&input)
25            .map_err(|e| SynapticError::Parsing(format!("invalid JSON: {e}")))
26    }
27}