← SDK docs

VENZX + OpenAI Agents SDK

Put a check in front of every function tool an agent calls. Allowed calls run, blocked calls do not, and calls that need a person wait for one.

Tested with openai-agents 0.20.0. The code on this page is the file our tests run.

5-minute quick start

  1. 1.Install

    pip install venzx openai-agents
  2. 2.Set your keys

    Create a VENZX key in your dashboard. The example uses an OpenAI model, so it needs that key too.

    export VENZX_API_KEY="pai-..."
    export OPENAI_API_KEY="sk-..."
  3. 3.Save agent.py

    Save this as agent.py.
    """OpenAI Agents SDK + VENZX: check every tool call before the tool runs.
    
    The integration point is a tool input guardrail. The SDK runs it after the
    model has asked for a tool and before the tool function is called, with the
    tool's name and its arguments. The guardrail asks VENZX:
    
        allow         -> the tool runs
        block         -> the tool does not run; the model gets the reason instead
        needs_review  -> the tool does not run; the model is told it is held for a person
                         (or, with APPROVAL_WAIT_SECONDS set, wait for them and then run)
    
    Tested with openai-agents 0.20.0.
    
    Run it:
        pip install venzx openai-agents
        export VENZX_API_KEY="pai-..."  OPENAI_API_KEY="sk-..."
        python agent.py
    """
    from __future__ import annotations
    
    import asyncio
    import json
    import os
    
    from agents import (
        Agent,
        Runner,
        ToolGuardrailFunctionOutput,
        ToolInputGuardrailData,
        function_tool,
        tool_input_guardrail,
    )
    
    from venzx import ApprovalRequired, Blocked, Guard, Policy, Venzx
    
    # What this agent may do. Sent with every check, so it applies even if the
    # account's own policy is empty.
    POLICY = (
        Policy()
        .allow_tools("lookup_order", "refund")     # anything else is blocked
        .require_approval("refund")                # a person must say yes
    )
    
    # Stand-in for your systems, so you can see what actually ran.
    ACTIONS: list = []
    
    
    def lookup_order(order_id: str) -> str:
        """Look up the status of an order."""
        ACTIONS.append(("lookup_order", order_id))
        return f"Order {order_id}: delivered, total $40.00"
    
    
    def refund(order_id: str, amount: float) -> str:
        """Refund an order."""
        ACTIONS.append(("refund", order_id, amount))
        return f"Refunded ${amount:.2f} on order {order_id}"
    
    
    def delete_customer(customer_id: str) -> str:
        """Delete a customer and all their data."""
        ACTIONS.append(("delete_customer", customer_id))
        return f"Deleted customer {customer_id}"
    
    
    # ── the VENZX check (identical in every framework example) ───────────────
    # Seconds to wait for a person when an action is held. 0 (the default) does
    # not wait: the model is told the action is held and carries on.
    APPROVAL_WAIT_SECONDS = float(os.environ.get("APPROVAL_WAIT_SECONDS", "0"))
    
    
    def venzx_refusal(guard: Guard, name: str, args: dict,
                      wait_seconds: float = APPROVAL_WAIT_SECONDS) -> str | None:
        """Ask VENZX about one tool call, before it runs.
    
        Returns None if the tool may run. Otherwise returns the sentence to give
        the model in place of the tool's result. Never raises: anything that goes
        wrong is a refusal, so a tool cannot run unchecked."""
        try:
            guard.tool_call(name, args)
            return None                                   # allowed
        except ApprovalRequired as e:
            approval_id = e.result.approval_id            # held for a person
        except Blocked as e:
            return f"BLOCKED: {e.result.reason}. It has NOT run."
        except Exception as e:  # noqa: BLE001
            return f"BLOCKED: the action could not be checked ({e}). It has NOT run."
    
        status = "pending"
        if wait_seconds > 0:
            try:
                status = guard.client.wait_for_approval(
                    approval_id, timeout=wait_seconds).get("status", "pending")
                # Reading "approved" is not permission. Claiming it is, and the
                # claim names this exact call, so it cannot be spent on another.
                guard.client.consume_approval(
                    approval_id, action={"name": name, "args": dict(args)})
                return None                               # a person said yes
            except Exception:  # noqa: BLE001
                pass                                      # rejected, expired or still pending
        if status == "rejected":
            return f"REJECTED: a person declined '{name}'. It has NOT run."
        return (f"HELD: '{name}' needs a person to approve it "
                f"(approval id {approval_id}). It has NOT run.")
    # ── end of the VENZX check ───────────────────────────────────────────────
    
    
    def venzx_guardrail(guard: Guard):
        """A tool input guardrail that checks the call with VENZX."""
    
        @tool_input_guardrail
        async def venzx_check(data: ToolInputGuardrailData) -> ToolGuardrailFunctionOutput:
            name = data.context.tool_name
            args = json.loads(data.context.tool_arguments or "{}")
            # The venzx client is synchronous; keep it off the event loop.
            refusal = await asyncio.to_thread(venzx_refusal, guard, name, args)
            if refusal is None:
                return ToolGuardrailFunctionOutput.allow()            # run the tool
            return ToolGuardrailFunctionOutput.reject_content(refusal)
    
        return venzx_check
    
    
    def build_agent(model=None, guard: Guard | None = None) -> Agent:
        """An agent whose every tool goes through the VENZX guardrail.
    
        ``model`` is optional: leave it out to use the SDK's default model."""
        check = venzx_guardrail(guard or Guard(Venzx(), policy=POLICY))
        tools = [function_tool(fn, tool_input_guardrails=[check])
                 for fn in (lookup_order, refund, delete_customer)]
        kwargs = {"model": model} if model is not None else {}
        return Agent(
            name="Support agent",
            instructions="You help customers with their orders. Use the tools.",
            tools=tools,
            **kwargs,
        )
    
    
    def main() -> None:
        result = Runner.run_sync(
            build_agent(),
            "Order A-1001 arrived broken. Look it up, refund it in full, "
            "then delete customer C-77.")
        print(result.final_output)
        print("\nWhat actually ran:", ACTIONS)
    
    
    if __name__ == "__main__":
        main()
    
  4. 4.Run it

    python agent.py

    The example is a support agent with three tools. Its policy lets lookup_order run, holds refund for a person, and has no entry for delete_customer.

    The agent’s final answer varies from run to run. The last line is what matters: it lists what actually ran, and it should hold lookup_order only.

    The refund did not run. It is waiting as an approval email at the address on your VENZX account. delete_customer did not run and never will under this policy.

  5. 5.Approve the refund

    Open the approval email and press Approve. By default the example does not wait for you: it tells the model the refund is held and moves on. To have the tool call wait for your decision and then run, set a wait before you start it:

    APPROVAL_WAIT_SECONDS=600 python agent.py

