CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ai-sdk--xai

xAI Grok provider for the AI SDK containing language model support for the xAI chat and completion APIs

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

xAI Provider for AI SDK

The xAI provider enables developers to integrate xAI's Grok language models through the AI SDK framework. It provides a comprehensive TypeScript-based implementation with support for text generation, chat functionality, and image generation using xAI's Grok models.

Package Information

  • Package Name: @ai-sdk/xai
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ai-sdk/xai

Core Imports

import { xai, createXai } from '@ai-sdk/xai';
import type { 
  XaiProvider, 
  XaiProviderSettings, 
  XaiProviderOptions, 
  XaiErrorData 
} from '@ai-sdk/xai';
import type { LanguageModelV2, ImageModelV2, ProviderV2 } from '@ai-sdk/provider';
import type { FetchFunction } from '@ai-sdk/provider-utils';

For CommonJS:

const { xai, createXai } = require('@ai-sdk/xai');

Basic Usage

import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';

// Simple text generation
const { text } = await generateText({
  model: xai('grok-beta'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

// Using image generation
const imageModel = xai.image('grok-2-image');

Architecture

The xAI provider is built around several key components:

  • Provider Interface: Main XaiProvider interface extending AI SDK's ProviderV2
  • Model Factory Functions: Methods for creating language and image models
  • Configuration System: Flexible settings for API keys, base URLs, and custom headers
  • Error Handling: Structured error types and response handlers
  • OpenAI Compatibility: Built on OpenAI-compatible foundation for consistency

Capabilities

Provider Creation

Factory functions for creating xAI provider instances with custom configuration.

function createXai(options?: XaiProviderSettings): XaiProvider;

const xai: XaiProvider;

interface XaiProviderSettings {
  baseURL?: string;
  apiKey?: string;
  headers?: Record<string, string>;
  fetch?: FetchFunction;
}

Provider Configuration

Language Models

Chat and text generation using xAI's Grok models with comprehensive model ID support.

// Provider methods for language models
(modelId: XaiChatModelId): LanguageModelV2;
languageModel(modelId: XaiChatModelId): LanguageModelV2;
chat(modelId: XaiChatModelId): LanguageModelV2;

type XaiChatModelId =
  | 'grok-code-fast-1'
  | 'grok-4' | 'grok-4-0709' | 'grok-4-latest'
  | 'grok-3' | 'grok-3-latest' | 'grok-3-fast' | 'grok-3-fast-latest'
  | 'grok-3-mini' | 'grok-3-mini-latest' | 'grok-3-mini-fast' | 'grok-3-mini-fast-latest'
  | 'grok-2-vision-1212' | 'grok-2-vision' | 'grok-2-vision-latest'
  | 'grok-2-image-1212' | 'grok-2-image' | 'grok-2-image-latest'
  | 'grok-2-1212' | 'grok-2' | 'grok-2-latest'
  | 'grok-vision-beta' | 'grok-beta'
  | (string & {});

Language Models

Image Generation

Image generation capabilities using xAI's image models.

image(modelId: XaiImageModelId): ImageModelV2;
imageModel(modelId: XaiImageModelId): ImageModelV2;

type XaiImageModelId = 'grok-2-image' | (string & {});

Image Generation

Text Embedding Models

Text embedding model support (currently not supported - throws NoSuchModelError).

textEmbeddingModel(modelId: string): never;

Advanced Options

Provider-specific options for search parameters and reasoning effort.

interface XaiProviderOptions {
  reasoningEffort?: 'low' | 'high';
  searchParameters?: {
    mode: 'off' | 'auto' | 'on';
    returnCitations?: boolean;
    fromDate?: string;
    toDate?: string;
    maxSearchResults?: number;
    sources?: Array<
      | { type: 'web'; country?: string; excludedWebsites?: string[]; allowedWebsites?: string[]; safeSearch?: boolean; }
      | { type: 'x'; xHandles?: string[]; }
      | { type: 'news'; country?: string; excludedWebsites?: string[]; safeSearch?: boolean; }
      | { type: 'rss'; links: string[]; }
    >;
  };
}

Advanced Configuration

Types

interface XaiProvider extends ProviderV2 {
  (modelId: XaiChatModelId): LanguageModelV2;
  languageModel(modelId: XaiChatModelId): LanguageModelV2;
  chat(modelId: XaiChatModelId): LanguageModelV2;
  textEmbeddingModel(modelId: string): never;
  image(modelId: XaiImageModelId): ImageModelV2;
  imageModel(modelId: XaiImageModelId): ImageModelV2;
}

interface XaiErrorData {
  error: {
    message: string;
    type?: string | null;
    param?: any;
    code?: string | number | null;
  };
}

docs

advanced-configuration.md

image-generation.md

index.md

language-models.md

provider-configuration.md

tile.json