addToolInputExamplesMiddleware
addToolInputExamplesMiddleware is a middleware function that appends input examples to tool descriptions. This is especially useful for language model providers that do not natively support the inputExamples property—the middleware serializes and injects the examples into the tool's description so models can learn from them.
Import
import { addToolInputExamplesMiddleware } from "ai"API
Signature
function addToolInputExamplesMiddleware(options?: { prefix?: string; format?: (example: { input: JSONObject }, index: number) => string; remove?: boolean;}): LanguageModelMiddleware;Parameters
prefix?:
format?:
remove?:
Returns
A LanguageModelMiddleware that:
- Locates function tools with an
inputExamplesproperty. - Serializes each input example (by default as JSON, or using your custom formatter).
- Prepends a section at the end of the tool description containing all formatted examples, prefixed by the
prefix. - Removes the
inputExamplesproperty from the tool (unlessremove: false). - Passes through all other tools (including those without examples) unchanged.
Usage Example
import { generateText, tool, wrapLanguageModel, addToolInputExamplesMiddleware,} from 'ai';import { openai } from '@ai-sdk/openai';import { z } from 'zod';
const model = wrapLanguageModel({ model: "anthropic/claude-sonnet-4.5", middleware: addToolInputExamplesMiddleware({ prefix: 'Input Examples:', format: (example, index) => `${index + 1}. ${JSON.stringify(example.input)}`, }),});
const result = await generateText({ model, tools: { weather: tool({ description: 'Get the weather in a location', inputSchema: z.object({ location: z.string() }), inputExamples: [ { input: { location: 'San Francisco' } }, { input: { location: 'London' } }, ], }), }, prompt: 'What is the weather in Tokyo?',});How It Works
-
For every function tool that defines
inputExamples, the middleware:-
Formats each example with the
formatfunction (default: JSON.stringify). -
Builds a section like:
Input Examples:{"location":"San Francisco"}{"location":"London"} -
Appends this section to the end of the tool's
description.
-
-
By default, it removes the
inputExamplesproperty after appending to prevent duplication (can be disabled withremove: false). -
Tools without input examples or non-function tools are left unmodified.
Tip: This middleware is especially useful with providers such as OpenAI or Anthropic, where native support for
inputExamplesis not available.
Example effect
If your original tool definition is:
{ type: 'function', name: 'weather', description: 'Get the weather in a location', inputSchema: { ... }, inputExamples: [ { input: { location: 'San Francisco' } }, { input: { location: 'London' } } ]}After applying the middleware (with default settings), the tool passed to the model will look like:
{ type: 'function', name: 'weather', description: `Get the weather in a location
Input Examples:{"location":"San Francisco"}{"location":"London"}`, inputSchema: { ... } // inputExamples is removed by default}