Annotation Interface ToolOutputGuardrails


@Experimental @Retention(RUNTIME) @Documented @Target({TYPE,METHOD}) public @interface ToolOutputGuardrails
Applies ToolOutputGuardrails to a tool: they validate, or rewrite, the result of the tool before the LLM sees it. This is the place to protect against indirect prompt injection in text returned by tools.

The annotation can be placed on a tool method (annotated with @Tool) or on the class declaring tool methods. On a class, it applies to every tool method of that class.

Within the class level and within the method level, the nearest declaration wins. A class annotation is looked up on the class of the object the tool runs on, then on its superclasses, then on its interfaces: an annotation on a subclass replaces the one of its parents (and an annotation without guardrails removes them), also for the tool methods the subclass only inherits. This differs from the class-level annotations of Spring and Jakarta EE (such as @Transactional or @RolesAllowed), which do not apply to inherited methods. A method annotation is looked up on the tool method of that class, then on the methods it overrides in its superclasses, then in its interfaces (@Tool can be on any of them, and an overload is not part of the chain), so an annotation on an implementation applies when @Tool is on the interface, and the reverse. The class annotation is found through the ClassMetadataProviderFactory, and the method annotation is read from the methods.

A framework that runs its own tool loop, and has no Method for a tool, does not have these annotations read: it passes the guardrails it found for the tool as the guardrails of the AiServiceTool, and binds the tool with bind(tool).

The levels add up, as one chain. They run in the reverse order of the input guardrails, so that the guardrails set on the builder, which belong to the application, are the last to see the result before it goes to the LLM (the note that an input guardrail changed the arguments, if any, is added after them):

  1. the guardrails of the AiServiceTool of the tool
  2. the guardrails of the method annotation
  3. the guardrails of the class annotation
  4. the guardrails set on the AiServices builder (they apply to every tool)

Within a level, the guardrails run in the order they are given.

No level replaces another. This differs from OutputGuardrails, where the guardrails set on the builder replace the ones of the annotations. A guardrail that must not apply to some tools can skip them by checking toolName() or toolSource().

The guardrails also run on error results; use isError() to skip them. Unlike OutputGuardrails, there is no retry or reprompt: a failure is sent to the LLM as the result of the tool.

Duplicates are removed: the same guardrail class declared by annotations or by the toolOutputGuardrailClasses(..) methods of the builder, at any level, runs once for a tool, at the position of the builder (last) when the builder names it, otherwise at its first occurrence. Guardrail instances set on the builder or on an AiServiceTool are only duplicates of themselves (the same object twice), since two instances of one class can differ in their state, and an instance and a class naming the same class both run.

This annotation is unrelated to OutputGuardrails, which applies to the response of an AI service. On a class that is both an AI service and is used as a tool, each annotation keeps its own meaning. On an AI service interface, it does not guard the tools the AI service calls: it only guards a method of the interface that is itself a tool. Building an AI service whose annotation guards no tool fails with an IllegalConfigurationException.

Since:
1.25.0
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The tool output guardrail classes to apply.
  • Element Details

    • value

      Class<? extends ToolOutputGuardrail>[] value
      The tool output guardrail classes to apply. They are instantiated through the same mechanism as the classes of OutputGuardrails.
      Returns:
      the guardrail classes, in the order they are applied.