open-guardrail-nextjs
v2.5.0
Published
Next.js App Router adapter for open-guardrail — 215+ guards for API routes, prompt injection, PII, toxicity & more
Maintainers
Readme
open-guardrail-nextjs
Next.js App Router adapter for open-guardrail.
Install
pnpm add open-guardrail open-guardrail-nextjsUsage — App Router
createRouteGuard
Wrap an App Router route handler:
// app/api/chat/route.ts
import { pipe, promptInjection } from 'open-guardrail';
import { createRouteGuard } from 'open-guardrail-nextjs';
const guard = createRouteGuard({
input: pipe(promptInjection({ action: 'block' })),
});
export const POST = guard(async (request) => {
const { message } = await request.json();
const reply = await getAIResponse(message);
return Response.json({ reply });
});guardApiRoute
Lighter helper — returns parsed body or 403:
// app/api/chat/route.ts
import { pipe, toxicity } from 'open-guardrail';
import { guardApiRoute } from 'open-guardrail-nextjs';
const guard = guardApiRoute({
input: pipe(toxicity({ action: 'block' })),
});
export async function POST(request: Request) {
const checked = await guard(request);
if (checked instanceof Response) return checked;
const { body } = checked;
return Response.json({ reply: body.message });
}guardResponse
Guard LLM output before returning:
import { pipe, pii } from 'open-guardrail';
import { guardResponse } from 'open-guardrail-nextjs';
const outputPipeline = pipe(pii({ entities: ['email'], action: 'mask' }));
const safeText = await guardResponse(outputPipeline, llmOutput);License
MIT
