Skip to content

Vercel AI SDK

Point the Vercel AI SDK OpenAI provider at Promptix with createOpenAI({ baseURL }).

The Vercel AI SDK talks to Promptix through its OpenAI provider. Create a provider instance with the Promptix baseURL, then use generateText, streamText and the other helpers as usual.

Install

npm install ai @ai-sdk/openai zod

zod is only needed for structured output.

Create the provider

Use createOpenAI with the Promptix base URL and your key, in a module that only runs on the server.

lib/promptix.tsTypeScript
import { createOpenAI } from '@ai-sdk/openai'; export const promptix = createOpenAI({  baseURL: 'https://promptix.tn/api/v1',  apiKey: process.env.PROMPTIX_API_KEY,});

Use promptix.chat(modelId)

Recent versions of @ai-sdk/openai send promptix(modelId) to the OpenAI Responses API (/responses), which Promptix does not provide. Always select models with promptix.chat('provider/model') so requests go to /chat/completions.

Generate text

generate.tsTypeScript
import { generateText } from 'ai';import { promptix } from '@/lib/promptix'; const { text, usage } = await generateText({  model: promptix.chat('anthropic/claude-sonnet-4'),  system: 'You answer in French.',  prompt: 'Write a two-line welcome message for a coworking space in Tunis.',  maxRetries: 2,}); console.log(text);console.log(usage);

Stream text

stream.tsTypeScript
import { streamText } from 'ai';import { promptix } from '@/lib/promptix'; const result = streamText({  model: promptix.chat('openai/gpt-4o-mini'),  prompt: 'Describe the medina of Tunis in one paragraph.',}); for await (const text of result.textStream) {  process.stdout.write(text);}

In a Next.js route handler, return the stream directly. Use toTextStreamResponse() for a plain text stream; if your front end uses useChat, return the UI message stream response of your SDK version instead.

app/api/completion/route.tsTypeScript
// app/api/completion/route.tsimport { streamText } from 'ai';import { promptix } from '@/lib/promptix'; export const maxDuration = 60; export async function POST(req: Request) {  const { prompt } = await req.json();   const result = streamText({    model: promptix.chat('openai/gpt-4o-mini'),    prompt: String(prompt),    maxOutputTokens: 800,  });   return result.toTextStreamResponse();}

Structured output

generateObject validates the model's answer against a Zod schema. Choose a model that supports JSON output (see Choosing a model).

extract.tsTypeScript
import { generateObject } from 'ai';import { z } from 'zod';import { promptix } from '@/lib/promptix'; const { object } = await generateObject({  model: promptix.chat('openai/gpt-4o-mini'),  schema: z.object({    city: z.string(),    amountTnd: z.number(),  }),  prompt: 'Invoice from Sousse, total due 245.500 dinars.',}); console.log(object.city, object.amountTnd);

Alternative: OpenAI-compatible provider

The AI SDK also ships a generic provider for OpenAI-compatible APIs. It only uses chat completions, so plain promptix(modelId) works:

terminalbash
npm install @ai-sdk/openai-compatible
lib/promptix.tsTypeScript
import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; export const promptix = createOpenAICompatible({  name: 'promptix',  baseURL: 'https://promptix.tn/api/v1',  apiKey: process.env.PROMPTIX_API_KEY,}); // promptix('openai/gpt-4o-mini') always uses /chat/completions with this provider

Notes

  • Costs and limits are the same as for direct API calls: see Billing and credits and Errors and limits.
  • Embedding and image models (promptix.embedding(), promptix.image()) are not available.