Metadata-Version: 2.5
Name: gaas-openai-agents
Version: 0.3.0
Summary: OpenAI Agents SDK integration for GaaS (Governance as a Service)
Project-URL: Homepage, https://gaas.is
Project-URL: Documentation, https://gaas.to/sdks.html
Project-URL: Repository, https://github.com/H2OmAI/gaas
Project-URL: Changelog, https://github.com/H2OmAI/gaas/blob/main/CHANGELOG.md
Author-email: H2Om <sdk@gaas.is>
License-Expression: Apache-2.0
Keywords: agents,ai,gaas,governance,openai,openai-agents
Classifier: Development Status :: 3 - Alpha
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 :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: gaas-sdk>=0.2.7
Requires-Dist: httpx>=0.27.0
Provides-Extra: all
Requires-Dist: openai-agents>=0.17; extra == 'all'
Requires-Dist: openai<2.45; extra == 'all'
Provides-Extra: dev
Requires-Dist: openai-agents<0.18,>=0.17; extra == 'dev'
Requires-Dist: openai<2.45; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24.0; extra == 'dev'
Requires-Dist: pytest>=8.3.0; extra == 'dev'
Provides-Extra: openai-agents
Requires-Dist: openai-agents>=0.17; extra == 'openai-agents'
Requires-Dist: openai<2.45; extra == 'openai-agents'
Description-Content-Type: text/markdown

# gaas-openai-agents

GaaS (Governance as a Service) integration for the [OpenAI Agents SDK](https://github.com/openai/openai-agents-python).

Every function tool call is governed **before it executes**: the adapter submits a
governance intent to GaaS, and only an APPROVE (or an approved escalation) lets the
tool run. A BLOCK verdict raises `GovernanceBlockedError` **out of `Runner.run()`** —
the run halts; the error is never converted into text the model can route around.

```bash
pip install gaas-openai-agents[openai-agents]
```

> **Fails closed by default (since 0.2.0).** If GaaS cannot give a decision
> (unreachable, timeout, any HTTP error including a wrong API key), the tool does
> **not** run. Set `fail_open=True` to run it anyway. See
> [When GaaS can't answer](#when-gaas-cant-answer).

## Quickstart

```python
from agents import Agent, Runner, function_tool
from gaas_openai_agents import govern_tools, GaaSGovernanceConfig

@function_tool
def send_email(to: str, subject: str, body: str) -> str:
    """Send an email."""
    ...

config = GaaSGovernanceConfig(api_key="gsk_...", agent_id="my-agent")
agent = Agent(
    name="assistant",
    instructions="You are a helpful assistant.",
    tools=govern_tools([send_email], config=config),
)

result = await Runner.run(agent, "Email the Q3 report to finance@acme.com")
```

## Verdict flow

```
tool call → GaaS intent → ┌─────────┐
                          │ APPROVE │ → tool executes
                          │ BLOCK   │ → GovernanceBlockedError raised out of Runner.run()
                          │ ESCALATE│ → raises by default; hold-and-poll optional (below)
                          │NO ANSWER│ → GovernanceBlockedError (UNEVALUATED) out of Runner.run()
                          └─────────┘
```

Only governance blocks halt the run. Errors *inside* your tool function keep the
framework's default behaviour (formatted for the model; the run continues).

## Configuration

```python
from gaas_openai_agents import GaaSGovernanceConfig

config = GaaSGovernanceConfig(
    api_url="https://api.gaas.is",    # GaaS API endpoint
    api_key="gsk_...",                # Your API key
    agent_id="my-agent",              # Agent identifier (appears in audit trail)
    block_on_escalate=True,           # Raise GovernanceBlockedError on ESCALATE
    timeout_seconds=240.0,             # Covers a deliberated decision (30-60 s)
    sensitivity="INTERNAL",           # Default sensitivity level for tool inputs
    fail_open=False,                  # No decision from GaaS → block the tool (default)
    raise_on_governance_error=False,  # True: re-raise the underlying error instead
    extra_regulatory_domains=["HIPAA"],   # Additional domains for all intents
    extra_data_categories=["PHI"],        # Additional data categories for all intents
    hold_on_escalate=False,           # Wait for the human decision on ESCALATE
    escalation_poll_seconds=5.0,      # Poll interval while holding
    escalation_max_wait_seconds=600.0,  # Give up (treat as timeout) after this
)
```

### Hold-and-Poll on ESCALATE

With `hold_on_escalate=True`, an ESCALATE verdict no longer raises immediately.
The tool call holds while GaaS routes the escalation to a human reviewer, polling
the escalation status until it is decided:

- **approve / modify** — the tool executes.
- **deny** — raises `GovernanceBlockedError` with verdict `ESCALATE_DENY`.
- **timeout** — raises `GovernanceBlockedError` with verdict `ESCALATE_TIMEOUT`.

`hold_on_escalate` takes precedence over `block_on_escalate`. Transient polling
errors are retried until the deadline.

## Handling blocked runs

```python
from gaas_openai_agents import GovernanceBlockedError

try:
    result = await Runner.run(agent, "wire $250k to the new vendor")
except GovernanceBlockedError as err:
    print(err.verdict)                  # BLOCK / ESCALATE / ESCALATE_DENY / ESCALATE_TIMEOUT / UNEVALUATED
    print(err.reason)                   # UNEVALUATED only, e.g. "HTTP 401", "timeout"
    print(err.decision_id)              # audit reference
    print(err.blocking_policies)        # policy IDs that triggered the block
    print(err.governance_proof_token)   # GPT token ID for the legal audit trail
```

`GovernanceBlockedError` subclasses `agents.exceptions.AgentsException`, so the
openai-agents runner propagates it unchanged.

## Notes

- Only `FunctionTool` instances (e.g. from `@function_tool`) can be governed.
  Hosted tools (web search, code interpreter) execute on OpenAI's side and are
  out of client-side governance reach.
- Tool metadata (name, description, JSON schema, guardrails, approval config)
  is preserved by wrapping.
- Fail-closed by default: if GaaS gives no decision the tool does not run. Set
  `fail_open=True` to run it anyway (see below).

## When GaaS can't answer

If GaaS gives no decision — a network error, a timeout, any HTTP status of 400
or above (a wrong API key is a 401), or a response without a verdict — the
adapter **fails closed**: the tool does not run, and `GovernanceBlockedError`
is raised with `verdict == "UNEVALUATED"` and a short `reason` such as
`"HTTP 401"` or `"timeout"`. It halts `Runner.run()` exactly as a BLOCK does. The API key never appears in the
message.

Three settings, checked in this order:

| Setting | When GaaS gives no decision |
|---|---|
| `raise_on_governance_error=True` | The underlying error is re-raised (e.g. `httpx.HTTPStatusError`, `httpx.ReadTimeout`). |
| `fail_open=True` | The tool runs anyway, ungoverned, and a WARNING is logged on the `gaas_openai_agents` logger. |
| neither (default) | The tool does not run; `GovernanceBlockedError` with verdict `UNEVALUATED`. |

```python
config = GaaSGovernanceConfig(
    api_key="gsk_...",
    fail_open=True,  # keep the agent working when GaaS is unreachable (actions run ungoverned)
)
```

Up to 0.1.2 the adapter failed open on any error, including 401, 403,
422 and 429, so a wrong API key let every action run ungoverned. gaas-openai-agents
0.2.0 made failing closed the default.

## Links

- Docs: https://gaas.to/sdks.html
- GaaS: https://gaas.is
