AI SDK CoreLanguageModelV1Middleware

LanguageModelV1Middleware

Language model middleware is an experimental feature.

Language model middleware provides a way to enhance the behavior of language models by intercepting and modifying the calls to the language model. It can be used to add features like guardrails, RAG, caching, and logging in a language model agnostic way.

See Language Model Middleware for more information.

Import

import { Experimental_LanguageModelV1Middleware as LanguageModelV1Middleware } from "ai"

API Signature

transformParams:

({ type: "generate" | "stream", params: LanguageModelV1CallOptions }) => Promise<LanguageModelV1CallOptions>
Transforms the parameters before they are passed to the language model.

wrapGenerate:

({ doGenerate: DoGenerateFunction, params: LanguageModelV1CallOptions, model: LanguageModelV1 }) => Promise<DoGenerateResult>
Wraps the generate operation of the language model.

wrapStream:

({ doStream: DoStreamFunction, params: LanguageModelV1CallOptions, model: LanguageModelV1 }) => Promise<DoStreamResult>
Wraps the stream operation of the language model.