new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Providers · all subjects

ai-sdk/extensibility

92 notes in this subject, read out of this brain and free to use. This is page 1 of 2.

Community providers implement Language Model Specification

There are community providers that implement the Language Model Specification and are compatible with the AI SDK.

Language Model Specification location

The Language Model Specification is located at https://github.com/vercel/ai/tree/main/packages/provider/src/language-model/v2.

Available AI SDK Adapters

The AI SDK provides adapters for LangChain and LlamaIndex, which are currently the available adapters for integrating with third-party libraries.

AI SDK Adapters enable UI functions with third-party libraries

Adapters are lightweight integrations that enable you to use the AI SDK UI functions (useChat and useCompletion) with 3rd party libraries.

LangSmithDeploymentTransport usage example

Example using LangSmithDeploymentTransport with useChat hook. Constructor accepts url (required, 'http://localhost:2024' for local or 'https://your-deployment.us.langgraph.app' for deployment) and optional apiKey. Passed as transport option to useChat.

Composing toUIMessageStream into caller-owned stream

When caller owns message lifecycle, use toUIMessageStream with sendStart: false and sendFinish: false to suppress outer chunks and avoid duplicate boundaries. Create outer stream with createUIMessageStream, compose inner toUIMessageStream by reading chunks and writing them. Set options independently to suppress only one boundary if needed.

Basic LangChain chat example

Example showing integration of LangChain ChatOpenAI with AI SDK using toBaseMessages and toUIMessageStream. Creates a Next.js API route that converts UI messages to LangChain format, streams response through the model, and converts back to UI stream format.

LangGraph workflow example

Example showing LangGraph StateGraph usage. Creates graph with MessagesAnnotation, adds agent node with callModel function, connects edges from __start__ to agent to __end__, converts UI messages via toBaseMessages, streams with streamMode ['values', 'messages'], and converts via toUIMessageStream.

streamEvents() example

Example using LangChain's streamEvents() method with version 'v2' for granular event streaming. The adapter automatically detects and handles streamEvents format, producing events like on_chat_model_stream, on_tool_start, on_tool_end.

Custom data streaming with LangChain tool

Example showing tool emitting progress updates using config.writer(). Tool emits events with type (e.g., 'progress', 'status'), optional id for persistence, and custom data fields. Stream uses streamMode ['values', 'messages', 'custom']. Client handles custom data via onData callback for transient events or renders persistent data parts with id.

Custom data behavior in LangChain adapter

Data with an id field is persistent (added to message.parts for rendering). Data without an id is transient (only delivered via the onData callback). The type field determines the event name: { type: 'progress' } becomes data-progress.

LangChain agent with tools example

Example creating LangChain agent with image generation tool. Uses createAgent with model and tools, converts UI messages via toBaseMessages, streams with streamMode ['values', 'messages', 'tools'], and converts stream via toUIMessageStream. Tool progress is streamed via on_tool_event events (preliminary: true) and on_tool_end events (final tool output).

LangChain adapter package

The `@ai-sdk/langchain` adapter provides seamless integration between LangChain, LangGraph, and the AI SDK. Installation requires both `@ai-sdk/langchain` and `@langchain/core` as peer dependency.

toBaseMessages function

Converts AI SDK `UIMessage` objects to LangChain `BaseMessage` objects. Parameters: `messages` (UIMessage[] - required). Returns: Promise<BaseMessage[]>.

convertModelMessages function

Converts AI SDK `ModelMessage` objects to LangChain `BaseMessage` objects. Useful when you already have model messages from `convertToModelMessages`. Parameters: `modelMessages` (ModelMessage[] - required). Returns: BaseMessage[].

toUIMessageStream function

Converts a LangChain/LangGraph stream to an AI SDK `UIMessageStream`. Automatically detects the stream type and handles direct model streams, LangGraph streams, and `streamEvents()` output. Parameters: `stream` (AsyncIterable<AIMessageChunk> | ReadableStream - required), `options` (ToUIMessageStreamOptions<TState> - optional with properties: sendStart (boolean, defaults to true), sendFinish (boolean, defaults to true), onStart, onToken, onText, onFinal, onFinish, onError, onAbort callbacks). Returns: ReadableStream<UIMessageChunk>.

LangSmithDeploymentTransport class

A ChatTransport implementation for LangSmith/LangGraph deployments. Used with the useChat hook's transport option. Constructor parameters: options (LangSmithDeploymentTransportOptions) with url (string - required, LangSmith deployment URL or local server URL), apiKey (string - optional for authentication), graphId (string - optional, defaults to 'agent'). Implements ChatTransport.

