Quickstart: OpenAI
Anonymize OpenAI requests in under 10 lines
Prerequisites
rdakt-aiandopenaiinstalledOPENAI_API_KEYenvironment variable set
Async Usage
import httpx
from openai import AsyncOpenAI
from rdakt_ai import RdaktMiddleware
client = AsyncOpenAI(
http_client=httpx.AsyncClient(
transport=RdaktMiddleware(inner=httpx.AsyncHTTPTransport()),
),
)
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 Usage
import httpx
from openai import OpenAI
from rdakt_ai import RdaktSyncMiddleware
client = OpenAI(
http_client=httpx.Client(
transport=RdaktSyncMiddleware(inner=httpx.HTTPTransport()),
),
)
response = 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)Streaming
Streaming works automatically. Tokens are deanonymized as they arrive:
stream = await client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "Write a report about Jane Doe, email jane@acme.com"}
],
stream=True,
)
async for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)The LLM sees anonymized values like <PERSON_1> and <EMAIL_1>. Rdakt AI replaces them back to "Jane Doe" and "jane@acme.com" in the streamed response before your code sees it.
What Just Happened?
- Detection — Rdakt AI scanned the outbound message and found "John Smith" (PERSON), "123-45-6789" (SSN)
- Anonymization — The message sent to OpenAI contained
<PERSON_1>and<SSN_1>instead of real values - Deanonymization — The streamed response had tokens replaced back to original values in real-time