Interface ToolInputGuardrail
- All Superinterfaces:
Guardrail<ToolInputGuardrailRequest, ToolInputGuardrailResult>
Unlike InputGuardrail, it has no retry or reprompt: a non-fatal failure becomes an error tool result
sent to the LLM, which may then correct itself.
A non-fatal failure does not stop the other guardrails of the chain from running; their failures are aggregated. A fatal failure stops the chain and aborts the AI service invocation.
One instance can be shared by every tool and every invocation of an AI service, and called concurrently, so implementations must be thread-safe.
- Since:
- 1.25.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault ToolInputGuardrailResultProduces a non-fatal failure.default ToolInputGuardrailResultProduces a non-fatal failure, seefailure(String).default ToolInputGuardrailResultProduces a fatal failure.default ToolInputGuardrailResultProduces a fatal failure, seefatal(String).default ToolInputGuardrailResultsuccess()Produces a successful result without any rewrite.default ToolInputGuardrailResultsuccessWith(@Nullable String rewrittenArguments) Produces a successful result with rewritten tool arguments, which replace the original ones for the following guardrails and for the tool execution.default ToolInputGuardrailResultsuccessWithArguments(Map<String, ?> rewrittenArguments) Produces a successful result with rewritten tool arguments, given as a map of argument names to values, which replace the original ones for the following guardrails and for the tool execution.validate(ToolInputGuardrailRequest request) Validates the tool execution request before the tool is executed.Methods inherited from interface Guardrail
name, validateAsync
-
Method Details
-
validate
Validates the tool execution request before the tool is executed.Implementations must not modify the request.
- Specified by:
validatein interfaceGuardrail<ToolInputGuardrailRequest, ToolInputGuardrailResult>- Parameters:
request- the request- Returns:
- the result of the validation
-
success
Produces a successful result without any rewrite.- Returns:
- the result of a successful tool input guardrail validation
-
successWith
Produces a successful result with rewritten tool arguments, which replace the original ones for the following guardrails and for the tool execution.The chat memory keeps the tool call as the LLM made it: the rewrite changes what the tool runs with and what the events report, not the history. The tool result that the LLM gets starts with a note that the arguments were changed (see
ToolInputGuardrailsConfig). Pass values that only the tool needs throughInvocationParameters, not through the arguments.- Parameters:
rewrittenArguments- the new arguments of the tool, as a JSON object.nullmeans no rewrite (a plain success). Anything else that is not a JSON object (malformed JSON, an array, a number) is a fatal failure of the guardrail: it aborts the invocation with aToolInputGuardrailException, before the tool runs.- Returns:
- the result of a successful tool input guardrail validation carrying the rewritten arguments
-
successWithArguments
Produces a successful result with rewritten tool arguments, given as a map of argument names to values, which replace the original ones for the following guardrails and for the tool execution. The map is serialized to a JSON object with the same JSON codec thatToolInputGuardrailRequest.argumentsAsMap()parses the arguments with, so a guardrail can read the arguments as a map, change them and return the map, without escaping anything by hand:var arguments = new HashMap<>(request.argumentsAsMap()); arguments.put("query", sanitize((String) arguments.get("query"))); return successWithArguments(arguments);The chat memory keeps the tool call as the LLM made it, as with
successWith(String). UnlikesuccessWith(String), anullmap is rejected: a guardrail that redacts the arguments and returnsnullby mistake must not let the original arguments through. Anullmap, or a value that has no JSON representation, is a fatal failure of the guardrail: it aborts the invocation with aToolInputGuardrailException, before the tool runs.- Parameters:
rewrittenArguments- the new arguments of the tool, by name; must not benull- Returns:
- the result of a successful tool input guardrail validation carrying the rewritten arguments
-
failure
Produces a non-fatal failure. The tool is not executed and the message is sent to the LLM verbatim as the content of an error tool result, so it is fully under your control.Do not quote the checked content in the message: that would send it to the LLM anyway. Do not leave it blank either: some LLM providers reject an empty tool result.
- Parameters:
message- the message describing the failure, sent to the LLM- Returns:
- the result of a failed tool input guardrail validation
-
failure
Produces a non-fatal failure, seefailure(String).- Parameters:
message- the message describing the failure, sent to the LLMcause- the exception that caused this failure; it is not sent to the LLM- Returns:
- the result of a failed tool input guardrail validation
-
fatal
Produces a fatal failure. It stops the other guardrails and aborts the AI service invocation with aToolInputGuardrailException; the LLM does not get a chance to retry. Use it when retrying cannot help, e.g. a rejected page that would come back identical.- Parameters:
message- the message describing the failure- Returns:
- the result of a fatally failed tool input guardrail validation
-
fatal
Produces a fatal failure, seefatal(String).- Parameters:
message- the message describing the failurecause- the exception that caused this failure- Returns:
- the result of a fatally failed tool input guardrail validation
-