The Vercel AI SDK talks to OpenAI-compatible providers. Aim createOpenAI at ValGuard so Next.js routes, server actions, and streaming UIs inherit the same validators as your Python services.
When you need this
A support bot streams a refund promise before policy checks finish. With block or re-ask enabled, ValGuard buffers the completion — the UI should expect later first tokens. See Trust.
Setup
- Create a ValGuard agent for the route.
- Attach packs; shadow mode on staging.
- Set
baseURLtohttps://api.valguard.ai/v1and sendX-VG-Agenton each request. - Map
X-VG-Validation-Statusin your route handler for UX fallbacks.
Code
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";
const vg = createOpenAI({
baseURL: "https://api.valguard.ai/v1",
apiKey: process.env.VG_API_KEY,
headers: { "X-VG-Agent": "support-stream" },
});
export async function POST(req: Request) {
const { messages } = await req.json();
const result = streamText({
model: vg("openai/gpt-4o-mini"),
messages,
});
return result.toDataStreamResponse();
}
Honest limits
- Client-side-only validation is not a substitute for the proxy when you need a centralized audit trail.
- Edge middleware cannot replace server-side routing through ValGuard for protected keys.
- Streaming UX differs when rules buffer output.