Developer guide

How to Redact PII Before Sending Data to OpenAI (Node.js)

OpenAI requests can expose PII if you pass raw user input through unchanged. Emails, names, and phone numbers should be sanitized locally before API calls—then send only the cleaned text onward.

The Problem

Example: sending raw input to OpenAI

const userInput = "Contact me at john@email.com";
await openai.chat.completions.create({
  model: "gpt-4.1-mini",
  messages: [{ role: "user", content: userInput }],
});

This sends raw PII to an external API—and often into vendor logs.

The Solution

Run OpenRedaction in your process first. Call detect(), pass result.redacted to OpenAI, and keep any redactionMap out of vendor traffic.

For gateways, RAG, citations, and telemetry layers, read the PII redaction guide.

Install OpenRedaction

Install the library:

npm install openredaction openai

Redact before sending

detect() is async. Reuse one OpenRedaction instance across requests.

import OpenAI from "openai";
import { OpenRedaction } from "openredaction";

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const redactor = new OpenRedaction({
  redactionMode: "placeholder",
  deterministic: true,
});

async function safeCompletion(userInput: string) {
  const { redacted } = await redactor.detect(userInput);

  return openai.chat.completions.create({
    model: "gpt-4.1-mini",
    messages: [{ role: "user", content: redacted }],
  });
}

await safeCompletion("Contact me at john@email.com");

Example output

Input
Email me at jane@company.com and call 555-123-4567
Sent to OpenAI
Email me at [EMAIL_…] and call [PHONE_…]

Why this matters

  • Avoid sending raw user identifiers to external APIs
  • Reduce compliance risk (GDPR, CCPA, sector rules)
  • Keep prompts, vendor logs, and local traces cleaner
  • Retain control over sensitive data in your process

Where to use this

  • Before OpenAI (or any LLM) API calls
  • Before logging user input or tool payloads
  • Before storing prompts, embeddings, or responses
  • At Express/gateway ingress — see Node.js redaction

Regex vs AI detection

Regex (plus validators) is fast and predictable for structured identifiers—emails, phones, cards, national IDs. NER or ML can help with messy free-text names. Most production stacks run local pattern redaction first, then optional NER. Details in the implementation section.

Use it locally in your app—then harden the rest of the pipeline.