Class JsonExtractorOutputGuardrail<T>
- Type Parameters:
T- The type of object that the class should deserialize from JSON
- All Implemented Interfaces:
Guardrail<OutputGuardrailRequest, OutputGuardrailResult>, OutputGuardrail
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.
Deserialization uses a plain Jackson ObjectMapper with its default settings. A
guardrail exists to reject output that does not fit the expected shape, so it deliberately
does not use LangChain4j's own JSON codec, which is more forgiving: that codec reads private
fields without setters, accepts enum constants case-insensitively, and parses dates an LLM
has written in a field-wise form. Output this guardrail rejects should stay rejected.
One consequence is that this guardrail always uses Jackson 2, and is the one part of
LangChain4j that the langchain4j-jackson3 opt-in does not switch over.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJsonExtractorOutputGuardrail(com.fasterxml.jackson.core.type.TypeReference<T> outputType) Deprecated, for removal: This API element is subject to removal in a future version.JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.fasterxml.jackson.core.type.TypeReference<T> outputType) Deprecated, for removal: This API element is subject to removal in a future version.useJsonExtractorOutputGuardrail(Type), which does not expose Jackson types in its signature.JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<T> outputClass) Deprecated, for removal: This API element is subject to removal in a future version.useJsonExtractorOutputGuardrail(Class), which does not expose Jackson types in its signature.JsonExtractorOutputGuardrail(Class<T> outputClass) Deserializes with a plain JacksonObjectMapper, as this guardrail has always done.JsonExtractorOutputGuardrail(Type outputType) Deserializes with a plain JacksonObjectMapper, matchingJsonExtractorOutputGuardrail(Class). -
Method Summary
Modifier and TypeMethodDescriptionprotected Optional<JsonParsingUtils.ParsedJson<T>> deserialize(String llmResponse) Tries to deserialize the provided LLM response string into an object of type T using the configuredObjectMapper.protected StringgetInvalidJsonMessage(AiMessage aiMessage, String json) Generates a message indicating that the provided JSON is invalid.protected StringgetInvalidJsonReprompt(AiMessage aiMessage, String json) Generates a reprompt message indicating that the provided JSON is invalid.protected OutputGuardrailResultinvokeInvalidJson(AiMessage aiMessage, String json) Validates the response from the LLM.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Guardrail
name, validateAsyncMethods inherited from interface OutputGuardrail
failure, failure, failureWithMessageRemoval, failureWithMessageRemoval, fatal, fatal, fatalWithMessageRemoval, fatalWithMessageRemoval, reprompt, reprompt, retry, retry, success, successWith, successWith, successWith, successWith, validate
-
Field Details
-
DEFAULT_REPROMPT_MESSAGE
The default message to use when reprompting- See Also:
-
DEFAULT_REPROMPT_PROMPT
The default prompt to append to the LLM during a reprompt- See Also:
-
-
Constructor Details
-
JsonExtractorOutputGuardrail
@Deprecated(since="1.20.0", forRemoval=true) public JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Class<T> outputClass) Deprecated, for removal: This API element is subject to removal in a future version.useJsonExtractorOutputGuardrail(Class), which does not expose Jackson types in its signature. It parses with a defaultObjectMapper, so pass one here only if you need a mapper configured differently. -
JsonExtractorOutputGuardrail
@Deprecated(since="1.20.0", forRemoval=true) public JsonExtractorOutputGuardrail(com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.fasterxml.jackson.core.type.TypeReference<T> outputType) Deprecated, for removal: This API element is subject to removal in a future version.useJsonExtractorOutputGuardrail(Type), which does not expose Jackson types in its signature. Passnew TypeReference<Foo>() {}.getType()to migrate, and pass a mapper here only if you need one configured differently from the default. -
JsonExtractorOutputGuardrail
-
JsonExtractorOutputGuardrail
Deserializes with a plain JacksonObjectMapper, matchingJsonExtractorOutputGuardrail(Class). -
JsonExtractorOutputGuardrail
@Deprecated(since="1.20.0", forRemoval=true) public JsonExtractorOutputGuardrail(com.fasterxml.jackson.core.type.TypeReference<T> outputType) Deprecated, for removal: This API element is subject to removal in a future version.useJsonExtractorOutputGuardrail(Type), which does not expose Jackson types -TypeReferencelives in Jackson's core package, which moved in Jackson 3. Passnew TypeReference<Foo>() {}.getType()to migrate.
-
-
Method Details
-
validate
Description copied from interface:OutputGuardrailValidates the response from the LLM.- Specified by:
validatein interfaceOutputGuardrail- Parameters:
responseFromLLM- the response from the LLM
-
invokeInvalidJson
-
getInvalidJsonMessage
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
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
Tries to deserialize the provided LLM response string into an object of type T using the configuredObjectMapper. 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
-
JsonExtractorOutputGuardrail(Type), which does not expose Jackson types -TypeReferencelives in Jackson's core package, which moved in Jackson 3.