Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 237 additions & 0 deletions content/providers/01-ai-sdk-providers/35-minimax.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
---
title: MiniMax
description: Learn how to use MiniMax's models with the AI SDK.
---

# MiniMax Provider

The [MiniMax](https://www.minimaxi.com) provider offers access to powerful language models through the MiniMax API, including their latest [MiniMax-M2 model](https://platform.minimax.io/docs/guides/text-generation).

API keys can be obtained from the [MiniMax Platform](https://platform.minimax.io/user-center/basic-information/interface-key).

## Setup

The MiniMax provider is available via the `@ai-sdk/minimax` module. You can install it with:

<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
<Tab>
<Snippet text="pnpm add @ai-sdk/minimax" dark />
</Tab>
<Tab>
<Snippet text="npm install @ai-sdk/minimax" dark />
</Tab>
<Tab>
<Snippet text="yarn add @ai-sdk/minimax" dark />
</Tab>

<Tab>
<Snippet text="bun add @ai-sdk/minimax" dark />
</Tab>
</Tabs>

## Provider Instance

The MiniMax provider supports two API compatibility modes:

### Anthropic-Compatible API (Default)

You can import the default provider instance `minimax` from `@ai-sdk/minimax`:

```ts
import { minimax } from '@ai-sdk/minimax';
```

Or explicitly use the Anthropic-compatible instance:

```ts
import { minimaxAnthropic } from '@ai-sdk/minimax';
```

### OpenAI-Compatible API

For OpenAI-compatible API format:

```ts
import { minimaxOpenAI } from '@ai-sdk/minimax';
```

## Custom Configuration

For custom configuration, you can use the `createMinimax` (Anthropic-compatible) or `createMinimaxOpenAI` (OpenAI-compatible) functions:

### Anthropic-Compatible Configuration (Default)

```ts
import { createMinimax } from '@ai-sdk/minimax';

const minimax = createMinimax({
apiKey: process.env.MINIMAX_API_KEY ?? ''
});
```

### OpenAI-Compatible Configuration

```ts
import { createMinimaxOpenAI } from '@ai-sdk/minimax';

const minimaxOpenAI = createMinimaxOpenAI({
apiKey: process.env.MINIMAX_API_KEY ?? ''
});
```

### Configuration Options

You can use the following optional settings to customize the MiniMax provider instance:

- **baseURL** _string_

Use a different URL prefix for API calls.
- Anthropic-compatible default: `https://api.minimax.io/anthropic/v1`
- OpenAI-compatible default: `https://api.minimax.io/v1`

- **apiKey** _string_

API key that is being sent using the `Authorization` header. It defaults to
the `MINIMAX_API_KEY` environment variable.

- **headers** _Record&lt;string,string&gt;_

Custom headers to include in the requests.

- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_

Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.

## Language Models

You can create language models using a provider instance:

### Anthropic-Compatible (Default)

```ts
import { minimax } from '@ai-sdk/minimax';
import { generateText } from 'ai';

const { text } = await generateText({
model: minimax('MiniMax-M2'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
```

### OpenAI-Compatible

```ts
import { minimaxOpenAI } from '@ai-sdk/minimax';
import { generateText } from 'ai';

const { text } = await generateText({
model: minimaxOpenAI('MiniMax-M2'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
```

You can also use the `.chat()` or `.languageModel()` factory methods:

```ts
// Default (Anthropic-compatible):
const model = minimax.chat('MiniMax-M2');
// or
const model = minimax.languageModel('MiniMax-M2');

// For OpenAI-compatible:
const openaiModel = minimaxOpenAI.chat('MiniMax-M2');
// or
const openaiModel = minimaxOpenAI.languageModel('MiniMax-M2');
```

MiniMax language models can be used in the `streamText` function
(see [AI SDK Core](/docs/ai-sdk-core)).

## API Compatibility

MiniMax provides two API formats. Both are included in this package:

### When to Use Each Format

- **Anthropic-Compatible** (`minimax`, default): Provides better support for advanced features and is recommended for new projects.
- **OpenAI-Compatible** (`minimaxOpenAI`): Use this if you're migrating from OpenAI or prefer the OpenAI API format.

### Key Differences

The main difference is the API request/response format:

- **Anthropic format** (default): Uses Anthropic Messages API format with `anthropic-version` header
- **OpenAI format**: Uses standard OpenAI chat completion format

Both formats access the same MiniMax models with the same capabilities.

## Available Models

MiniMax offers two model variants:

| Model Name | Description |
| ---------- | ----------- |
| `MiniMax-M2` | Agentic capabilities, Advanced reasoning |
| `MiniMax-M2-Stable` | High concurrency and commercial use |

Both models share the same API interface and usage patterns.

## Model Capabilities

| Model | Text Generation | Object Generation | Image Input | Tool Usage | Tool Streaming |
| ------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
| `MiniMax-M2` | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
| `MiniMax-M2-Stable` | <Check size={18} /> | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |

<Note>
Please see the [MiniMax docs](https://platform.minimax.io/docs/api-reference/text-intro) for a full list
of available models and their capabilities. The provider accepts any model ID as a
string for forward compatibility.
</Note>

## Example Usage

### Basic Text Generation

```ts
import { minimax } from '@ai-sdk/minimax';
import { generateText } from 'ai';

const result = await generateText({
model: minimax('MiniMax-M2'),
prompt: 'Explain quantum computing in simple terms.',
});

console.log(result.text);
```

### Streaming

```ts
import { minimax } from '@ai-sdk/minimax';
import { streamText } from 'ai';

const result = streamText({
model: minimax('MiniMax-M2'),
prompt: 'Write a short story about a robot learning to paint.',
});

for await (const chunk of result.textStream) {
console.log(chunk);
}
```

### Using MiniMax-M2-Stable for Production

```ts
import { minimax } from '@ai-sdk/minimax';
import { generateText } from 'ai';

const result = await generateText({
model: minimax('MiniMax-M2-Stable'),
prompt: 'Write a short story about a robot learning to paint.',
});

console.log(result.text);
```

1 change: 1 addition & 0 deletions examples/ai-core/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ TOGETHER_AI_API_KEY=""
VERCEL_API_KEY=""
VERCEL_OIDC_TOKEN=""
XAI_API_KEY=""
MINIMAX_API_KEY=""
1 change: 1 addition & 0 deletions examples/ai-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@ai-sdk/lmnt": "workspace:*",
"@ai-sdk/luma": "workspace:*",
"@ai-sdk/hume": "workspace:*",
"@ai-sdk/minimax": "workspace:*",
"@ai-sdk/mistral": "workspace:*",
"@ai-sdk/openai": "3.0.0-beta.34",
"@ai-sdk/openai-compatible": "workspace:*",
Expand Down
29 changes: 29 additions & 0 deletions examples/ai-core/src/e2e/minimax.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'dotenv/config';
import { expect } from 'vitest';
import { minimax as provider } from '@ai-sdk/minimax';
import { APICallError } from 'ai';
import {
createFeatureTestSuite,
createLanguageModelWithCapabilities,
} from './feature-test-suite';
import { MinimaxErrorData } from '@ai-sdk/minimax';

const createChatModel = (modelId: string) =>
createLanguageModelWithCapabilities(provider.chat(modelId));

createFeatureTestSuite({
name: 'MiniMax',
models: {
invalidModel: provider.chat('no-such-model'),
languageModels: [createChatModel('MiniMax-M2')],
},
timeout: 10000,
customAssertions: {
errorValidator: (error: APICallError) => {
expect(
(error.data as MinimaxErrorData).error.message,
).toBeDefined();
},
},
})();

20 changes: 20 additions & 0 deletions examples/ai-core/src/generate-text/minimax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { minimax } from '@ai-sdk/minimax';
import { generateText } from 'ai';
import 'dotenv/config';

async function main() {
const result = await generateText({
model: minimax('MiniMax-M2'),
prompt: 'Invent a new holiday and describe its traditions.',
});

console.log('Text:');
console.log(result.text);
console.log();

console.log('Token usage:', result.usage);
console.log('Finish reason:', result.finishReason);
}

main().catch(console.error);

21 changes: 21 additions & 0 deletions examples/ai-core/src/stream-text/minimax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { minimax } from '@ai-sdk/minimax';
import { streamText } from 'ai';
import 'dotenv/config';

async function main() {
const result = streamText({
model: minimax('MiniMax-M2'),
prompt: 'Invent a new holiday and describe its traditions.',
});

for await (const textPart of result.textStream) {
process.stdout.write(textPart);
}

console.log();
console.log('Token usage:', await result.usage);
console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);

Loading