Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/agents/guardrail.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from __future__ import annotations

import inspect
Expand All @@ -22,13 +23,12 @@ class GuardrailFunctionOutput:

output_info: Any
"""
Optional information about the guardrail's output. For example, the guardrail could include
information about the checks it performed and granular results.
Optional metadata about the guardrail's execution, such as details of checks performed or results.
"""

tripwire_triggered: bool
"""
Whether the tripwire was triggered. If triggered, the agent's execution will be halted.
Whether the tripwire was triggered; if True, agent execution will halt.
"""


Expand All @@ -42,7 +42,9 @@ class InputGuardrailResult:
"""

output: GuardrailFunctionOutput
"""The output of the guardrail function."""
"""
The output of the guardrail function.
"""


@dataclass
Expand All @@ -65,12 +67,15 @@ class OutputGuardrailResult:
"""

output: GuardrailFunctionOutput
"""The output of the guardrail function."""
"""
The output of the guardrail function.
"""


@dataclass
class InputGuardrail(Generic[TContext]):
"""Input guardrails are checks that run in parallel to the agent's execution.
"""
Input guardrails are checks that run in parallel to the agent's execution.
They can be used to do things like:
- Check if input messages are off-topic
- Take over control of the agent's execution if an unexpected input is detected
Expand All @@ -86,13 +91,13 @@ class InputGuardrail(Generic[TContext]):
[RunContextWrapper[TContext], Agent[Any], str | list[TResponseInputItem]],
MaybeAwaitable[GuardrailFunctionOutput],
]
"""A function that receives the agent input and the context, and returns a
`GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally
include information about the guardrail's output.
"""
A function that receives the agent input and the context, and returns a `GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally, include information about the guardrail's output.
"""

name: str | None = None
"""The name of the guardrail, used for tracing. If not provided, we'll use the guardrail
"""
The name of the guardrail, used for tracing. If not provided, we'll use the guardrail
function's name.
"""

Expand Down Expand Up @@ -126,7 +131,8 @@ async def run(

@dataclass
class OutputGuardrail(Generic[TContext]):
"""Output guardrails are checks that run on the final output of an agent.
"""
Output guardrails are checks that run on the final output of an agent.
They can be used to do check if the output passes certain validation criteria

You can use the `@output_guardrail()` decorator to turn a function into an `OutputGuardrail`,
Expand All @@ -140,14 +146,13 @@ class OutputGuardrail(Generic[TContext]):
[RunContextWrapper[TContext], Agent[Any], Any],
MaybeAwaitable[GuardrailFunctionOutput],
]
"""A function that receives the final agent, its output, and the context, and returns a
`GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally
include information about the guardrail's output.
"""
A function that receives the final agent, its output, and the context, and returns a `GuardrailResult`. The result marks whether the tripwire was triggered, and can optionally, include information about the guardrail's output.
"""

name: str | None = None
"""The name of the guardrail, used for tracing. If not provided, we'll use the guardrail
function's name.
"""
The name of the guardrail, used for tracing. If not provided, we'll use the guardrail function's name.
"""

def get_name(self) -> str:
Expand Down
Loading