ZeroLeaks
Shield SDKProvider Wrappers

OpenAI Provider

Wrap your OpenAI client with Shield protection for automatic prompt hardening, injection detection, and output sanitization.

OpenAI Provider

The shieldOpenAI wrapper adds transparent security to your existing OpenAI client. It intercepts every chat.completions.create call to harden system prompts, detect injections in user messages, and sanitize leaked content from responses.

Usage

import OpenAI from "openai";
import { shieldOpenAI } from "@zeroleaks/shield/openai";

const client = shieldOpenAI(new OpenAI(), {
  systemPrompt: "You are a financial advisor...",
  onDetection: "block",
});

const response = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "You are a financial advisor..." },
    { role: "user", content: userInput },
  ],
});

How It Works

On every call to chat.completions.create, Shield:

  1. Clones the messages array (never mutates your original objects)
  2. Hardens any system message with security rules (unless harden: false)
  3. Scans every user message for injection patterns (unless detect: false)
  4. Calls the original OpenAI API
  5. Sanitizes the response text for leaked system prompt fragments (unless sanitize: false)

Options

OptionTypeDefaultDescription
systemPromptstringThe system prompt to protect (used for output sanitization)
hardenHardenOptions | false{}Hardening options, or false to disable
detectDetectOptions | false{}Detection options, or false to disable
sanitizeSanitizeOptions | false{}Sanitization options, or false to disable
streamingSanitize"buffer" | "chunked" | "passthrough""buffer""buffer": full buffer then sanitize. "chunked": 8KB chunks, lower memory. "passthrough": skip sanitization.
streamingChunkSizenumber8192Chunk size for "chunked" mode
throwOnLeakbooleanfalseWhen true, throw LeakDetectedError instead of redacting leaked content
onDetection"block" | "warn""block""block" throws an error, "warn" calls the callback only
onInjectionDetected(result) => voidCallback when injection is detected
onLeakDetected(result) => voidCallback when output leak is detected

Blocking vs Warning

By default, onDetection is "block" -- if an injection is detected, Shield throws an Error with details about the risk level and matched categories. To log instead of blocking:

const client = shieldOpenAI(new OpenAI(), {
  onDetection: "warn",
  onInjectionDetected: (result) => {
    console.warn(`Injection detected: ${result.risk}`, result.matches);
  },
});

On this page