Tawa docs
Cookbook

Raw OpenAI (no framework)

Screen a prompt before an OpenAI call, without the OpenAI Agents SDK.

If you're calling the OpenAI client directly — no Agents SDK — screen the prompt yourself before the call. If Tawa blocks or leans toward blocking, the request is rejected without ever reaching OpenAI.

import os
from openai import OpenAI
from tawaguard import TawaGuard

openai = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tawa = TawaGuard(os.environ["TAWA_API_KEY"])

_BLOCKED_ACTIONS = {"block", "hard_block", "soft_block"}


def chat(user_message: str):
    scan = tawa.analyze(user_message)

    if scan.action in _BLOCKED_ACTIONS or scan.suggestion == "block":
        raise ValueError("Message blocked by content policy.")

    return openai.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": user_message}],
    )

Building a Python agent with the OpenAI Agents SDK? The OpenAI Agents SDK integration wraps this pattern for you — no manual check needed at every call site.