How it plugs in

The integration point is function_tool(fn, tool_input_guardrails=[...]), which OpenAI Agents SDK provides for exactly this.

The Agents SDK runs a tool’s input guardrails after the model has asked for the tool and before the tool function is called. The guardrail receives the tool’s name and its arguments.

The example’s guardrail asks VENZX and returns ToolGuardrailFunctionOutput.allow() to let the tool run, or .reject_content(reason) to skip it. It is attached to every tool in one loop in build_agent.

When a call is refused. reject_content makes the reason the tool’s output. The model reads it and the run continues. To stop the whole run instead, return ToolGuardrailFunctionOutput.raise_exception(), which raises ToolInputGuardrailTripwireTriggered.

The check itself

venzx_refusal in the file above is the same in every framework example. It returns nothing when the tool may run, and otherwise the sentence to give the model:

  • Allowed: the tool runs.
  • Blocked: the tool does not run, and the model is told why.
  • Held for a person: the tool does not run. With APPROVAL_WAIT_SECONDS set, it waits for the decision and then claims the approval for that exact tool and those exact arguments before running. An approval cannot be spent on a different call.
  • Anything goes wrong, including VENZX being unreachable: the tool does not run.

The policy at the top of the file travels with every check. Change it there, or delete it to use the policy saved on your account. See Policy and the human-approval gate.

What this does not cover

  • A guardrail covers the tools it is attached to. A function tool created without it is not checked, so build your tools in one place, as the example does.
  • Hosted tools, such as web search, run on OpenAI’s side and never reach your process. This guardrail does not see them.
  • The check runs in your process. Code that calls a tool function directly, without the Runner, bypasses it.

VENZX answers before your tool runs and never sees it execute. The general limits are in What VENZX does NOT do.

Other frameworks

LangGraph · CrewAI · MCP · SDK docs

The example, its pinned requirements.txt and its test are in examples/openai-agents/ of the SDK repository.