Skip to content

Vercel AI SDK

Use proxshield inside your app as AI SDK middleware, with no proxy to run.

If your app uses the AI SDK, you can skip the proxy and run proxshield in-process. It works with every provider the AI SDK supports.

Terminal window
npm install proxshield
import { openai } from "@ai-sdk/openai";
import { generateText, wrapLanguageModel } from "ai";
import { proxshieldMiddleware } from "proxshield/ai-sdk";
const model = wrapLanguageModel({
model: openai("gpt-5"),
middleware: proxshieldMiddleware(),
});
const { text } = await generateText({
model,
prompt: "Draft a reply to Jordan Lee (jordan@acme.com) about the late refund.",
});
// The model saw [PERSON_1] and [EMAIL_1]; `text` has the real values back.

Streaming (streamText), tool calls and tool results work the same way. Text deltas, tool-input deltas and final tool-call inputs are restored.

If a prompt or tool result is an injection, the call throws before the model is called:

import { ProxshieldBlockedError } from "proxshield/ai-sdk";
try {
await generateText({ model, prompt });
} catch (e) {
if (e instanceof ProxshieldBlockedError) console.warn(e.message);
else throw e;
}

Each proxshieldMiddleware() call loads its own copy of the models. To wrap several models, build one guard and share it:

import { createGuard, defaultConfig } from "proxshield";
import { proxshieldMiddleware } from "proxshield/ai-sdk";
const guard = createGuard(defaultConfig({ piiModel: "privacy-filter" }));
await guard.warmup(); // optional: load the models now instead of on the first call
const middleware = proxshieldMiddleware({ guard });

The middleware targets the AI SDK’s v4 language model specification (AI SDK 7).