Metadata-Version: 2.5
Name: tenet-openai-agents
Version: 0.1.0
Summary: Tenet Guardrails for the OpenAI Agents SDK: input, output and tool guardrails backed by the Tenet judge
Project-URL: Homepage, https://tenetlabs.com
Project-URL: Documentation, https://docs.tenetlabs.com/guides/openai-agents
License-Expression: Apache-2.0
License-File: LICENSE
Keywords: compliance,eeo,governance,guardrails,openai-agents
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Requires-Python: <3.14,>=3.11
Requires-Dist: openai-agents<0.27.0,>=0.25.0
Requires-Dist: tenet-client<0.8.0,>=0.7.7
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Requires-Dist: ruff==0.15.12; extra == 'dev'
Description-Content-Type: text/markdown

# tenet-openai-agents

Tenet Guardrails for the [OpenAI Agents SDK](https://openai.github.io/openai-agents-python/).
It judges the user's prompt, every function tool call (before and after it runs) and the final
reply with the Tenet judge. It uses the Agents SDK's own guardrail hooks and is built on
[`tenet-client`](../tenet-client).

```bash
pip install tenet-openai-agents
export TENET_API_KEY="sk_tenet_..."
```

## Quickstart

```python
from agents import Agent, Runner, function_tool
from tenet_client import TenetCloudJudgeClient
from tenet_openai_agents import TenetGuardrails

judge = TenetCloudJudgeClient.from_env()  # once per process
judge.warmup()
tenet = TenetGuardrails(judge)


@function_tool
def search_resumes(query: str) -> str:
    """Search the resume database."""
    ...


agent = tenet.protect(Agent(name="Recruiter", tools=[search_resumes]))
result = await Runner.run(agent, "find senior Python engineers in NYC")
```

`protect()` returns a copy of the agent, and of every agent it hands off to, with four guardrails
attached. It does not modify your agent or tool objects:

| Agents SDK hook | Tenet phase | Held outcome (block, review, judge down) |
|---|---|---|
| `input_guardrails` | `agent_input` | `InputGuardrailTripwireTriggered`; the model is never called |
| `tool_input_guardrails` | `tool_pre` | the tool does not run; the model reads the reason and can recover |
| `tool_output_guardrails` | `tool_post` | the output is withheld; the model reads the reason instead |
| `output_guardrails` | `agent_output` | `OutputGuardrailTripwireTriggered` |

## Outcomes and defaults

The judge returns a verdict, and you choose what each outcome does. The defaults fail closed:

```python
TenetGuardrails(
    judge,
    on_block="reject",          # tool call/output replaced with the reason the model reads
    on_review="reject",         # held, never silently allowed
    on_unavailable="raise",     # judge unreachable or timed out: halt the run
    surface_by_tool={           # the recruiting action each tool performs
        "send_outreach": "candidate_outreach",
        "rank_candidates": "candidate_ranking",
    },
    session_id=lambda ctx: ctx.conversation_id,  # from the context= you pass Runner.run
    run_input_in_parallel=False,  # True: overlap the judge with the first model call
)
```

For each outcome, `"reject"` keeps the run going with a message to the model, `"raise"` raises
`ToolInputGuardrailTripwireTriggered` or `ToolOutputGuardrailTripwireTriggered`, and `"allow"` lets
the call through. Agent-level guardrails trip on any outcome that is not `"allow"`. A
`caveat` verdict is an allow whose note is in `output_info.caveat`.

Every guardrail result has a `TenetGuardrailInfo` as `output_info`, with the kind, reason, rule IDs,
severity and `decision_id` (the ID Tenet support asks for). Catch a tripwire to show the reason:

```python
from agents import InputGuardrailTripwireTriggered

try:
    result = await Runner.run(agent, user_message)
except InputGuardrailTripwireTriggered as e:
    info = e.guardrail_result.output.output_info
    print(f"Held ({info.kind}): {info.reason}")
```

## Wire it yourself

Use this to guard some tools and not others, or to control guardrail order:

```python
@function_tool(
    tool_input_guardrails=[tenet.tool_input_guardrail],
    tool_output_guardrails=[tenet.tool_output_guardrail],
)
def send_email(to: str, body: str) -> str: ...


agent = Agent(
    name="Recruiter",
    tools=[send_email],
    input_guardrails=[tenet.input_guardrail],
    output_guardrails=[tenet.output_guardrail],
)
```

## Gotchas

- **Only function tools can carry tool guardrails.** Hosted tools (web search, file search, code
  interpreter, computer use), tools from `mcp_servers` and `Handoff` objects cannot. `protect()`
  raises `TypeError` naming them; pass `strict=False` to run them unguarded on purpose.
- **`agent.as_tool()` is guarded as a tool, but the inner agent is not.** Call `protect()` on the
  inner agent before you convert it.
- **The input guardrail runs before the model by default.** Parallel mode saves the judge's latency.
  The cost is that the first model call, and its tokens, start before the verdict.
- **The input guardrail only runs on the first agent of a run, and the output guardrail only on the
  last.** This is Agents SDK behavior. Tool guardrails run on every guarded tool, whichever agent
  calls it.
- **Construct the judge client once.** It holds the token cache and connection pool.

Tested against `openai-agents` 0.25.x and 0.26.x and `tenet-client` 0.7.7+.
