Package com.tnsai.parser
Class RetryableParser<T>
java.lang.Object
com.tnsai.parser.RetryableParser<T>
- Type Parameters:
T- the target type
- All Implemented Interfaces:
OutputParser<T>
Wrapper that adds retry-with-error-correction capability to any OutputParser.
When parsing fails, this parser can generate an error correction prompt that can be sent back to the LLM to fix its output. This implements the "Automatic Retry with Error" pattern from production LLM systems.
Usage
// Create base parser
JsonOutputParser<Person> baseParser = JsonOutputParser.forType(Person.class);
// Wrap with retry capability
RetryableParser<Person> parser = RetryableParser.wrap(baseParser)
.maxRetries(3)
.build();
// First attempt
ParseResult<Person> result = parser.parse(llmOutput);
// If failed, get correction prompt
if (result.isFailure()) {
String correctionPrompt = parser.getCorrectionPrompt(llmOutput, result.getError());
// Send correctionPrompt to LLM for another attempt
}
// Or use with LLM integration
Person person = parser.parseWithRetry(initialOutput, llm::complete);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for RetryableParser.static final recordRecord of a single parse attempt. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears the attempt history.intGets the number of parse attempts made.Gets all parse attempts made.getCorrectionPrompt(String failedOutput, String error) Gets a correction prompt to send to the LLM when parsing fails.Returns a format instruction string to include in the LLM prompt.Gets the last parse attempt.intGets the maximum number of retries configured.Returns a schema description of the expected output format.Returns the target class this parser produces.Parses the raw LLM output into the target type.parseWithRetry(String initialOutput, Function<String, String> llmFunction) Parses with automatic retry using the provided LLM function.static <T> RetryableParser.Builder<T> wrap(OutputParser<T> parser) Creates a builder wrapping the given parser.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
-
Method Details
-
wrap
Creates a builder wrapping the given parser.- Type Parameters:
T- the type- Parameters:
parser- the base parser to wrap- Returns:
- new builder
-
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)
-
getCorrectionPrompt
Gets a correction prompt to send to the LLM when parsing fails.- Parameters:
failedOutput- the output that failed to parseerror- the error message- Returns:
- prompt asking LLM to correct its output
-
parseWithRetry
Parses with automatic retry using the provided LLM function.If parsing fails, this method generates a correction prompt and calls the LLM function to get a corrected response, up to maxRetries times.
- Parameters:
initialOutput- the initial LLM outputllmFunction- function that takes a prompt and returns LLM response- Returns:
- final parse result after all attempts
-
getAttemptCount
public int getAttemptCount()Gets the number of parse attempts made.- Returns:
- attempt count
-
getAttempts
Gets all parse attempts made.- Returns:
- list of attempts
-
getLastAttempt
Gets the last parse attempt.- Returns:
- last attempt or null if no attempts
-
clearAttempts
public void clearAttempts()Clears the attempt history. -
getMaxRetries
public int getMaxRetries()Gets the maximum number of retries configured.- Returns:
- max retries
-