Class RetryableParser<T>

java.lang.Object
com.tnsai.parser.RetryableParser<T>
Type Parameters:
T - the target type
All Implemented Interfaces:
OutputParser<T>

public class RetryableParser<T> extends Object implements 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);
 
  • Method Details

    • wrap

      public static <T> RetryableParser.Builder<T> wrap(OutputParser<T> parser)
      Creates a builder wrapping the given parser.
      Type Parameters:
      T - the type
      Parameters:
      parser - the base parser to wrap
      Returns:
      new builder
    • parse

      public ParseResult<T> parse(String output)
      Description copied from interface: OutputParser
      Parses the raw LLM output into the target type.
      Specified by:
      parse in interface OutputParser<T>
      Parameters:
      output - the raw string output from the LLM
      Returns:
      parse result containing either the parsed object or error details
    • getTargetType

      public Class<T> getTargetType()
      Description copied from interface: OutputParser
      Returns the target class this parser produces.
      Specified by:
      getTargetType in interface OutputParser<T>
      Returns:
      the target class
    • getFormatInstruction

      public String getFormatInstruction()
      Description copied from interface: OutputParser
      Returns a format instruction string to include in the LLM prompt.

      This helps guide the LLM to produce output in the expected format.

      Specified by:
      getFormatInstruction in interface OutputParser<T>
      Returns:
      format instruction for the prompt
    • getSchemaDescription

      public String getSchemaDescription()
      Description copied from interface: OutputParser
      Returns a schema description of the expected output format.
      Specified by:
      getSchemaDescription in interface OutputParser<T>
      Returns:
      schema description (e.g., JSON Schema, example)
    • getCorrectionPrompt

      public String getCorrectionPrompt(String failedOutput, String error)
      Gets a correction prompt to send to the LLM when parsing fails.
      Parameters:
      failedOutput - the output that failed to parse
      error - the error message
      Returns:
      prompt asking LLM to correct its output
    • parseWithRetry

      public ParseResult<T> parseWithRetry(String initialOutput, Function<String,String> llmFunction)
      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 output
      llmFunction - 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

      public List<RetryableParser.ParseAttempt> getAttempts()
      Gets all parse attempts made.
      Returns:
      list of attempts
    • getLastAttempt

      public RetryableParser.ParseAttempt 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