LangChain adapter features

The adapter supports: converting AI SDK UIMessage to LangChain BaseMessage format, transforming LangChain/LangGraph streams to AI SDK UIMessageStream, streamEvents() output for granular event streaming and observability, LangSmithDeploymentTransport for connecting to deployed LangGraph graphs, full support for text, tool calls, tool results, and multimodal content, custom data streaming with typed events (data-{type}).

streamEvents() vs graph.stream() usage

Use streamEvents() for debugging, observability, filtering by event type, agents created with createAgent, and migrating existing LCEL applications that rely on callbacks. Use graph.stream() with streamMode for LangGraph applications where you need structured state updates via values, messages, tools, or custom modes.

useChat hook supports multimodal messages with parts array

The useChat hook's sendMessage method accepts messages with a parts array. Each part can be either a file object with type 'file', mediaType, and url properties for images, or a text object with type 'text' and text property for text content.

convertToModelMessages handles multimodal content

The convertToModelMessages function automatically converts UIMessages to model messages and handles multimodal content including images, eliminating the need for manual conversion.

OpenAI language model implementation

OpenAIChatLanguageModel, located at packages/openai/src/chat/openai-chat-language-model.ts, is an example of a provider-specific language model implementation of LanguageModelV4.

Anthropic language model implementation

AnthropicLanguageModel, located at packages/anthropic/src/anthropic-language-model.ts, is an example of a provider-specific language model implementation of LanguageModelV4.

OpenAI embedding model implementation

OpenAIEmbeddingModel, located at packages/openai/src/embedding/openai-embedding-model.ts, is an example of a provider-specific embedding model implementation of EmbeddingModelV4.

Mistral embedding model implementation

MistralEmbeddingModel, located at packages/mistral/src/mistral-embedding-model.ts, is an example of a provider-specific embedding model implementation of EmbeddingModelV4.

Cohere reranking model implementation

CohereRerankingModel, located at packages/cohere/src/reranking/cohere-reranking-model.ts, is an example of a provider-specific reranking model implementation of RerankingModelV4.

Bedrock reranking model implementation

BedrockRerankingModel, located at packages/amazon-bedrock/src/reranking/bedrock-reranking-model.ts, is an example of a provider-specific reranking model implementation of RerankingModelV4.

OpenAI transcription model implementation

OpenAITranscriptionModel, located at packages/openai/src/transcription/openai-transcription-model.ts, is an example of a provider-specific transcription model implementation of TranscriptionModelV4.

Deepgram transcription model implementation

DeepgramTranscriptionModel, located at packages/deepgram/src/deepgram-transcription-model.ts, is an example of a provider-specific transcription model implementation of TranscriptionModelV4.

OpenAI speech model implementation

OpenAISpeechModel, located at packages/openai/src/speech/openai-speech-model.ts, is an example of a provider-specific speech model implementation of SpeechModelV4.

ElevenLabs speech model implementation

ElevenLabsSpeechModel, located at packages/elevenlabs/src/elevenlabs-speech-model.ts, is an example of a provider-specific speech model implementation of SpeechModelV4.

Fal video model implementation

FalVideoModel, located at packages/fal/src/fal-video-model.ts, is an example of a provider-specific video model implementation of VideoModelV4.

Replicate video model implementation

ReplicateVideoModel, located at packages/replicate/src/replicate-video-model.ts, is an example of a provider-specific video model implementation of VideoModelV4.

isCustomReasoning utility function

The isCustomReasoning function from @ai-sdk/provider-utils checks whether the caller supplied a custom value for reasoning (anything other than undefined or 'provider-default'). It returns false if no custom value was supplied, meaning no action is needed. It returns true if a custom value was supplied.

AI function and model specification relationship

The Vercel AI SDK uses a layered architecture where user-facing AI functions (like streamText and generateText) depend on model specifications (interfaces like LanguageModelV4), which are implemented by provider-specific model implementations (like OpenAIChatLanguageModel and AnthropicLanguageModel).

LanguageModelV4 interface

LanguageModelV4 is the model specification interface located at packages/provider/src/language-model/v4/language-model-v4.ts that defines how language models are used for text generation and structured generation workflows from prompt or message input.

embed function

embed is an AI function located at packages/ai/src/embed/embed.ts that creates a single embedding vector for one text value.

embedMany function

embedMany is an AI function located at packages/ai/src/embed/embed-many.ts that creates embedding vectors for multiple text values, batching calls when needed.

EmbeddingModelV4 interface

EmbeddingModelV4 is the model specification interface located at packages/provider/src/embedding-model/v4/embedding-model-v4.ts that defines how embedding models convert text into numeric vectors for similarity and retrieval use cases.

