Interface GuardedToolExecution
ToolGuardrailService.executeGuardrails(ToolGuardrailService.ToolKey, ToolExecutionRequest, GuardrailRequestParams, Function): the call as the input guardrails left it, and the result the LLM gets.
An input guardrail can rewrite the arguments of the call. The rewritten request replaces the one of the LLM for
everything that follows, the way a rewritten user message does for the AI service guardrails: report the call
with request(), not with the request the LLM sent.
- Since:
- 1.25.0
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic GuardedToolExecution.Builderbuilder()Creates a builder.The stage whose guardrails turned the call into an error result, so that a caller does not have to infer it:ToolGuardrailStage.INPUTwhen an input guardrail rejected the call (the tool did not run), andToolGuardrailStage.OUTPUTwhen the output guardrails turned the successful result of the tool into an error result (a failure, or a rewrite to an error).booleanWhether a tool output guardrail replaced the result of the tool, with a rewrite or with the message of a non-fatal failure:result()is then not the result the tool returned.request()The tool call as the input guardrails left it: the request of the LLM, with the arguments rewritten by the input guardrails if they rewrote them.result()The result to send to the LLM: the result of the tool as the output guardrails left it, or the error result of a guardrail failure (whose text is the failure message, verbatim).resultContentsForLlm(List<Content> contents) The contents that the LLM gets for contents of a result of this call.default GuardedToolExecution.BuilderCreates a builder that starts from this execution, for a service that decorates the execution ofToolGuardrailService.executeGuardrails(ToolGuardrailService.ToolKey, ToolExecutionRequest, GuardrailRequestParams, Function).
-
Method Details
-
builder
-
toBuilder
Creates a builder that starts from this execution, for a service that decorates the execution ofToolGuardrailService.executeGuardrails(ToolGuardrailService.ToolKey, ToolExecutionRequest, GuardrailRequestParams, Function). The copy keeps the arguments rewrite note of this execution (seeresultContentsForLlm(List)), also when it is given another request: the note describes what the input guardrails changed in the call of the LLM.- Returns:
- A new builder, with the request, the result, the failed stage, whether the result was replaced and the note of this execution.
-
request
ToolExecutionRequest request()The tool call as the input guardrails left it: the request of the LLM, with the arguments rewritten by the input guardrails if they rewrote them. For a call an input guardrail rejected, the arguments as the guardrails that ran before the rejection left them.- Returns:
- The request, never
null.
-
result
ToolExecutionResult result()The result to send to the LLM: the result of the tool as the output guardrails left it, or the error result of a guardrail failure (whose text is the failure message, verbatim).- Returns:
- The result, never
null.
-
failedStage
Optional<ToolGuardrailStage> failedStage()The stage whose guardrails turned the call into an error result, so that a caller does not have to infer it:ToolGuardrailStage.INPUTwhen an input guardrail rejected the call (the tool did not run), andToolGuardrailStage.OUTPUTwhen the output guardrails turned the successful result of the tool into an error result (a failure, or a rewrite to an error).Empty for every other call: a call without a guardrail failure, a tool that failed on its own (also when an output guardrail rewrote its error result), and a rewrite that keeps a success. A fatal failure does not return an execution: it throws a
ToolInputGuardrailExceptionor aToolOutputGuardrailException.- Returns:
- The stage, empty if no guardrail failed the call.
-
isResultReplaced
boolean isResultReplaced()Whether a tool output guardrail replaced the result of the tool, with a rewrite or with the message of a non-fatal failure:result()is then not the result the tool returned. That is also the case when an output guardrail rewrote the error result of a tool that failed on its own.falsewhen the tool's result is sent as it is, and when an input guardrail rejected the call (the tool did not run).A framework that reports the call with its own
ToolExecutionpasses it toToolExecution.Builder.resultReplaced(boolean).- Returns:
- Whether a tool output guardrail replaced the result of the tool.
-
resultContentsForLlm
The contents that the LLM gets for contents of a result of this call. They are the contents as they are, unless an input guardrail changed a value the LLM sent and the tool ran with it: the chat memory keeps the call as the LLM made it, so the arguments rewrite note of theToolInputGuardrailsConfigof the service tells the LLM what ran. The note then goes into the first text content, or, when there is none, into a text content of its own, in front of the other contents.Apply it to the contents of every result message of the call that the LLM gets: the contents of
result(), and the text that replaces the result when the call is rolled back (List.of(TextContent.from(text))). The events and theToolExecutions of the call keep the result without the note.- Parameters:
contents- The contents of a result of this call. Must not be null.- Returns:
- The contents the LLM gets:
contentsitself when there is no note, otherwise an unmodifiable copy with the note.
-