DeepInfra

The DeepInfra provider contains support for state-of-the-art models through the DeepInfra API, including Llama 3, Mixtral, Qwen, and many other popular open-source models. Start with AI Gateway for models in its catalog (recommended), or use @ai-sdk/deepinfra to connect directly to DeepInfra.

import { generateText } from 'ai';

const { text } = await generateText({
  model: "meta/llama-4-scout",
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
  providerOptions: {
    gateway: {
      order: ['deepinfra'],
    },
  },
});

console.log(text);

Using AI Gateway

The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install ai:

$ pnpm add ai

Set an AI Gateway API key in your environment:

export AI_GATEWAY_API_KEY=your_api_key_here

Vercel deployments can also use OIDC authentication. See the AI Gateway provider documentation for configuration and the model catalog for available models.

The example sets providerOptions.gateway.order to ['deepinfra']. This sends the request to DeepInfra first, then falls back to other available providers of the same model if DeepInfra is unavailable or the request fails. order sets a preference; it does not restrict fallback to DeepInfra. See provider ordering.

The dropdown lists models served by DeepInfra through Gateway with a corresponding direct-provider example. Provider availability depends on the model and your team's Provider Allowlist.

Using @ai-sdk/deepinfra

For direct access to DeepInfra, install ai and @ai-sdk/deepinfra:

$ pnpm add ai @ai-sdk/deepinfra

Set your DeepInfra API credential in the DEEPINFRA_API_KEY environment variable:

export DEEPINFRA_API_KEY=your_api_key_here

AI Gateway does not expose every API in @ai-sdk/deepinfra. Use the direct provider for provider-specific APIs or custom connection settings.

The following sections document the direct @ai-sdk/deepinfra provider, including its model factories and settings. Gateway model and feature availability can differ.

Provider Instance

You can import the default provider instance deepInfra from @ai-sdk/deepinfra:

import { deepInfra } from '@ai-sdk/deepinfra';

If you need a customized setup, you can import createDeepInfra from @ai-sdk/deepinfra and create a provider instance with your settings:

import { createDeepInfra } from '@ai-sdk/deepinfra';

const deepInfra = createDeepInfra({
  apiKey: process.env.DEEPINFRA_API_KEY ?? '',
});

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

  • baseURL string

    Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is https://api.deepinfra.com/v1.

    Note: Language models and embeddings use OpenAI-compatible endpoints at {baseURL}/openai, while image models use {baseURL}/inference.

  • apiKey string | PromiseLike<string> | (() => string | PromiseLike<string>)

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

    A function is called before each request and may return a promise. Use it to supply refreshed credentials; a promise value resolves to the same key each time.

  • headers Record<string,string>

    Custom headers to include in the requests.

  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>

    Custom fetch implementation. Defaults to the global fetch function. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.

Language Models

You can create language models using a provider instance. The first argument is the model ID, for example:

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

