OGuardAI
Guides

SDK Guide

Recommended integration paths for Python and TypeScript applications

Decision Table

What are you building?Recommended pathComplexity
Chatbot (OpenAI, Anthropic, etc.)OpenAI-compatible proxyLowest
RAG pipeline4-step API integrationMedium
API backend (Python)FastAPI middlewareLow
API backend (Node.js)Express middlewareLow
LangChain agentLangChain adapterLow
Vercel AI appVercel AI SDK adapterLow
Custom pipeline or non-HTTPDirect HTTP APIMedium

Path 1: OpenAI-Compatible Proxy (Chatbots)

Point your OpenAI client at OGuardAI's proxy endpoint. Input is automatically protected before reaching the LLM, and output is automatically restored.

import openai

client = openai.OpenAI(
    base_url="http://localhost:8081/v1",  # OGuardAI proxy (separate from main server)
    api_key="your-openai-key",
)
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Email john@acme.com about the invoice."}],
)
print(response.choices[0].message.content)  # Original PII restored in output

Full guide: OpenAI Proxy Guide

Path 2: RAG Pipeline (4-Step API)

Protect documents at ingestion time, protect queries at search time.

import httpx

# Step 1: Protect document before indexing
resp = httpx.post("http://localhost:3000/v1/transform", json={"input": doc_text})
safe_doc = resp.json()["safe_text"]
index.upsert(safe_doc, metadata={"session_state": resp.json()["session_state"]})

# Step 2: Protect user query
resp = httpx.post("http://localhost:3000/v1/transform", json={"input": user_query})
safe_query = resp.json()["safe_text"]

# Step 3: Search with protected query, build context from protected docs
results = index.search(safe_query, top_k=5)

# Step 4: Restore LLM answer
llm_answer = call_llm(safe_query, results)
resp = httpx.post("http://localhost:3000/v1/rehydrate", json={
    "output": llm_answer, "session_state": session_state
})
print(resp.json()["restored_text"])

Path 3: FastAPI Middleware (Python Backend)

from fastapi import FastAPI
from guardai_sdk import OGuardAIClient

app = FastAPI()
guardai = OGuardAIClient(base_url="http://localhost:3000")

@app.post("/chat")
async def chat(message: str):
    result = guardai.transform(message)
    llm_reply = call_llm(result.safe_text)
    restored = guardai.rehydrate(llm_reply, session_state=result.session_state)
    return {"reply": restored.restored_text}

Path 4: Express Middleware (Node.js Backend)

import express from "express";
import { OGuardAIClient } from "@oguardai/sdk";

const app = express();
const guardai = new OGuardAIClient({ baseUrl: "http://localhost:3000" });

app.post("/chat", async (req, res) => {
  const result = await guardai.transform(req.body.message);
  const reply = await callLlm(result.safeText);
  const restored = await guardai.rehydrate(reply, result.sessionState);
  res.json({ reply: restored.restoredText });
});

Path 5: Direct HTTP API (Custom Pipelines)

Use when no SDK or adapter fits. Two calls: transform before the LLM, rehydrate after.

# Transform
curl -X POST http://localhost:3000/v1/transform \
  -H "Content-Type: application/json" \
  -d '{"input": "Contact jane@example.com or call 555-0123."}'

# Rehydrate (pass session_state from transform response)
curl -X POST http://localhost:3000/v1/rehydrate \
  -H "Content-Type: application/json" \
  -d '{"output": "I emailed `{{email:a1b2}}` and called `{{phone:c3d4}}`.",
       "session_state": "<blob from transform>"}'

What NOT to Do

Do not store raw PII alongside tokenized text. The session blob is the only link between tokens and original values. If you store raw PII in your database "just in case," you defeat the purpose of tokenization.

Do not parse or modify the session state blob. It is AES-256-GCM encrypted and authenticated. Any modification will cause unseal to fail. Treat it as an opaque binary blob.

Do not skip rehydrate and regex-replace tokens yourself. Token IDs are scoped to a session. {{email:a1b2}} in one session is a completely different value than {{email:a1b2}} in another. Only the rehydrate endpoint with the correct session blob can restore values.

Do not call the LLM with raw text and then try to protect the output. OGuardAI protects input before the LLM sees it. The output guard is a safety net, not the primary protection mechanism.

Do not set detector.mode: builtin and expect person names to be detected. Person, company, and location detection requires the NER sidecar (detector.mode: both or detector.mode: advanced). Builtin mode only covers pattern-based entity types (email, phone, SSN, IBAN, etc.).

Do not use auth.mode: dev in production. Dev mode disables authentication. Always set a real auth mode and API keys for production.