ValGuard + Vercel AI SDK: validated streams in Next.js

Use createOpenAI with ValGuard base URL and X-VG-Agent. Block and re-ask rules buffer the stream before the UI sees tokens.

Last verified:

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

  1. Create a ValGuard agent for the route.
  2. Attach packs; shadow mode on staging.
  3. Set baseURL to https://api.valguard.ai/v1 and send X-VG-Agent on each request.
  4. Map X-VG-Validation-Status in 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.

Related

Next step

Quickstart and API integration patterns.