# Vercel AI SDK Integration

URL: https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/integrations/vercel-ai-sdk

Interfaze has a native Vercel AI SDK provider, so you get `generateText`, `streamText`, and structured output through `Output` out of the box, plus Interfaze extras like [precontext](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/precontext), [reasoning](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/reasoning), and the [cache](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/caching) flag on `providerMetadata`.

## Installation

These examples use Vercel AI SDK v7 (`ai@^7.0.0`).

**npm / yarn**

```typescript
npm install ai @interfaze-ai/ai-sdk
# or
yarn add ai @interfaze-ai/ai-sdk
```

## Official Documentation

- [Vercel AI SDK Documentation](https://ai-sdk.dev/docs/introduction)
- [Interfaze provider on npm](https://www.npmjs.com/package/@interfaze-ai/ai-sdk)
- [Vercel Stream](https://ai-sdk.dev/docs/foundations/streaming#streaming)

## Basic Setup

Import the default `interfaze` instance to read `INTERFAZE_API_KEY` from the environment, or build your own with `createInterfaze`.

**Vercel AI SDK**

```typescript
import { createInterfaze, interfaze } from "@interfaze-ai/ai-sdk";

// reads INTERFAZE_API_KEY from the environment
interfaze("interfaze-1");

// or configure it yourself
const custom = createInterfaze({ apiKey: "<your-api-key>" });
```

## Examples

### Text Generation

**Vercel AI SDK**

```typescript
import { generateText } from "ai";

const { text } = await generateText({
  model: interfaze("interfaze-1"),
  prompt: "Write a short story about a robot learning to paint",
});

console.log(text);
```

### Structured Output

Learn more about [structured output](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/structured-output).

**Vercel AI SDK**

```typescript
import { generateText, Output } from "ai";
import { z } from "zod";

const weatherSchema = z.object({
  city: z.string().describe("The name of the city"),
  temperature_celsius: z.number().describe("Current temperature in Celsius"),
  condition: z.string().describe("Weather condition, e.g. sunny, rainy, cloudy"),
});

const { output } = await generateText({
  model: interfaze("interfaze-1"),
  output: Output.object({ schema: weatherSchema }),
  prompt: "What is the current weather in Tokyo?",
});

console.log(output);
```

### Image / File Input

**Vercel AI SDK**

```typescript
import { generateText } from "ai";

const { text } = await generateText({
  model: interfaze("interfaze-1"),
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "What is in this image?" },
        {
          type: "image",
          image: new URL("https://r2public.jigsawstack.com/interfaze/examples/construction.png"),
        },
      ],
    },
  ],
});

console.log(text);
```

### Streaming

Learn more about [streaming](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/streaming).

**Vercel AI SDK**

```typescript
import { streamText } from "ai";

const { textStream } = streamText({
  model: interfaze("interfaze-1"),
  prompt: "Write a short story about a robot learning to paint",
});

for await (const delta of textStream) {
  process.stdout.write(delta);
}
```

### Function Calling

Learn more about [function calling](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/function-calling).

**Vercel AI SDK**

```typescript
import { generateText, tool, stepCountIs } from "ai";
import { z } from "zod";

const { text } = await generateText({
  model: interfaze("interfaze-1"),
  prompt: "What's the weather like in San Francisco?",
  tools: {
    weather: tool({
      description: "Get the weather in a location",
      inputSchema: z.object({
        location: z.string().describe("The location to get the weather for"),
      }),
      execute: async ({ location }) => ({
        location,
        temperature: 72 + Math.floor(Math.random() * 21) - 10,
      }),
    }),
  },
  stopWhen: stepCountIs(5),
  toolChoice: "auto",
});

console.log(text);
```

## Interfaze metadata

Fields a plain chat provider would drop come back on `providerMetadata.interfaze`.

**Vercel AI SDK**

```typescript
import { generateText } from "ai";

const { text, providerMetadata } = await generateText({
  model: interfaze("interfaze-1"),
  prompt: "Latest news on Nvidia",
});

console.log(providerMetadata?.interfaze?.vcache); // boolean — cache hit
console.log(providerMetadata?.interfaze?.reasoning); // string | undefined
console.log(providerMetadata?.interfaze?.precontext); // raw task output
```

- `vcache` tells you whether the response came from the [cache](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/caching).
- `reasoning` carries the thinking text when [reasoning](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/reasoning) is on.
- `precontext` carries the raw output of any task the model ran. Learn more about [precontext](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/precontext).

## Client options

Router, cache, and streaming behaviour are set once on the provider.

**Vercel AI SDK**

```typescript
import { createInterfaze } from "@interfaze-ai/ai-sdk";

const interfaze = createInterfaze({
  apiKey: process.env.INTERFAZE_API_KEY,
  showAdditionalInfo: true, // stream precontext deltas as they're produced
  bypassMoA: true, // skip the mixture-of-agents router
  bypassCache: true, // skip the semantic cache
});
```

Learn more about [bypassing MoA](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/bypass-moa) and [caching](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/caching).

## Request options

Per-request options go under `providerOptions.interfaze`.

**Vercel AI SDK**

```typescript
import { generateText } from "ai";

const { text, providerMetadata } = await generateText({
  model: interfaze("interfaze-1"),
  prompt: "Which region should we launch in first, and why?",
  providerOptions: {
    interfaze: {
      reasoningEffort: "high",
      guard: ["S1", "S10", "S12_IMAGE"],
    },
  },
});

console.log(providerMetadata?.interfaze?.reasoning);
console.log(text);
```

Learn more about [reasoning](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/reasoning) and [guardrails](https://interfaze-landing-1am6r0242-jigsaw-stack.vercel.app/docs/guardrails).

## Errors

Interfaze errors surface as the AI SDK's `APICallError`, carrying the HTTP status and response body.

**Vercel AI SDK**

```typescript
import { APICallError, generateText } from "ai";

try {
  await generateText({
    model: interfaze("interfaze-1"),
    prompt: "Who is the founder of Interfaze?",
  });
} catch (error) {
  if (APICallError.isInstance(error)) {
    console.log(error.statusCode); // e.g. 400, 401, 429
    console.log(error.responseBody); // raw Interfaze error payload
  }
}
```
