|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import inspect |
| 4 | +from collections.abc import Awaitable |
| 5 | +from dataclasses import dataclass |
| 6 | +from typing import Any, Callable, Generic, Optional, overload |
| 7 | + |
| 8 | +from typing_extensions import TypeVar |
| 9 | + |
| 10 | +from .agent import Agent |
| 11 | +from .tool_context import ToolContext |
| 12 | +from .util._types import MaybeAwaitable |
| 13 | +from openai.types.responses import ResponseFunctionToolCall |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class ToolGuardrailFunctionOutput: |
| 18 | + """The output of a tool guardrail function. |
| 19 | +
|
| 20 | + - `output_info`: Optional data about checks performed. |
| 21 | + - `tripwire_triggered`: Whether the guardrail was tripped. |
| 22 | + - `model_message`: Message to send back to the model as the tool output if tripped. |
| 23 | + """ |
| 24 | + |
| 25 | + output_info: Any |
| 26 | + tripwire_triggered: bool |
| 27 | + model_message: Optional[str] = None |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class ToolInputGuardrailData: |
| 32 | + """Input data passed to a tool input guardrail function.""" |
| 33 | + |
| 34 | + context: ToolContext[Any] |
| 35 | + agent: Agent[Any] |
| 36 | + tool_call: ResponseFunctionToolCall |
| 37 | + |
| 38 | + |
| 39 | +@dataclass |
| 40 | +class ToolOutputGuardrailData(ToolInputGuardrailData): |
| 41 | + """Input data passed to a tool output guardrail function. |
| 42 | +
|
| 43 | + Extends input data with the tool's output. |
| 44 | + """ |
| 45 | + |
| 46 | + output: Any |
| 47 | + |
| 48 | + |
| 49 | +TContext_co = TypeVar("TContext_co", bound=Any, covariant=True) |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class ToolInputGuardrail(Generic[TContext_co]): |
| 54 | + """A guardrail that runs before a function tool is invoked.""" |
| 55 | + |
| 56 | + guardrail_function: Callable[[ToolInputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]] |
| 57 | + name: str | None = None |
| 58 | + |
| 59 | + def get_name(self) -> str: |
| 60 | + return self.name or self.guardrail_function.__name__ |
| 61 | + |
| 62 | + async def run( |
| 63 | + self, data: ToolInputGuardrailData |
| 64 | + ) -> ToolGuardrailFunctionOutput: |
| 65 | + result = self.guardrail_function(data) |
| 66 | + if inspect.isawaitable(result): |
| 67 | + return await result # type: ignore[return-value] |
| 68 | + return result # type: ignore[return-value] |
| 69 | + |
| 70 | + |
| 71 | +@dataclass |
| 72 | +class ToolOutputGuardrail(Generic[TContext_co]): |
| 73 | + """A guardrail that runs after a function tool is invoked.""" |
| 74 | + |
| 75 | + guardrail_function: Callable[[ToolOutputGuardrailData], MaybeAwaitable[ToolGuardrailFunctionOutput]] |
| 76 | + name: str | None = None |
| 77 | + |
| 78 | + def get_name(self) -> str: |
| 79 | + return self.name or self.guardrail_function.__name__ |
| 80 | + |
| 81 | + async def run( |
| 82 | + self, data: ToolOutputGuardrailData |
| 83 | + ) -> ToolGuardrailFunctionOutput: |
| 84 | + result = self.guardrail_function(data) |
| 85 | + if inspect.isawaitable(result): |
| 86 | + return await result # type: ignore[return-value] |
| 87 | + return result # type: ignore[return-value] |
| 88 | + |
| 89 | + |
| 90 | +# Decorators |
| 91 | +_ToolInputFuncSync = Callable[[ToolInputGuardrailData], ToolGuardrailFunctionOutput] |
| 92 | +_ToolInputFuncAsync = Callable[[ToolInputGuardrailData], Awaitable[ToolGuardrailFunctionOutput]] |
| 93 | + |
| 94 | + |
| 95 | +@overload |
| 96 | +def tool_input_guardrail(func: _ToolInputFuncSync): # type: ignore[overload-overlap] |
| 97 | + ... |
| 98 | + |
| 99 | + |
| 100 | +@overload |
| 101 | +def tool_input_guardrail(func: _ToolInputFuncAsync): # type: ignore[overload-overlap] |
| 102 | + ... |
| 103 | + |
| 104 | + |
| 105 | +@overload |
| 106 | +def tool_input_guardrail(*, name: str | None = None) -> Callable[[ |
| 107 | + _ToolInputFuncSync | _ToolInputFuncAsync |
| 108 | +], ToolInputGuardrail[Any]]: ... |
| 109 | + |
| 110 | + |
| 111 | +def tool_input_guardrail( |
| 112 | + func: _ToolInputFuncSync | _ToolInputFuncAsync | None = None, |
| 113 | + *, |
| 114 | + name: str | None = None, |
| 115 | +) -> ToolInputGuardrail[Any] | Callable[[ |
| 116 | + _ToolInputFuncSync | _ToolInputFuncAsync |
| 117 | +], ToolInputGuardrail[Any]]: |
| 118 | + """Decorator to create a ToolInputGuardrail from a function.""" |
| 119 | + |
| 120 | + def decorator(f: _ToolInputFuncSync | _ToolInputFuncAsync) -> ToolInputGuardrail[Any]: |
| 121 | + return ToolInputGuardrail(guardrail_function=f, name=name or f.__name__) |
| 122 | + |
| 123 | + if func is not None: |
| 124 | + return decorator(func) |
| 125 | + return decorator |
| 126 | + |
| 127 | + |
| 128 | +_ToolOutputFuncSync = Callable[[ToolOutputGuardrailData], ToolGuardrailFunctionOutput] |
| 129 | +_ToolOutputFuncAsync = Callable[[ToolOutputGuardrailData], Awaitable[ToolGuardrailFunctionOutput]] |
| 130 | + |
| 131 | + |
| 132 | +@overload |
| 133 | +def tool_output_guardrail(func: _ToolOutputFuncSync): # type: ignore[overload-overlap] |
| 134 | + ... |
| 135 | + |
| 136 | + |
| 137 | +@overload |
| 138 | +def tool_output_guardrail(func: _ToolOutputFuncAsync): # type: ignore[overload-overlap] |
| 139 | + ... |
| 140 | + |
| 141 | + |
| 142 | +@overload |
| 143 | +def tool_output_guardrail(*, name: str | None = None) -> Callable[[ |
| 144 | + _ToolOutputFuncSync | _ToolOutputFuncAsync |
| 145 | +], ToolOutputGuardrail[Any]]: ... |
| 146 | + |
| 147 | + |
| 148 | +def tool_output_guardrail( |
| 149 | + func: _ToolOutputFuncSync | _ToolOutputFuncAsync | None = None, |
| 150 | + *, |
| 151 | + name: str | None = None, |
| 152 | +) -> ToolOutputGuardrail[Any] | Callable[[ |
| 153 | + _ToolOutputFuncSync | _ToolOutputFuncAsync |
| 154 | +], ToolOutputGuardrail[Any]]: |
| 155 | + """Decorator to create a ToolOutputGuardrail from a function.""" |
| 156 | + |
| 157 | + def decorator(f: _ToolOutputFuncSync | _ToolOutputFuncAsync) -> ToolOutputGuardrail[Any]: |
| 158 | + return ToolOutputGuardrail(guardrail_function=f, name=name or f.__name__) |
| 159 | + |
| 160 | + if func is not None: |
| 161 | + return decorator(func) |
| 162 | + return decorator |
| 163 | + |
0 commit comments