> For clean Markdown of any page, append `.md` to the page URL.
> For a complete documentation index, see https://docs.sarvam.ai/llms.txt.
> For full documentation content in one file, see https://docs.sarvam.ai/llms-full.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.sarvam.ai/_mcp/server.

# Build with Sarvam AI in the Vercel AI SDK

> Use the community sarvam-ai-sdk provider to call Sarvam chat, text-to-speech, speech-to-text, translation, transliteration, and language identification from the Vercel AI SDK: install, setup, and code examples for every capability.

## Overview

The **[Vercel AI SDK](https://ai-sdk.dev)** is a TypeScript toolkit for building AI-powered applications, giving you a single set of functions (`generateText`, `streamText`, `generateObject`, and more) that work the same way across different model providers. Instead of learning each provider's own API, you write your app logic once against the AI SDK and swap providers by changing the `model` you pass in.

**[sarvam-ai-sdk](https://github.com/sarvamai/sarvam-ai-sdk)** is a community provider for the Vercel AI SDK that wraps Sarvam's **chat completion**, **text-to-speech**, **speech-to-text**, **translation**, **transliteration**, and **language identification** APIs behind the AI SDK's standard functions: `generateText`, `generateObject`, `generateSpeech`, and `transcribe`.

If you already build with the AI SDK, this lets you swap in Sarvam models without changing how you call `generateText` or stream responses, and it works alongside tool calling and structured output the same way any other AI SDK provider does.

`sarvam-ai-sdk` versions track a specific major version of `ai`. Install matching versions or you will hit type and runtime mismatches.

| Sarvam AI SDK version | Vercel AI SDK version |
| --------------------- | --------------------- |
| `0.5.x` (beta)        | `8.x.x` (beta)        |
| `0.4.x` (current)     | `7.x.x` (current)     |
| `0.3.x`               | `6.x.x`               |
| `0.2.x`               | `5.x.x`               |
| `0.1.x`               | `4.x.x`               |

## Quick start

### 1. Install

```bash
npm install sarvam-ai-sdk ai@7
```

`sarvam-ai-sdk` currently tracks AI SDK v7; installing `ai@latest` can silently pull in an incompatible future major version, so pin the major version shown in the compatibility table above.

### 2. Set your API key

Get an API key from **[dashboard.sarvam.ai](https://dashboard.sarvam.ai)** and add it to your environment:

```bash title=".env"
SARVAM_API_KEY="your_api_key"
```

### 3. Create a provider instance

```ts
import { sarvam } from "sarvam-ai-sdk";
```

`sarvam` reads `SARVAM_API_KEY` from the environment automatically, so no explicit client setup is needed.

### 4. Generate text

```ts
import { sarvam } from "sarvam-ai-sdk";
import { generateText } from "ai";

try {
  const { text } = await generateText({
    model: sarvam("sarvam-30b"),
    prompt: "Translate this to malayalam: 'Keep cooking, guys'",
  });

  console.log(text); // പാചകം തുടരൂ, സുഹൃത്തുക്കളേ
} catch (error) {
  console.error("generateText failed:", error);
}
```

If longer completions come back truncated mid-sentence, the model is hitting its output token limit. Raise the limit and check the finish reason to confirm when a response was truncated. Through the AI SDK this is `maxOutputTokens` / `finishReason`; calling Sarvam directly (Python or REST) it's the underlying `max_tokens` / `finish_reason`:

```ts
const result = await generateText({
  model: sarvam("sarvam-30b"),
  prompt: "Write a detailed explanation of...",
  maxOutputTokens: 4096,
});

if (result.finishReason === "length") {
  console.warn("Response was truncated; consider raising maxOutputTokens further.");
}
```

```python
from sarvamai import SarvamAI

client = SarvamAI(api_subscription_key="YOUR_SARVAM_API_KEY")

response = client.chat.completions(
    model="sarvam-30b",
    messages=[{"role": "user", "content": "Write a detailed explanation of..."}],
    max_tokens=4096,
)

if response.choices[0].finish_reason == "length":
    print("Response was truncated; consider raising max_tokens further.")
```

```bash
curl -X POST https://api.sarvam.ai/v1/chat/completions \
  -H "api-subscription-key: $SARVAM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "sarvam-30b",
    "messages": [{"role": "user", "content": "Write a detailed explanation of..."}],
    "max_tokens": 4096
  }'
```

Check `choices[0].finish_reason` in the response; `"length"` means the output was truncated.

***

## Text-to-speech

Use `generateSpeech` with `sarvam.speech(model, targetLanguage)`:

```ts
import { sarvam } from "sarvam-ai-sdk";
import { generateSpeech } from "ai";
import { writeFile } from "fs/promises";

try {
  const { audio } = await generateSpeech({
    model: sarvam.speech("bulbul:v3", "ml-IN"),
    text: "പാചകം തുടരൂ, സുഹൃത്തുക്കളേ",
  });

  const audioBuffer = Buffer.from(audio.base64, "base64");
  await writeFile("./output.wav", audioBuffer);
} catch (error) {
  console.error("generateSpeech failed:", error);
}
```

See [Text to Speech](/api-reference-docs/text-to-speech/api/overview) for supported languages, speakers, and audio options available on the underlying API.

## Speech-to-text

Use `transcribe` with `sarvam.transcription(model, language)`:

```ts
import { sarvam } from "sarvam-ai-sdk";
import { transcribe } from "ai";
import { readFile } from "fs/promises";

try {
  const { text } = await transcribe({
    model: sarvam.transcription("saaras:v3", "en-IN"),
    audio: await readFile("./input.wav"),
  });

  console.log(text); // Pachakam thudaroo, suhruthukkale.
} catch (error) {
  console.error("transcribe failed:", error);
}
```

See [Saaras](/api-reference-docs/getting-started/models/saaras) and [Saarika (Legacy)](/api-reference-docs/getting-started/models/saarika) for model details.

## Translation

```ts
import { sarvam } from "sarvam-ai-sdk";
import { generateText } from "ai";

try {
  const result = await generateText({
    model: sarvam.translation("mayura:v1", {
      from: "ml-IN",
      to: "en-IN",
    }),
    prompt: "ഇതൊക്കെ ശ്രദ്ധിക്കണ്ടേ അംബാനെ?",
  });

  console.log(result.text); // Shouldn't we be careful about this, Ambane?
} catch (error) {
  console.error("Translation failed:", error);
}
```

Only `prompt` and `role: user` messages are translated; `system` and `assistant` messages pass through unchanged.

## Transliteration

```ts
import { sarvam } from "sarvam-ai-sdk";
import { generateText } from "ai";

try {
  const result = await generateText({
    model: sarvam.transliterate({
      to: "ml-IN",
      from: "en-IN", // optional
    }),
    prompt: "eda mone, happy alle?",
  });

  console.log(result.text); // എടാ മോനെ, ഹാപ്പി അല്ലേ?
} catch (error) {
  console.error("Transliteration failed:", error);
}
```

## Language identification

```ts
import { sarvam } from "sarvam-ai-sdk";
import { generateText } from "ai";

try {
  const result = await generateText({
    model: sarvam.languageIdentification(),
    prompt: "ബുദ്ധിയാണ് സാറേ ഇവൻ്റെ മെയിൻ",
  });

  console.log(result.text); // ml-IN
} catch (error) {
  console.error("Language identification failed:", error);
}
```

***

## Tool calling

Sarvam chat models support the AI SDK's standard `tool()` interface:

```ts
import { z } from "zod";
import { generateText, tool } from "ai";
import { sarvam } from "sarvam-ai-sdk";

try {
  const result = await generateText({
    model: sarvam("sarvam-30b"),
    tools: {
      weather: tool({
        description: "Get the weather in a location",
        inputSchema: z.object({
          location: z.string(),
        }),
        execute: async ({ location }) => ({
          location,
          temperature: 72 + Math.floor(Math.random() * 21) - 10,
        }),
      }),
    },
    instructions: "You are a helpful AI",
    prompt: "കൊച്ചിയിലെ കാലാവസ്ഥ എന്താണ്?",
  });

  console.log(result.toolResults);
} catch (error) {
  console.error("Tool call failed:", error);
}
```

## Structured output

Prefer `generateText` with `Output.object`; `generateObject` still works but is deprecated in the AI SDK:

```ts
import { z } from "zod";
import { sarvam } from "sarvam-ai-sdk";
import { generateText, Output } from "ai";

try {
  const { output } = await generateText({
    model: sarvam("sarvam-105b"),
    output: Output.object({
      name: "Recipe",
      description: "A recipe with a name, ingredients and steps",
      schema: z.object({
        recipe: z.object({
          name: z.string(),
          ingredients: z.array(z.string()),
          steps: z.array(z.string()),
        }),
      }),
    }),
    prompt: "Generate a South Indian recipe, in Malayalam",
  });

  console.log(output);
} catch (error) {
  console.error("Structured output generation failed:", error);
}
```

***

## All APIs at a glance

```ts
import { sarvam } from "sarvam-ai-sdk";

// Chat completion
sarvam("sarvam-105b");
sarvam.languageModel("sarvam-30b");

// Transliteration
sarvam.transliterate({ to: "ml-IN", from: "en-IN" });

// Translation
sarvam.translation("mayura:v1", { from: "en-IN", to: "ml-IN" });

// Language identification
sarvam.languageIdentification();

// Text-to-speech
sarvam.speech("bulbul:v3", "ml-IN");

// Speech-to-text
sarvam.transcription("saaras:v3");
```

| Function                                              | AI SDK entry point                             | Underlying Sarvam capability                                              |
| ----------------------------------------------------- | ---------------------------------------------- | ------------------------------------------------------------------------- |
| `sarvam("sarvam-105b")` / `sarvam.languageModel(...)` | `generateText`, `generateObject`, `streamText` | [Chat completion](/api-reference-docs/chat-completion/overview)           |
| `sarvam.translation(model, { from, to })`             | `generateText`                                 | [Translation](/api-reference-docs/text-preprocessing/api/translate)       |
| `sarvam.transliterate({ from, to })`                  | `generateText`                                 | Transliteration                                                           |
| `sarvam.languageIdentification()`                     | `generateText`                                 | [Language identification](/api-reference-docs/text-preprocessing/api/lid) |
| `sarvam.speech(model, targetLanguage)`                | `generateSpeech`                               | [Text to Speech](/api-reference-docs/text-to-speech/api/overview)         |
| `sarvam.transcription(model, language)`               | `transcribe`                                   | [Speech to Text](/api-reference-docs/speech-to-text/apis/overview)        |

***

## Troubleshooting

**Type or runtime errors after install:** Your `sarvam-ai-sdk` and `ai` versions are mismatched. Check the version table above and pin `ai` to the major version your `sarvam-ai-sdk` version expects (for example `ai@6` with `sarvam-ai-sdk@0.3.x`).

**401/403 from Sarvam:** Confirm `SARVAM_API_KEY` is set in the environment the process actually reads (`.env` loaded, no stray whitespace in the key). Sarvam's REST API returns `403` (not `401`) for auth failures; see [Errors & Troubleshooting](/api-reference-docs/errors-troubleshooting).

**Translation/transliteration/LID returns the input unchanged:** These wrappers only act on `prompt` and `role: user` messages; `system` and `assistant` messages are passed through as-is by design.

**`generateObject` deprecation warnings:** Migrate to `generateText` with `Output.object(...)` as shown above; `generateObject` still works but is deprecated upstream in the AI SDK.

**Responses get cut off partway through:** Output length is capped by default (`maxOutputTokens` in the AI SDK, `max_tokens` when calling Sarvam directly from Python or REST), so long completions can stop mid-sentence. Raise the limit and check the finish reason (`finishReason` / `finish_reason` equals `"length"`) to confirm when a response was truncated; see the [Generate text](#4-generate-text) section above for examples in JavaScript, Python, and cURL.

***

## Additional resources

* [sarvam-ai-sdk on GitHub](https://github.com/sarvamai/sarvam-ai-sdk) (source, issues, changelog)
* [sarvam-ai-sdk on npm](https://npmjs.com/sarvam-ai-sdk)
* [Sarvam provider on the AI SDK docs](https://v7.ai-sdk.dev/providers/community-providers/sarvam)
* [Chat completion overview](/api-reference-docs/chat-completion/overview)
* [Text to Speech overview](/api-reference-docs/text-to-speech/api/overview)
* [Speech to Text overview](/api-reference-docs/speech-to-text/apis/overview)
* [Build workflows with Sarvam AI in n8n](/api-reference-docs/integration/n8n) (same Sarvam capabilities without writing code)

***

## Need help?

* Sarvam Support: [developer@sarvam.ai](mailto:developer@sarvam.ai)
* Community: [Join the Discord Community](https://discord.com/invite/5rAsykttcs)