ai 6.0.31 → 6.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/00-introduction/index.mdx +76 -0
- package/docs/02-foundations/01-overview.mdx +43 -0
- package/docs/02-foundations/02-providers-and-models.mdx +163 -0
- package/docs/02-foundations/03-prompts.mdx +620 -0
- package/docs/02-foundations/04-tools.mdx +160 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/index.mdx +43 -0
- package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
- package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
- package/docs/02-getting-started/04-svelte.mdx +627 -0
- package/docs/02-getting-started/05-nuxt.mdx +566 -0
- package/docs/02-getting-started/06-nodejs.mdx +512 -0
- package/docs/02-getting-started/07-expo.mdx +766 -0
- package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
- package/docs/02-getting-started/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +367 -0
- package/docs/03-agents/03-workflows.mdx +370 -0
- package/docs/03-agents/04-loop-control.mdx +350 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/index.mdx +40 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
- package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/index.mdx +88 -0
- package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
- package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
- package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
- package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
- package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
- package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
- package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
- package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
- package/docs/04-ai-sdk-ui/index.mdx +64 -0
- package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
- package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
- package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
- package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
- package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
- package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
- package/docs/05-ai-sdk-rsc/index.mdx +58 -0
- package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
- package/docs/06-advanced/02-stopping-streams.mdx +184 -0
- package/docs/06-advanced/03-backpressure.mdx +173 -0
- package/docs/06-advanced/04-caching.mdx +169 -0
- package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
- package/docs/06-advanced/06-rate-limiting.mdx +60 -0
- package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
- package/docs/06-advanced/08-model-as-router.mdx +120 -0
- package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
- package/docs/06-advanced/09-sequential-generations.mdx +55 -0
- package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
- package/docs/06-advanced/index.mdx +11 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
- package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
- package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
- package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
- package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
- package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
- package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
- package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
- package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
- package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
- package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
- package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
- package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
- package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
- package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
- package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
- package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
- package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
- package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
- package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
- package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
- package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
- package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
- package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
- package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
- package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
- package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
- package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
- package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
- package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
- package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
- package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
- package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
- package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
- package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
- package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
- package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
- package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
- package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
- package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
- package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
- package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
- package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
- package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
- package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
- package/docs/07-reference/index.mdx +34 -0
- package/docs/08-migration-guides/00-versioning.mdx +46 -0
- package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
- package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
- package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
- package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
- package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
- package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
- package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
- package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
- package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
- package/docs/08-migration-guides/index.mdx +22 -0
- package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
- package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
- package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
- package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
- package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
- package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
- package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
- package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
- package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
- package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
- package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
- package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
- package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
- package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
- package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
- package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
- package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
- package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
- package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
- package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
- package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
- package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
- package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
- package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
- package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
- package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
- package/docs/09-troubleshooting/index.mdx +11 -0
- package/package.json +8 -4
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: LanguageModelV3Middleware
|
|
3
|
+
description: Middleware for enhancing language model behavior (API Reference)
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `LanguageModelV3Middleware`
|
|
7
|
+
|
|
8
|
+
<Note type="warning">
|
|
9
|
+
Language model middleware is an experimental feature.
|
|
10
|
+
</Note>
|
|
11
|
+
|
|
12
|
+
Language model middleware provides a way to enhance the behavior of language models
|
|
13
|
+
by intercepting and modifying the calls to the language model. It can be used to add
|
|
14
|
+
features like guardrails, RAG, caching, and logging in a language model agnostic way.
|
|
15
|
+
|
|
16
|
+
See [Language Model Middleware](/docs/ai-sdk-core/middleware) for more information.
|
|
17
|
+
|
|
18
|
+
## Import
|
|
19
|
+
|
|
20
|
+
<Snippet
|
|
21
|
+
text={`import { LanguageModelV3Middleware } from "ai"`}
|
|
22
|
+
prompt={false}
|
|
23
|
+
/>
|
|
24
|
+
|
|
25
|
+
## API Signature
|
|
26
|
+
|
|
27
|
+
<PropertiesTable
|
|
28
|
+
content={[
|
|
29
|
+
{
|
|
30
|
+
name: 'transformParams',
|
|
31
|
+
type: '({ type: "generate" | "stream", params: LanguageModelV3CallOptions }) => Promise<LanguageModelV3CallOptions>',
|
|
32
|
+
description:
|
|
33
|
+
'Transforms the parameters before they are passed to the language model.',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
name: 'wrapGenerate',
|
|
37
|
+
type: '({ doGenerate: DoGenerateFunction, params: LanguageModelV3CallOptions, model: LanguageModelV3 }) => Promise<DoGenerateResult>',
|
|
38
|
+
description: 'Wraps the generate operation of the language model.',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'wrapStream',
|
|
42
|
+
type: '({ doStream: DoStreamFunction, params: LanguageModelV3CallOptions, model: LanguageModelV3 }) => Promise<DoStreamResult>',
|
|
43
|
+
description: 'Wraps the stream operation of the language model.',
|
|
44
|
+
},
|
|
45
|
+
]}
|
|
46
|
+
/>
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: extractReasoningMiddleware
|
|
3
|
+
description: Middleware that extracts XML-tagged reasoning sections from generated text
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `extractReasoningMiddleware()`
|
|
7
|
+
|
|
8
|
+
`extractReasoningMiddleware` is a middleware function that extracts XML-tagged reasoning sections from generated text and exposes them separately from the main text content. This is particularly useful when you want to separate an AI model's reasoning process from its final output.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { extractReasoningMiddleware } from 'ai';
|
|
12
|
+
|
|
13
|
+
const middleware = extractReasoningMiddleware({
|
|
14
|
+
tagName: 'reasoning',
|
|
15
|
+
separator: '\n',
|
|
16
|
+
});
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Import
|
|
20
|
+
|
|
21
|
+
<Snippet
|
|
22
|
+
text={`import { extractReasoningMiddleware } from "ai"`}
|
|
23
|
+
prompt={false}
|
|
24
|
+
/>
|
|
25
|
+
|
|
26
|
+
## API Signature
|
|
27
|
+
|
|
28
|
+
### Parameters
|
|
29
|
+
|
|
30
|
+
<PropertiesTable
|
|
31
|
+
content={[
|
|
32
|
+
{
|
|
33
|
+
name: 'tagName',
|
|
34
|
+
type: 'string',
|
|
35
|
+
isOptional: false,
|
|
36
|
+
description:
|
|
37
|
+
'The name of the XML tag to extract reasoning from (without angle brackets)',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'separator',
|
|
41
|
+
type: 'string',
|
|
42
|
+
isOptional: true,
|
|
43
|
+
description:
|
|
44
|
+
'The separator to use between reasoning and text sections. Defaults to "\\n"',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'startWithReasoning',
|
|
48
|
+
type: 'boolean',
|
|
49
|
+
isOptional: true,
|
|
50
|
+
description:
|
|
51
|
+
'Starts with reasoning tokens. Set to true when the response always starts with reasoning and the initial tag is omitted. Defaults to false.',
|
|
52
|
+
},
|
|
53
|
+
]}
|
|
54
|
+
/>
|
|
55
|
+
|
|
56
|
+
### Returns
|
|
57
|
+
|
|
58
|
+
Returns a middleware object that:
|
|
59
|
+
|
|
60
|
+
- Processes both streaming and non-streaming responses
|
|
61
|
+
- Extracts content between specified XML tags as reasoning
|
|
62
|
+
- Removes the XML tags and reasoning from the main text
|
|
63
|
+
- Adds a `reasoning` property to the result containing the extracted content
|
|
64
|
+
- Maintains proper separation between text sections using the specified separator
|
|
65
|
+
|
|
66
|
+
### Type Parameters
|
|
67
|
+
|
|
68
|
+
The middleware works with the `LanguageModelV3StreamPart` type for streaming responses.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: simulateStreamingMiddleware
|
|
3
|
+
description: Middleware that simulates streaming for non-streaming language models
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `simulateStreamingMiddleware()`
|
|
7
|
+
|
|
8
|
+
`simulateStreamingMiddleware` is a middleware function that simulates streaming behavior with responses from non-streaming language models. This is useful when you want to maintain a consistent streaming interface even when using models that only provide complete responses.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { simulateStreamingMiddleware } from 'ai';
|
|
12
|
+
|
|
13
|
+
const middleware = simulateStreamingMiddleware();
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Import
|
|
17
|
+
|
|
18
|
+
<Snippet
|
|
19
|
+
text={`import { simulateStreamingMiddleware } from "ai"`}
|
|
20
|
+
prompt={false}
|
|
21
|
+
/>
|
|
22
|
+
|
|
23
|
+
## API Signature
|
|
24
|
+
|
|
25
|
+
### Parameters
|
|
26
|
+
|
|
27
|
+
This middleware doesn't accept any parameters.
|
|
28
|
+
|
|
29
|
+
### Returns
|
|
30
|
+
|
|
31
|
+
Returns a middleware object that:
|
|
32
|
+
|
|
33
|
+
- Takes a complete response from a language model
|
|
34
|
+
- Converts it into a simulated stream of chunks
|
|
35
|
+
- Properly handles various response components including:
|
|
36
|
+
- Text content
|
|
37
|
+
- Reasoning (as string or array of objects)
|
|
38
|
+
- Tool calls
|
|
39
|
+
- Metadata and usage information
|
|
40
|
+
- Warnings
|
|
41
|
+
|
|
42
|
+
### Usage Example
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { streamText } from 'ai';
|
|
46
|
+
import { wrapLanguageModel } from 'ai';
|
|
47
|
+
import { simulateStreamingMiddleware } from 'ai';
|
|
48
|
+
|
|
49
|
+
// Example with a non-streaming model
|
|
50
|
+
const result = streamText({
|
|
51
|
+
model: wrapLanguageModel({
|
|
52
|
+
model: nonStreamingModel,
|
|
53
|
+
middleware: simulateStreamingMiddleware(),
|
|
54
|
+
}),
|
|
55
|
+
prompt: 'Your prompt here',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Now you can use the streaming interface
|
|
59
|
+
for await (const chunk of result.fullStream) {
|
|
60
|
+
// Process streaming chunks
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## How It Works
|
|
65
|
+
|
|
66
|
+
The middleware:
|
|
67
|
+
|
|
68
|
+
1. Awaits the complete response from the language model
|
|
69
|
+
2. Creates a `ReadableStream` that emits chunks in the correct sequence
|
|
70
|
+
3. Simulates streaming by breaking down the response into appropriate chunk types
|
|
71
|
+
4. Preserves all metadata, reasoning, tool calls, and other response properties
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: defaultSettingsMiddleware
|
|
3
|
+
description: Middleware that applies default settings for language models
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `defaultSettingsMiddleware()`
|
|
7
|
+
|
|
8
|
+
`defaultSettingsMiddleware` is a middleware function that applies default settings to language model calls. This is useful when you want to establish consistent default parameters across multiple model invocations.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { defaultSettingsMiddleware } from 'ai';
|
|
12
|
+
|
|
13
|
+
const middleware = defaultSettingsMiddleware({
|
|
14
|
+
settings: {
|
|
15
|
+
temperature: 0.7,
|
|
16
|
+
maxOutputTokens: 1000,
|
|
17
|
+
// other settings...
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Import
|
|
23
|
+
|
|
24
|
+
<Snippet
|
|
25
|
+
text={`import { defaultSettingsMiddleware } from "ai"`}
|
|
26
|
+
prompt={false}
|
|
27
|
+
/>
|
|
28
|
+
|
|
29
|
+
## API Signature
|
|
30
|
+
|
|
31
|
+
### Parameters
|
|
32
|
+
|
|
33
|
+
The middleware accepts a configuration object with the following properties:
|
|
34
|
+
|
|
35
|
+
- `settings`: An object containing default parameter values to apply to language model calls. These can include any valid `LanguageModelV3CallOptions` properties and optional provider metadata.
|
|
36
|
+
|
|
37
|
+
### Returns
|
|
38
|
+
|
|
39
|
+
Returns a middleware object that:
|
|
40
|
+
|
|
41
|
+
- Merges the default settings with the parameters provided in each model call
|
|
42
|
+
- Ensures that explicitly provided parameters take precedence over defaults
|
|
43
|
+
- Merges provider metadata objects
|
|
44
|
+
|
|
45
|
+
### Usage Example
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { streamText, wrapLanguageModel, defaultSettingsMiddleware } from 'ai';
|
|
49
|
+
|
|
50
|
+
// Create a model with default settings
|
|
51
|
+
const modelWithDefaults = wrapLanguageModel({
|
|
52
|
+
model: gateway('anthropic/claude-sonnet-4.5'),
|
|
53
|
+
middleware: defaultSettingsMiddleware({
|
|
54
|
+
settings: {
|
|
55
|
+
providerOptions: {
|
|
56
|
+
openai: {
|
|
57
|
+
reasoningEffort: 'high',
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Use the model - default settings will be applied
|
|
65
|
+
const result = await streamText({
|
|
66
|
+
model: modelWithDefaults,
|
|
67
|
+
prompt: 'Your prompt here',
|
|
68
|
+
// These parameters will override the defaults
|
|
69
|
+
temperature: 0.8,
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## How It Works
|
|
74
|
+
|
|
75
|
+
The middleware:
|
|
76
|
+
|
|
77
|
+
1. Takes a set of default settings as configuration
|
|
78
|
+
2. Merges these defaults with the parameters provided in each model call
|
|
79
|
+
3. Ensures that explicitly provided parameters take precedence over defaults
|
|
80
|
+
4. Merges provider metadata objects from both sources
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: addToolInputExamplesMiddleware
|
|
3
|
+
description: Middleware that appends tool input examples to tool descriptions.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `addToolInputExamplesMiddleware`
|
|
7
|
+
|
|
8
|
+
`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.
|
|
9
|
+
|
|
10
|
+
## Import
|
|
11
|
+
|
|
12
|
+
<Snippet
|
|
13
|
+
text={`import { addToolInputExamplesMiddleware } from "ai"`}
|
|
14
|
+
prompt={false}
|
|
15
|
+
/>
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
### Signature
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
function addToolInputExamplesMiddleware(options?: {
|
|
23
|
+
prefix?: string;
|
|
24
|
+
format?: (example: { input: JSONObject }, index: number) => string;
|
|
25
|
+
remove?: boolean;
|
|
26
|
+
}): LanguageModelMiddleware;
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Parameters
|
|
30
|
+
|
|
31
|
+
<PropertiesTable
|
|
32
|
+
content={[
|
|
33
|
+
{
|
|
34
|
+
name: 'prefix',
|
|
35
|
+
type: 'string',
|
|
36
|
+
isOptional: true,
|
|
37
|
+
description:
|
|
38
|
+
"A prefix prepended before the input examples section. Defaults to `'Input Examples:'`.",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'format',
|
|
42
|
+
type: '(example: { input: JSONObject }, index: number) => string',
|
|
43
|
+
isOptional: true,
|
|
44
|
+
description:
|
|
45
|
+
'Optional custom formatter for each example. Receives the example object and its index. Default: JSON.stringify(example.input).',
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: 'remove',
|
|
49
|
+
type: 'boolean',
|
|
50
|
+
isOptional: true,
|
|
51
|
+
description:
|
|
52
|
+
'Whether to remove the `inputExamples` property from the tool after adding them to the description. Default: true.',
|
|
53
|
+
},
|
|
54
|
+
]}
|
|
55
|
+
/>
|
|
56
|
+
|
|
57
|
+
### Returns
|
|
58
|
+
|
|
59
|
+
A [LanguageModelMiddleware](/docs/03-ai-sdk-core/40-middleware) that:
|
|
60
|
+
|
|
61
|
+
- Locates function tools with an `inputExamples` property.
|
|
62
|
+
- Serializes each input example (by default as JSON, or using your custom formatter).
|
|
63
|
+
- Prepends a section at the end of the tool description containing all formatted examples, prefixed by the `prefix`.
|
|
64
|
+
- Removes the `inputExamples` property from the tool (unless `remove: false`).
|
|
65
|
+
- Passes through all other tools (including those without examples) unchanged.
|
|
66
|
+
|
|
67
|
+
## Usage Example
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import {
|
|
71
|
+
generateText,
|
|
72
|
+
tool,
|
|
73
|
+
wrapLanguageModel,
|
|
74
|
+
addToolInputExamplesMiddleware,
|
|
75
|
+
} from 'ai';
|
|
76
|
+
import { openai } from '@ai-sdk/openai';
|
|
77
|
+
import { z } from 'zod';
|
|
78
|
+
|
|
79
|
+
const model = wrapLanguageModel({
|
|
80
|
+
model: __MODEL__,
|
|
81
|
+
middleware: addToolInputExamplesMiddleware({
|
|
82
|
+
prefix: 'Input Examples:',
|
|
83
|
+
format: (example, index) =>
|
|
84
|
+
`${index + 1}. ${JSON.stringify(example.input)}`,
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const result = await generateText({
|
|
89
|
+
model,
|
|
90
|
+
tools: {
|
|
91
|
+
weather: tool({
|
|
92
|
+
description: 'Get the weather in a location',
|
|
93
|
+
inputSchema: z.object({ location: z.string() }),
|
|
94
|
+
inputExamples: [
|
|
95
|
+
{ input: { location: 'San Francisco' } },
|
|
96
|
+
{ input: { location: 'London' } },
|
|
97
|
+
],
|
|
98
|
+
}),
|
|
99
|
+
},
|
|
100
|
+
prompt: 'What is the weather in Tokyo?',
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## How It Works
|
|
105
|
+
|
|
106
|
+
1. For every function tool that defines `inputExamples`, the middleware:
|
|
107
|
+
|
|
108
|
+
- Formats each example with the `format` function (default: JSON.stringify).
|
|
109
|
+
- Builds a section like:
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
Input Examples:
|
|
113
|
+
{"location":"San Francisco"}
|
|
114
|
+
{"location":"London"}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
- Appends this section to the end of the tool's `description`.
|
|
118
|
+
|
|
119
|
+
2. By default, it removes the `inputExamples` property after appending to prevent duplication (can be disabled with `remove: false`).
|
|
120
|
+
3. Tools without input examples or non-function tools are left unmodified.
|
|
121
|
+
|
|
122
|
+
> **Tip:** This middleware is especially useful with providers such as OpenAI or Anthropic, where native support for `inputExamples` is not available.
|
|
123
|
+
|
|
124
|
+
## Example effect
|
|
125
|
+
|
|
126
|
+
If your original tool definition is:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
{
|
|
130
|
+
type: 'function',
|
|
131
|
+
name: 'weather',
|
|
132
|
+
description: 'Get the weather in a location',
|
|
133
|
+
inputSchema: { ... },
|
|
134
|
+
inputExamples: [
|
|
135
|
+
{ input: { location: 'San Francisco' } },
|
|
136
|
+
{ input: { location: 'London' } }
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
After applying the middleware (with default settings), the tool passed to the model will look like:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
{
|
|
145
|
+
type: 'function',
|
|
146
|
+
name: 'weather',
|
|
147
|
+
description: `Get the weather in a location
|
|
148
|
+
|
|
149
|
+
Input Examples:
|
|
150
|
+
{"location":"San Francisco"}
|
|
151
|
+
{"location":"London"}`,
|
|
152
|
+
inputSchema: { ... }
|
|
153
|
+
// inputExamples is removed by default
|
|
154
|
+
}
|
|
155
|
+
```
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: extractJsonMiddleware
|
|
3
|
+
description: Middleware that extracts JSON from text content by stripping markdown code fences
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `extractJsonMiddleware()`
|
|
7
|
+
|
|
8
|
+
`extractJsonMiddleware` is a middleware function that extracts JSON from text content by stripping markdown code fences and other formatting. This is useful when using `Output.object()` with models that wrap JSON responses in markdown code blocks (e.g., ` ```json ... ``` `).
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { extractJsonMiddleware } from 'ai';
|
|
12
|
+
|
|
13
|
+
const middleware = extractJsonMiddleware();
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Import
|
|
17
|
+
|
|
18
|
+
<Snippet text={`import { extractJsonMiddleware } from "ai"`} prompt={false} />
|
|
19
|
+
|
|
20
|
+
## API Signature
|
|
21
|
+
|
|
22
|
+
### Parameters
|
|
23
|
+
|
|
24
|
+
<PropertiesTable
|
|
25
|
+
content={[
|
|
26
|
+
{
|
|
27
|
+
name: 'transform',
|
|
28
|
+
type: '(text: string) => string',
|
|
29
|
+
isOptional: true,
|
|
30
|
+
description:
|
|
31
|
+
'Custom transform function to apply to text content. Receives the raw text and should return the transformed text. If not provided, the default transform strips markdown code fences.',
|
|
32
|
+
},
|
|
33
|
+
]}
|
|
34
|
+
/>
|
|
35
|
+
|
|
36
|
+
### Returns
|
|
37
|
+
|
|
38
|
+
Returns a middleware object that:
|
|
39
|
+
|
|
40
|
+
- Processes both streaming and non-streaming responses
|
|
41
|
+
- Strips markdown code fences (` ```json ` and ` ``` `) from text content
|
|
42
|
+
- Applies custom transformations when a `transform` function is provided
|
|
43
|
+
- Maintains proper streaming behavior with efficient buffering
|
|
44
|
+
|
|
45
|
+
## Usage Examples
|
|
46
|
+
|
|
47
|
+
### Basic Usage
|
|
48
|
+
|
|
49
|
+
Strip markdown code fences from model responses when using structured output:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import {
|
|
53
|
+
generateText,
|
|
54
|
+
wrapLanguageModel,
|
|
55
|
+
extractJsonMiddleware,
|
|
56
|
+
Output,
|
|
57
|
+
} from 'ai';
|
|
58
|
+
import { z } from 'zod';
|
|
59
|
+
|
|
60
|
+
const result = await generateText({
|
|
61
|
+
model: wrapLanguageModel({
|
|
62
|
+
model: yourModel,
|
|
63
|
+
middleware: extractJsonMiddleware(),
|
|
64
|
+
}),
|
|
65
|
+
output: Output.object({
|
|
66
|
+
schema: z.object({
|
|
67
|
+
recipe: z.object({
|
|
68
|
+
name: z.string(),
|
|
69
|
+
steps: z.array(z.string()),
|
|
70
|
+
}),
|
|
71
|
+
}),
|
|
72
|
+
}),
|
|
73
|
+
prompt: 'Generate a lasagna recipe.',
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
console.log(result.output);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### With Streaming
|
|
80
|
+
|
|
81
|
+
The middleware also works with streaming responses:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import {
|
|
85
|
+
streamText,
|
|
86
|
+
wrapLanguageModel,
|
|
87
|
+
extractJsonMiddleware,
|
|
88
|
+
Output,
|
|
89
|
+
} from 'ai';
|
|
90
|
+
import { z } from 'zod';
|
|
91
|
+
|
|
92
|
+
const { partialOutputStream } = streamText({
|
|
93
|
+
model: wrapLanguageModel({
|
|
94
|
+
model: yourModel,
|
|
95
|
+
middleware: extractJsonMiddleware(),
|
|
96
|
+
}),
|
|
97
|
+
output: Output.object({
|
|
98
|
+
schema: z.object({
|
|
99
|
+
recipe: z.object({
|
|
100
|
+
ingredients: z.array(z.string()),
|
|
101
|
+
steps: z.array(z.string()),
|
|
102
|
+
}),
|
|
103
|
+
}),
|
|
104
|
+
}),
|
|
105
|
+
prompt: 'Generate a detailed recipe.',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
for await (const partialObject of partialOutputStream) {
|
|
109
|
+
console.log(partialObject);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Custom Transform Function
|
|
114
|
+
|
|
115
|
+
For models that use different formatting, you can provide a custom transform:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { extractJsonMiddleware } from 'ai';
|
|
119
|
+
|
|
120
|
+
const middleware = extractJsonMiddleware({
|
|
121
|
+
transform: text =>
|
|
122
|
+
text
|
|
123
|
+
.replace(/^PREFIX/, '')
|
|
124
|
+
.replace(/SUFFIX$/, '')
|
|
125
|
+
.trim(),
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## How It Works
|
|
130
|
+
|
|
131
|
+
The middleware handles text content in two ways:
|
|
132
|
+
|
|
133
|
+
### Non-Streaming (generateText)
|
|
134
|
+
|
|
135
|
+
1. Receives the complete response from the model
|
|
136
|
+
2. Applies the transform function to strip markdown fences (or custom formatting)
|
|
137
|
+
3. Returns the cleaned text content
|
|
138
|
+
|
|
139
|
+
### Streaming (streamText)
|
|
140
|
+
|
|
141
|
+
1. Buffers initial content to detect markdown fence prefixes (` ```json\n `)
|
|
142
|
+
2. If a fence is detected, strips the prefix and switches to streaming mode
|
|
143
|
+
3. Maintains a small suffix buffer to handle the closing fence (` \n``` `)
|
|
144
|
+
4. When the stream ends, strips any trailing fence from the buffer
|
|
145
|
+
5. For custom transforms, buffers all content and applies the transform at the end
|
|
146
|
+
|
|
147
|
+
This approach ensures efficient streaming while correctly handling code fences that may be split across multiple chunks.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: stepCountIs
|
|
3
|
+
description: API Reference for stepCountIs.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `stepCountIs()`
|
|
7
|
+
|
|
8
|
+
Creates a stop condition that stops when the number of steps reaches a specified count.
|
|
9
|
+
|
|
10
|
+
This function is used with `stopWhen` in `generateText` and `streamText` to control when a tool-calling loop should stop based on the number of steps executed.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { generateText, stepCountIs } from 'ai';
|
|
14
|
+
__PROVIDER_IMPORT__;
|
|
15
|
+
|
|
16
|
+
const result = await generateText({
|
|
17
|
+
model: __MODEL__,
|
|
18
|
+
tools: {
|
|
19
|
+
// your tools
|
|
20
|
+
},
|
|
21
|
+
// Stop after 5 steps
|
|
22
|
+
stopWhen: stepCountIs(5),
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Import
|
|
27
|
+
|
|
28
|
+
<Snippet text={`import { stepCountIs } from "ai"`} prompt={false} />
|
|
29
|
+
|
|
30
|
+
## API Signature
|
|
31
|
+
|
|
32
|
+
### Parameters
|
|
33
|
+
|
|
34
|
+
<PropertiesTable
|
|
35
|
+
content={[
|
|
36
|
+
{
|
|
37
|
+
name: 'count',
|
|
38
|
+
type: 'number',
|
|
39
|
+
description:
|
|
40
|
+
'The maximum number of steps to execute before stopping the tool-calling loop.',
|
|
41
|
+
},
|
|
42
|
+
]}
|
|
43
|
+
/>
|
|
44
|
+
|
|
45
|
+
### Returns
|
|
46
|
+
|
|
47
|
+
A `StopCondition` function that returns `true` when the step count reaches the specified number. The function can be used with the `stopWhen` parameter in `generateText` and `streamText`.
|
|
48
|
+
|
|
49
|
+
## Examples
|
|
50
|
+
|
|
51
|
+
### Basic Usage
|
|
52
|
+
|
|
53
|
+
Stop after 3 steps:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { generateText, stepCountIs } from 'ai';
|
|
57
|
+
|
|
58
|
+
const result = await generateText({
|
|
59
|
+
model: yourModel,
|
|
60
|
+
tools: yourTools,
|
|
61
|
+
stopWhen: stepCountIs(3),
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Combining with Other Conditions
|
|
66
|
+
|
|
67
|
+
You can combine multiple stop conditions in an array:
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { generateText, stepCountIs, hasToolCall } from 'ai';
|
|
71
|
+
|
|
72
|
+
const result = await generateText({
|
|
73
|
+
model: yourModel,
|
|
74
|
+
tools: yourTools,
|
|
75
|
+
// Stop after 10 steps OR when finalAnswer tool is called
|
|
76
|
+
stopWhen: [stepCountIs(10), hasToolCall('finalAnswer')],
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## See also
|
|
81
|
+
|
|
82
|
+
- [`hasToolCall()`](/docs/reference/ai-sdk-core/has-tool-call)
|
|
83
|
+
- [`generateText()`](/docs/reference/ai-sdk-core/generate-text)
|
|
84
|
+
- [`streamText()`](/docs/reference/ai-sdk-core/stream-text)
|