rerank function

rerank is an AI function located at packages/ai/src/rerank/rerank.ts that reorders documents and returns a relevance-ranked result set for a query.

RerankingModelV4 interface

RerankingModelV4 is the model specification interface located at packages/provider/src/reranking-model/v4/reranking-model-v4.ts that defines how reranking models reorder candidate documents by relevance to a query.

transcribe function

transcribe is an AI function located at packages/ai/src/transcribe/transcribe.ts that transcribes audio into text with segment and metadata support.

TranscriptionModelV4 interface

TranscriptionModelV4 is the model specification interface located at packages/provider/src/transcription-model/v4/transcription-model-v4.ts that defines how transcription models convert audio input into text transcripts.

generateSpeech function

generateSpeech is an AI function located at packages/ai/src/generate-speech/generate-speech.ts that generates speech audio from text input.

SpeechModelV4 interface

SpeechModelV4 is the model specification interface located at packages/provider/src/speech-model/v4/speech-model-v4.ts that defines how speech models synthesize audio from text input.

generateVideo function

generateVideo is an AI function located at packages/ai/src/generate-video/generate-video.ts that generates one or more videos from prompt input.

VideoModelV4 interface

VideoModelV4 is the model specification interface located at packages/provider/src/video-model/v4/video-model-v4.ts that defines how video models generate video outputs from prompts.

LanguageModelV4CallOptions reasoning field values

The reasoning field on LanguageModelV4CallOptions, located at packages/provider/src/language-model/v4/language-model-v4-call-options.ts, accepts the following possible values: 'provider-default', 'none', 'minimal', 'low', 'medium', 'high', 'xhigh'. This field controls how much reasoning a model performs before responding.

Reasoning 'none' handling

When reasoning is set to 'none', the provider should disable reasoning. Only some providers support this; others should emit an unsupported warning.

Effort mapping for reasoning values

mapReasoningToProviderEffort maps the reasoning spec enum to a provider-specific effort string via an effortMap. If the exact level has no provider equivalent, coerce to the next lower level; if there is no lower level, coerce to the next higher one. Emits a compatibility warning when coercion occurs, or an unsupported warning if no mapping exists at all.

Budget mapping for reasoning values

mapReasoningToProviderBudget maps the reasoning spec enum to an absolute token budget. It takes the model's maximum reasoning budget (or overall max output tokens if no separate reasoning limit exists), multiplies by a percentage for each level (defaults: minimal 2%, low 10%, medium 30%, high 60%, xhigh 90%), and clamps the result between minReasoningBudget (default 1024) and maxReasoningBudget. Custom percentages can be provided per provider.

Default reasoning budget percentages

The default percentages for budget mapping are: minimal 2%, low 10%, medium 30%, high 60%, xhigh 90%. The default minimum reasoning budget is 1024 tokens.

Reasoning unsupported warning

Providers that do not support reasoning configuration at the API level should emit an unsupported warning when isCustomReasoning returns true.

Experimental function prefix

If you are unable to find any of the AI functions mentioned in the architecture documentation in the codebase, they may only exist with an experimental_ prefix, meaning they are experimental and stable versions will likely be implemented at a later point.

Custom provider support via Language Model Specification

The AI SDK provides a Language Model Specification that enables building custom providers. Custom providers can integrate any service with the AI SDK while maintaining compatibility across providers.

@ai-sdk/rsc module for streaming React components

The @ai-sdk/rsc module provides tools for streaming React Server Components from the server to the client during language model generations. It includes the createStreamableUI function for creating streamable UI components.

wrapStream middleware caches stream parts as array

In LanguageModelMiddleware.wrapStream, the stream response should be cached as an array of LanguageModelV4StreamPart objects. When returning cached stream data, use the simulateReadableStream function to create a simulated ReadableStream that yields the cached response chunk-by-chunk.

wrapLanguageModel function wraps model with middleware

Use wrapLanguageModel to wrap a model with middleware. It accepts an object with a model property (model identifier) and a middleware property (LanguageModelMiddleware instance).

LanguageModelV4StreamPart timestamp handling in cache

When caching LanguageModelV4StreamPart objects, timestamp values are serialized as strings in JSON. When retrieving from cache, check if p.type === 'response-metadata' and p.timestamp exists, then convert the timestamp string back to a Date object.

TransformStream captures full response during streaming

A TransformStream can be used to intercept and collect all chunks from a stream during wrapStream. The collected chunks can be stored in cache in the flush method after streaming completes.

Give your agent this brain