Skip to content

AI data loss prevention for Python + FastAPI

People often paste personal data into AI prompts – card numbers, phone numbers, home addresses, and whole résumés. Once that data reaches your AI provider, the provider’s logging and retention apply to it, not yours.

Arcjet sensitive info detection scans prompt content inside your application, before it reaches the AI provider. Detection runs locally in your own environment, so the raw text doesn’t leave your app. The SDK reports the decision to Arcjet with the type and position of each finding, but not the matched text. When Arcjet finds something, you decide what happens next: block the request, remove the data, or ask the user to edit their prompt.

In this example we use LangChain to create a simple AI chat server with FastAPI, and Arcjet to prevent sensitive information from being sent to the AI model. The same principles can be applied to any AI application, including those built with other frameworks.

Set up the environment and install dependencies (uses uv, but you can also use pip to install the Arcjet Python SDK):

Terminal window
# Export your Arcjet API key from https://console.arcjet.com
export ARCJET_KEY="ajkey_..."
export ARCJET_ENV=development
# Export your OpenAI API key (used by LangChain)
export OPENAI_API_KEY="sk-..."
# Install dependencies
uv add "arcjet[sensitive-info-rampart]" fastapi uvicorn langchain langchain-openai

Create the chat server:

main.py
import logging
import os
from arcjet import Mode, SensitiveInfoEntityType, arcjet, detect_sensitive_info
from arcjet_sensitive_info_rampart import rampart
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel
app = FastAPI()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
arcjet_key = os.getenv("ARCJET_KEY")
if not arcjet_key:
raise RuntimeError("ARCJET_KEY is required. Get one at https://console.arcjet.com")
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
raise RuntimeError(
"OPENAI_API_KEY is required. Get one at https://platform.openai.com"
)
llm = ChatOpenAI(model="gpt-4o-mini", api_key=openai_api_key)
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
("human", "{message}"),
]
)
chain = prompt | llm | StrOutputParser()
class ChatRequest(BaseModel):
message: str
# Create a single Arcjet client at startup and reuse it across requests
aj = arcjet(
key=arcjet_key, # Get your key from https://console.arcjet.com
rules=[
detect_sensitive_info(
mode=Mode.LIVE, # Blocks requests. Use Mode.DRY_RUN to log only
# Block PII types that should never appear in AI prompts.
# Remove types your app legitimately handles (for example, EMAIL for a
# support bot).
deny=[
SensitiveInfoEntityType.CREDIT_CARD_NUMBER,
SensitiveInfoEntityType.EMAIL,
],
backend=rampart(),
),
],
)
@app.post("/chat")
async def chat(request: Request, body: ChatRequest):
# Scan the user message for sensitive information before it reaches the
# AI model. Pass the full conversation if you want to scan all messages.
decision = await aj.protect(request, sensitive_info_value=body.message)
if decision.is_denied():
if decision.reason_v2.type == "SENSITIVE_INFO":
logger.warning("Request blocked due to sensitive information")
return JSONResponse(
{"error": "Sensitive information detected – remove it from your prompt"},
status_code=400,
)
# Block other denials too, such as one from a remote rule.
return JSONResponse({"error": "Forbidden"}, status_code=403)
# Fail closed when Arcjet couldn't complete the check.
if decision.is_error():
logger.warning("Arcjet error")
return JSONResponse({"error": "Service unavailable"}, status_code=503)
# Arcjet approved – call the AI model
reply = await chain.ainvoke({"message": body.message})
return {"reply": reply}

Then run the server:

Terminal window
uv run uvicorn main:app --reload

And send a message to the API endpoint:

Terminal window
curl -X POST http://localhost:8000/chat \
-H "Content-Type: application/json" \
-d '{"message": "My email is test@example.com"}'

Requests appear in your Arcjet Console in real time.

Sensitive info detection runs on-device with Rampart, a named-entity-recognition (NER) model paired with deterministic recognizers:

  • Deterministic recognizers match structured types with validated patterns – card numbers, email addresses, IP addresses, URLs, and Social Security numbers.
  • The NER model detects the free-form PII that people paste into prompts – names, street addresses, phone numbers, and government or financial identifiers. These don’t follow a fixed pattern, so a model finds them where pattern matching can’t.

The SDK rule runs detection on your own infrastructure, so the text isn’t sent to Arcjet or a third party for analysis. That makes it a good fit in front of an AI provider. The server-side sensitive information detector in an agent guard policy and coding agent hooks screen text on Arcjet’s side instead. Use the server-side detector for text longer than the SDK scans, and to detect protected health information (PHI) and secrets such as API keys, which on-device detection doesn’t cover.

Use deny to list the entity types to block, or allow to block everything except the types you list (the two are mutually exclusive). Tune the list to your app – for a support bot that legitimately collects phone numbers, leave PHONE_NUMBER out of deny:

detect_sensitive_info(
mode=Mode.LIVE, # Blocks requests. Use Mode.DRY_RUN to log only
deny=[
SensitiveInfoEntityType.CREDIT_CARD_NUMBER,
SensitiveInfoEntityType.EMAIL,
],
backend=rampart(),
)

For every entity type and where Arcjet detects it, see Entity types.

AI prompts are full of free-form PII – a pasted résumé, a shipping address, and someone’s full name. The Rampart NER model detects these, plus government and financial identifiers (SSNs, tax IDs, passports, driver’s licenses, bank and routing numbers). Add them to deny:

import os
from arcjet import Mode, SensitiveInfoEntityType, arcjet, detect_sensitive_info
from arcjet_sensitive_info_rampart import rampart
aj = arcjet(
key=os.environ["ARCJET_KEY"],
rules=[
detect_sensitive_info(
mode=Mode.LIVE,
# Names and addresses are common in pasted résumés and messages.
deny=[
SensitiveInfoEntityType.CREDIT_CARD_NUMBER,
SensitiveInfoEntityType.EMAIL,
SensitiveInfoEntityType.GIVEN_NAME,
SensitiveInfoEntityType.SURNAME,
SensitiveInfoEntityType.STREET_NAME,
],
backend=rampart(),
),
],
)

The model ships with the package (about 14.7 MB, quantized), so nothing is fetched at runtime, and inference is fast enough to run inline on each request. It recalls about 98% of private terms across seven Latin-script languages. It loads a native ONNX runtime (ONNX Runtime), which the extra installs for you. For the options and the full accuracy and latency breakdown, see the Rampart reference.

Pass the text to scan as sensitiveInfoValue in JavaScript, sensitive_info_value in Python, or WithSensitiveInfoValue in Go. For a chat endpoint, this is usually the user’s most recent message. To scan every message, pass the full conversation history instead – PII can appear earlier in the exchange.

Set mode to "DRY_RUN" in JavaScript, Mode.DRY_RUN in Python, or ModeDryRun in Go to log detections without blocking requests. Run it in production to see which PII appears in your prompts, then switch to live mode when the entity list matches what you want to block.

Sensitive info detection controls what data reaches your AI provider. Pair it with the other AI protection layers, each of which covers a different risk: