Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Output Parsers

Output parsers transform raw LLM output into structured data. Every parser in Synaptic implements the Runnable trait, so they compose naturally with prompt templates, chat models, and other runnables using the LCEL pipe operator (|).

Available Parsers

ParserInputOutputDescription
StrOutputParserMessageStringExtracts the text content from a message
JsonOutputParserStringserde_json::ValueParses a string as JSON
StructuredOutputParser<T>StringTDeserializes JSON into a typed struct
ListOutputParserStringVec<String>Splits by a configurable separator
EnumOutputParserStringStringValidates against a list of allowed values
BooleanOutputParserStringboolParses yes/no/true/false strings
MarkdownListOutputParserStringVec<String>Parses markdown bullet lists
NumberedListOutputParserStringVec<String>Parses numbered lists
XmlOutputParserStringXmlElementParses XML into a tree structure

All parsers also implement the FormatInstructions trait, which provides a get_format_instructions() method. You can include these instructions in your prompt to guide the LLM toward producing output in the expected format.

Quick Example

use synaptic::parsers::StrOutputParser;
use synaptic::runnables::Runnable;
use synaptic::core::{Message, RunnableConfig};

let parser = StrOutputParser;
let config = RunnableConfig::default();
let result = parser.invoke(Message::ai("Hello world"), &config).await?;
assert_eq!(result, "Hello world");

Sub-Pages