Class ResponseCorrection

java.lang.Object
ai.redouble.nucleo.tools.ResponseCorrection

public final class ResponseCorrection extends Object
The self-correction of one LLM exchange, shared by every job that sends a conversation and expects a typed answer: a response that fails to parse, or parses but fails the handler's required-field validation, is fed back to the model. The failed assistant message is already in the conversation (clients append it); this appends a user-role correction message carrying the error explanation and the ORIGINAL response handler, then throws ResponseCorrectionRetryException for the dispatcher to re-run the job transparently on the grown conversation. The budget is 2 corrections per job instance, so it survives the dispatcher's re-runs. On exhaustion the failure surfaces, a parse failure as JsonParseException and a missing required field as ResponseValidationException: required is required, and no caller receives an answer with a hole in it.
Since:
0.1 (2026-09-16)
Author:
Andrey Santrosyan
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Correction budget per job: at most this many parse/validation feedback retries.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    exchange(LLMClient client, ConversationContext conversation)
    One exchange: the conversation's last outgoing message sent on the client, the answer parsed or corrected.
    int
    Corrections consumed so far by the job holding this.
    <T> T
    parseOrCorrect(LLMResponse<T> response, ConversationContext conversation, OutgoingMessage<T> outgoingMessage)
    The parsed answer of a response, or the correction protocol: within budget the failure becomes a correction turn and a retry signal; past it, the failure surfaces, whether the answer did not parse or lacks a required field.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_CORRECTIONS

      public static final int MAX_CORRECTIONS
      Correction budget per job: at most this many parse/validation feedback retries.
      See Also:
  • Constructor Details

    • ResponseCorrection

      public ResponseCorrection()
  • Method Details

    • getUsed

      public int getUsed()
      Corrections consumed so far by the job holding this.
    • exchange

      public <T> T exchange(LLMClient client, ConversationContext conversation) throws LLMReadableCheckedException
      One exchange: the conversation's last outgoing message sent on the client, the answer parsed or corrected. The outgoing message is taken before the call, since the client appends the answer and the last message is no longer outgoing afterwards. What a thinker's call and a one-call tool both do, written once.
      Throws:
      LLMReadableCheckedException
    • parseOrCorrect

      public <T> T parseOrCorrect(LLMResponse<T> response, ConversationContext conversation, OutgoingMessage<T> outgoingMessage) throws LLMReadableCheckedException
      The parsed answer of a response, or the correction protocol: within budget the failure becomes a correction turn and a retry signal; past it, the failure surfaces, whether the answer did not parse or lacks a required field.
      Parameters:
      response - what the client answered
      conversation - the conversation the correction turn is appended to
      outgoingMessage - the message the response answered, taken before the call, since the client appends the answer after it
      Throws:
      LLMReadableCheckedException