RedaktAIDocs

OpenAI

Using Rdakt AI with OpenAI's API

OpenAI's Python SDK uses httpx internally, making it fully compatible with Rdakt AI's transport middleware.

Setup

import httpx
from openai import AsyncOpenAI
from rdakt_ai import RdaktMiddleware

client = AsyncOpenAI(
    http_client=httpx.AsyncClient(
        transport=RdaktMiddleware(inner=httpx.AsyncHTTPTransport()),
    ),
)

Supported Models

All OpenAI chat models work with Rdakt AI: GPT-4o, GPT-4o Mini, GPT-4 Turbo, GPT-3.5 Turbo, and any future chat completion models.

Message Format

OpenAI uses the messages[].content format:

{
  "model": "gpt-4o",
  "messages": [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Tell me about John Smith"}
  ]
}

Rdakt AI scans the content field of each message for PII.

Streaming

OpenAI streaming responses use SSE with this format:

data: {"choices":[{"delta":{"content":"Hello"},"index":0}]}

data: {"choices":[{"delta":{"content":" world"},"index":0}]}

data: [DONE]

Rdakt AI extracts content from choices[0].delta.content and deanonymizes tokens in real-time.

Streaming Example

stream = await client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Write about Jane Doe (jane@acme.com)"}
    ],
    stream=True,
)

async for chunk in stream:
    content = chunk.choices[0].delta.content
    if content:
        print(content, end="", flush=True)

Non-Streaming Example

response = await client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "user", "content": "Summarize the case for John Smith (SSN 123-45-6789)"}
    ],
)
print(response.choices[0].message.content)

Sync Client

import httpx
from openai import OpenAI
from rdakt_ai import RdaktSyncMiddleware

client = OpenAI(
    http_client=httpx.Client(
        transport=RdaktSyncMiddleware(inner=httpx.HTTPTransport()),
    ),
)

On this page