Skip to content

Commit c980f5b

Browse files
committed
docs: tweak tool guardrail documentation
1 parent fda7f30 commit c980f5b

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

docs/guardrails.md

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,48 +49,7 @@ Tool guardrails wrap **function tools** and let you validate or block tool calls
4949
- Output tool guardrails run after the tool executes and can replace the output or raise a tripwire.
5050
- Tool guardrails apply only to function tools created with [`function_tool`][agents.function_tool]; hosted tools (`WebSearchTool`, `FileSearchTool`, `HostedMCPTool`, `CodeInterpreterTool`, `ImageGenerationTool`) and local runtime tools (`ComputerTool`, `ShellTool`, `ApplyPatchTool`, `LocalShellTool`) do not use this guardrail pipeline.
5151

52-
```python
53-
import json
54-
from agents import (
55-
Agent,
56-
Runner,
57-
ToolGuardrailFunctionOutput,
58-
function_tool,
59-
tool_input_guardrail,
60-
tool_output_guardrail,
61-
)
62-
63-
@tool_input_guardrail
64-
def block_secrets(data):
65-
args = json.loads(data.context.tool_arguments or "{}")
66-
if "sk-" in json.dumps(args):
67-
return ToolGuardrailFunctionOutput.reject_content(
68-
"Remove secrets before calling this tool."
69-
)
70-
return ToolGuardrailFunctionOutput.allow()
71-
72-
73-
@tool_output_guardrail
74-
def redact_output(data):
75-
text = str(data.output or "")
76-
if "sk-" in text:
77-
return ToolGuardrailFunctionOutput.reject_content("Output contained sensitive data.")
78-
return ToolGuardrailFunctionOutput.allow()
79-
80-
81-
@function_tool(
82-
tool_input_guardrails=[block_secrets],
83-
tool_output_guardrails=[redact_output],
84-
)
85-
def classify_text(text: str) -> str:
86-
"""Classify text for internal routing."""
87-
return f"length:{len(text)}"
88-
89-
90-
agent = Agent(name="Classifier", tools=[classify_text])
91-
result = Runner.run_sync(agent, "hello world")
92-
print(result.final_output)
93-
```
52+
See the code snippet below for details.
9453

9554
## Tripwires
9655

@@ -213,3 +172,48 @@ async def main():
213172
2. This is the guardrail's output type.
214173
3. This is the guardrail function that receives the agent's output, and returns the result.
215174
4. This is the actual agent that defines the workflow.
175+
176+
Lastly, here are examples of tool guardrails.
177+
178+
```python
179+
import json
180+
from agents import (
181+
Agent,
182+
Runner,
183+
ToolGuardrailFunctionOutput,
184+
function_tool,
185+
tool_input_guardrail,
186+
tool_output_guardrail,
187+
)
188+
189+
@tool_input_guardrail
190+
def block_secrets(data):
191+
args = json.loads(data.context.tool_arguments or "{}")
192+
if "sk-" in json.dumps(args):
193+
return ToolGuardrailFunctionOutput.reject_content(
194+
"Remove secrets before calling this tool."
195+
)
196+
return ToolGuardrailFunctionOutput.allow()
197+
198+
199+
@tool_output_guardrail
200+
def redact_output(data):
201+
text = str(data.output or "")
202+
if "sk-" in text:
203+
return ToolGuardrailFunctionOutput.reject_content("Output contained sensitive data.")
204+
return ToolGuardrailFunctionOutput.allow()
205+
206+
207+
@function_tool(
208+
tool_input_guardrails=[block_secrets],
209+
tool_output_guardrails=[redact_output],
210+
)
211+
def classify_text(text: str) -> str:
212+
"""Classify text for internal routing."""
213+
return f"length:{len(text)}"
214+
215+
216+
agent = Agent(name="Classifier", tools=[classify_text])
217+
result = Runner.run_sync(agent, "hello world")
218+
print(result.final_output)
219+
```

0 commit comments

Comments
 (0)