const { text } = await generateText({
  model: deepInfra('meta-llama/Meta-Llama-3.1-70B-Instruct'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

DeepInfra language models can also be used in the streamText function (see AI SDK Core).

Model Capabilities

ModelImage InputObject GenerationTool UsageTool Streaming
deepseek-ai/DeepSeek-V3
deepseek-ai/DeepSeek-R1
deepseek-ai/DeepSeek-R1-Distill-Llama-70B
deepseek-ai/DeepSeek-R1-Turbo
google/codegemma-7b-it
google/gemma-2-9b-it
meta-llama/Llama-3.2-11B-Vision-Instruct
meta-llama/Llama-3.2-90B-Vision-Instruct
meta-llama/Llama-3.3-70B-Instruct-Turbo
meta-llama/Llama-3.3-70B-Instruct
meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8
meta-llama/Llama-4-Scout-17B-16E-Instruct
meta-llama/Meta-Llama-3.1-405B-Instruct
meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo
meta-llama/Meta-Llama-3.1-70B-Instruct
meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo
meta-llama/Meta-Llama-3.1-8B-Instruct
microsoft/WizardLM-2-8x22B
mistralai/Mixtral-8x7B-Instruct-v0.1
nvidia/Llama-3.1-Nemotron-70B-Instruct
Qwen/Qwen2-7B-Instruct
Qwen/Qwen2.5-72B-Instruct
Qwen/Qwen2.5-Coder-32B-Instruct
Qwen/QwQ-32B-Preview

The table above lists popular models. Please see the DeepInfra docs for a full list of available models. You can also pass any available provider model ID as a string if needed.

Image Models

You can create DeepInfra image models using the .image() factory method. For more on image generation with the AI SDK see generateImage().

import { deepInfra, type DeepInfraImageModelOptions } from '@ai-sdk/deepinfra';
import { generateImage } from 'ai';

const { image } = await generateImage({
  model: deepInfra.image('stabilityai/sd3.5'),
  prompt: 'A futuristic cityscape at sunset',
  aspectRatio: '16:9',
});

Model support for size and aspectRatio parameters varies by model. Please check the individual model documentation on DeepInfra's models page for supported options and additional parameters.

Model-specific options

You can pass model-specific parameters using the providerOptions.deepinfra field:

import { deepInfra } from '@ai-sdk/deepinfra';
import { generateImage } from 'ai';

const { image } = await generateImage({
  model: deepInfra.image('stabilityai/sd3.5'),
  prompt: 'A futuristic cityscape at sunset',
  aspectRatio: '16:9',
  providerOptions: {
    deepinfra: {
      num_inference_steps: 30,
    } satisfies DeepInfraImageModelOptions,
  },
});

The following options are available via providerOptions.deepinfra. You can type them with DeepInfraImageModelOptions.

  • negative_prompt string

    Text description of what to avoid in the generated image.

  • num_inference_steps number

    Number of denoising steps for models that expose this option.

  • guidance_scale number

    Guidance scale for models that expose this option.

  • guidance number

    Guidance value for image edit models that expose this option.

  • response_format 'b64_json'

    Format for OpenAI-compatible image responses. DeepInfra currently supports b64_json.

  • quality string

    OpenAI-compatible image quality option.

  • style string

    OpenAI-compatible image style option.

  • user string

    Unique identifier for your end user.

DeepInfra image generation models expose model-specific parameters. Additional fields can be passed through providerOptions.deepinfra and are validated by DeepInfra.

Image Editing

DeepInfra supports image editing through models like Qwen/Qwen-Image-Edit. Pass input images via prompt.images to transform or edit existing images.

Basic Image Editing

Transform an existing image using text prompts:

const imageBuffer = readFileSync('./input-image.png');

const { images } = await generateImage({
  model: deepInfra.image('Qwen/Qwen-Image-Edit'),
  prompt: {
    text: 'Turn the cat into a golden retriever dog',
    images: [imageBuffer],
  },
  size: '1024x1024',
});

Inpainting with Mask

Edit specific parts of an image using a mask. Transparent areas in the mask indicate where the image should be edited:

const image = readFileSync('./input-image.png');
const mask = readFileSync('./mask.png');

const { images } = await generateImage({
  model: deepInfra.image('Qwen/Qwen-Image-Edit'),
  prompt: {
    text: 'A sunlit indoor lounge area with a pool containing a flamingo',
    images: [image],
    mask: mask,
  },
});

Multi-Image Combining

Combine multiple reference images into a single output:

const cat = readFileSync('./cat.png');
const dog = readFileSync('./dog.png');

const { images } = await generateImage({
  model: deepInfra.image('Qwen/Qwen-Image-Edit'),
  prompt: {
    text: 'Create a scene with both animals together, playing as friends',
    images: [cat, dog],
  },
});

Input images can be provided as Buffer, ArrayBuffer, Uint8Array, or base64-encoded strings. DeepInfra uses an OpenAI-compatible image editing API at https://api.deepinfra.com/v1/openai/images/edits.

Model Capabilities

For models supporting aspect ratios, the following ratios are typically supported: 1:1 (default), 16:9, 1:9, 3:2, 2:3, 4:5, 5:4, 9:16, 9:21

For models supporting size parameters, dimensions must typically be:

  • Multiples of 32
  • Width and height between 256 and 1440 pixels
  • Default size is 1024x1024
ModelDimensions SpecificationNotesImage EditingMask InputMax Images Per Call
stabilityai/sd3.5Aspect RatioPremium quality base model, 8B parameters1
black-forest-labs/FLUX-1.1-proSizeLatest state-of-art model with superior prompt following1
black-forest-labs/FLUX-1-schnellSizeFast generation in 1-4 steps1
black-forest-labs/FLUX-1-devSizeOptimized for anatomical accuracy1
black-forest-labs/FLUX-proSizeFlagship Flux model1
black-forest-labs/FLUX.1-Kontext-devSizeImage editing and transformation model1
black-forest-labs/FLUX.1-Kontext-proSizeProfessional image editing and transformation1
stabilityai/sd3.5-mediumAspect RatioBalanced 2.5B parameter model1
stabilityai/sdxl-turboAspect RatioOptimized for fast generation1
Qwen/Qwen-Image-EditNot declaredNot declared1

For more details and pricing information, see the DeepInfra text-to-image models page.

Embedding Models

You can create DeepInfra embedding models using the .embeddingModel() factory method. For more on embedding models with the AI SDK see embed().

import { deepInfra } from '@ai-sdk/deepinfra';
import { embed } from 'ai';

const { embedding } = await embed({
  model: deepInfra.embeddingModel('BAAI/bge-large-en-v1.5'),
  value: 'sunny day at the beach',
});

Model Capabilities

ModelDimensionsMax Tokens
BAAI/bge-base-en-v1.5768512
BAAI/bge-large-en-v1.51024512
BAAI/bge-m310248192
intfloat/e5-base-v2768512
intfloat/e5-large-v21024512
intfloat/multilingual-e5-large1024512
sentence-transformers/all-MiniLM-L12-v2384256
sentence-transformers/all-MiniLM-L6-v2384256
sentence-transformers/all-mpnet-base-v2768384
sentence-transformers/clip-ViT-B-3251277
sentence-transformers/clip-ViT-B-32-multilingual-v151277
sentence-transformers/multi-qa-mpnet-base-dot-v1768512
sentence-transformers/paraphrase-MiniLM-L6-v2384128
shibing624/text2vec-base-chinese768512
thenlper/gte-base768512
thenlper/gte-large1024512

For a complete list of available embedding models, see the DeepInfra embeddings page.

Provider Option Reference

The following option definitions are generated from the provider catalog.

OperationOptionDescription
embeddingdimensionsThe number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
embeddinguserA unique identifier representing your end-user, which can help providers to monitor and detect abuse.
languageuserA unique identifier representing your end-user, which can help providers to monitor and detect abuse.
languagereasoningEffortReasoning effort for reasoning models. Defaults to medium. Set to null to send "reasoning_effort": null, for providers that treat an explicit null differently from an omitted field.
languagetextVerbosityControls the verbosity of the generated text. Defaults to medium.
languagestrictJsonSchemaWhether to use strict JSON schema validation. When true, the model uses constrained decoding to guarantee schema compliance. Only used when the provider supports structured outputs and a schema is provided.
languageechoEcho back the prompt in addition to the completion.
languagelogitBiasModify the likelihood of specified tokens appearing in the completion. Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100.
languagesuffixThe suffix that comes after a completion of inserted text.
imagenegative_promptnegative_prompt
imagenum_inference_stepsnum_inference_steps
imageguidance_scaleguidance_scale
imageguidanceguidance
imageresponse_formatresponse_format
imagequalityquality
imagestylestyle
imageuseruser