Package com.tnsai.parser
Class FormatAwareOutputParser<T>
java.lang.Object
com.tnsai.parser.FormatAwareOutputParser<T>
- Type Parameters:
T- the target type
- All Implemented Interfaces:
OutputParser<T>
Output parser that supports multiple formats (JSON, YAML, TOON, TONL).
Delegates to appropriate serializer based on configured format.
Usage:
// For JSON output (default)
OutputParser<User> parser = FormatAwareOutputParser.json(User.class);
// For YAML output
OutputParser<Spec> parser = FormatAwareOutputParser.yaml(Spec.class);
// For token-optimized TOON format
OutputParser<Report> parser = FormatAwareOutputParser.toon(Report.class);
// Use with agent
User user = agent.chatWithStructure("Get user info", parser, 3);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for FormatAwareOutputParser with fluent API. -
Constructor Summary
ConstructorsConstructorDescriptionFormatAwareOutputParser(Class<T> targetType, OutputFormat format) Creates a parser for single object output.FormatAwareOutputParser(Class<T> targetType, OutputFormat format, boolean isListOutput) Creates a parser with list output option. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> FormatAwareOutputParser.Builder<T> Creates a builder for more complex parser configurations.static <T> FormatAwareOutputParser<T> forFormat(Class<T> targetType, OutputFormat format) Gets the output format this parser uses.Returns a format instruction string to include in the LLM prompt.Returns a schema description of the expected output format.Returns the target class this parser produces.static <T> FormatAwareOutputParser<T> Parses the raw LLM output into the target type.static <T> FormatAwareOutputParser<T> static <T> FormatAwareOutputParser<T> static <T> FormatAwareOutputParser<T> static <T> FormatAwareOutputParser<T> Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.parser.OutputParser
getErrorCorrectionPrompt, parseOptional, parseOrThrow
-
Constructor Details
-
FormatAwareOutputParser
Creates a parser for single object output. -
FormatAwareOutputParser
Creates a parser with list output option.
-
-
Method Details
-
json
-
yaml
-
toon
-
tonl
-
text
-
forFormat
-
parse
Description copied from interface:OutputParserParses the raw LLM output into the target type.- Specified by:
parsein interfaceOutputParser<T>- Parameters:
output- the raw string output from the LLM- Returns:
- parse result containing either the parsed object or error details
-
getTargetType
Description copied from interface:OutputParserReturns the target class this parser produces.- Specified by:
getTargetTypein interfaceOutputParser<T>- Returns:
- the target class
-
getFormatInstruction
Description copied from interface:OutputParserReturns a format instruction string to include in the LLM prompt.This helps guide the LLM to produce output in the expected format.
- Specified by:
getFormatInstructionin interfaceOutputParser<T>- Returns:
- format instruction for the prompt
-
getSchemaDescription
Description copied from interface:OutputParserReturns a schema description of the expected output format.- Specified by:
getSchemaDescriptionin interfaceOutputParser<T>- Returns:
- schema description (e.g., JSON Schema, example)
-
getFormat
Gets the output format this parser uses. -
builder
Creates a builder for more complex parser configurations.
-