Class JsonExtractorOutputGuardrail<T>

java.lang.Object
dev.langchain4j.guardrails.JsonExtractorOutputGuardrail<T>
Type Parameters:
T - The type of object that the class should deserialize from JSON
All Implemented Interfaces:
dev.langchain4j.guardrail.Guardrail<dev.langchain4j.guardrail.OutputGuardrailRequest, dev.langchain4j.guardrail.OutputGuardrailResult>, dev.langchain4j.guardrail.OutputGuardrail

public class JsonExtractorOutputGuardrail<T> extends Object implements dev.langchain4j.guardrail.OutputGuardrail
An OutputGuardrail that will check whether or not a response can be successfully deserialized to an object of type T from JSON

If deserialization fails, the LLM will be reprompted with getInvalidJsonReprompt(AiMessage, String), which defaults to DEFAULT_REPROMPT_PROMPT.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default message to use when reprompting
    static final String
    The default prompt to append to the LLM during a reprompt
  • Constructor Summary

    Constructors
    Constructor
    Description
    JsonExtractorOutputGuardrail(com.fasterxml.jackson.core.type.TypeReference<T> outputType)
     
    JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.fasterxml.jackson.core.type.TypeReference<T> outputType)
     
    JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<T> outputClass)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Optional<dev.langchain4j.internal.JsonParsingUtils.ParsedJson<T>>
    deserialize(String llmResponse)
    Tries to deserialize the provided LLM response string into an object of type T using the configured ObjectMapper.
    protected String
    getInvalidJsonMessage(dev.langchain4j.data.message.AiMessage aiMessage, String json)
    Generates a message indicating that the provided JSON is invalid.
    protected String
    getInvalidJsonReprompt(dev.langchain4j.data.message.AiMessage aiMessage, String json)
    Generates a reprompt message indicating that the provided JSON is invalid.
    protected dev.langchain4j.guardrail.OutputGuardrailResult
    invokeInvalidJson(dev.langchain4j.data.message.AiMessage aiMessage, String json)
     
    dev.langchain4j.guardrail.OutputGuardrailResult
    validate(dev.langchain4j.data.message.AiMessage responseFromLLM)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface dev.langchain4j.guardrail.Guardrail

    name

    Methods inherited from interface dev.langchain4j.guardrail.OutputGuardrail

    failure, failure, failureWithMessageRemoval, failureWithMessageRemoval, fatal, fatal, fatalWithMessageRemoval, fatalWithMessageRemoval, reprompt, reprompt, retry, retry, success, successWith, successWith, successWith, successWith, validate
  • Field Details

    • DEFAULT_REPROMPT_MESSAGE

      public static final String DEFAULT_REPROMPT_MESSAGE
      The default message to use when reprompting
      See Also:
    • DEFAULT_REPROMPT_PROMPT

      public static final String DEFAULT_REPROMPT_PROMPT
      The default prompt to append to the LLM during a reprompt
      See Also:
  • Constructor Details

    • JsonExtractorOutputGuardrail

      public JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<T> outputClass)
    • JsonExtractorOutputGuardrail

      public JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.fasterxml.jackson.core.type.TypeReference<T> outputType)
    • JsonExtractorOutputGuardrail

      public JsonExtractorOutputGuardrail(Class<T> outputClass)
    • JsonExtractorOutputGuardrail

      public JsonExtractorOutputGuardrail(com.fasterxml.jackson.core.type.TypeReference<T> outputType)
  • Method Details

    • validate

      public dev.langchain4j.guardrail.OutputGuardrailResult validate(dev.langchain4j.data.message.AiMessage responseFromLLM)
      Specified by:
      validate in interface dev.langchain4j.guardrail.OutputGuardrail
    • invokeInvalidJson

      protected dev.langchain4j.guardrail.OutputGuardrailResult invokeInvalidJson(dev.langchain4j.data.message.AiMessage aiMessage, String json)
    • getInvalidJsonMessage

      protected String getInvalidJsonMessage(dev.langchain4j.data.message.AiMessage aiMessage, String json)
      Generates a message indicating that the provided JSON is invalid.
      Parameters:
      aiMessage - the AI message associated with the invalid JSON. This parameter is not used.
      json - the JSON that failed validation. This parameter is not used.
      Returns:
      a default message indicating that the JSON is invalid.
    • getInvalidJsonReprompt

      protected String getInvalidJsonReprompt(dev.langchain4j.data.message.AiMessage aiMessage, String json)
      Generates a reprompt message indicating that the provided JSON is invalid.

      This message is appended to the user message from the previous request.

      Parameters:
      aiMessage - the AI message associated with the invalid JSON. This parameter is not used.
      json - the JSON input that failed validation. This parameter is not used.
      Returns:
      a reprompt message indicating that the JSON is invalid.
    • deserialize

      protected Optional<dev.langchain4j.internal.JsonParsingUtils.ParsedJson<T>> deserialize(String llmResponse)
      Tries to deserialize the provided LLM response string into an object of type T using the configured ObjectMapper. If deserialization fails, an empty Optional is returned.
      Parameters:
      llmResponse - the JSON-formatted response string to be deserialized
      Returns:
      an Optional containing the deserialized object if successful, or an empty Optional if deserialization fails