feat(ai): implement experimental_prepareStep for streamText(v4) #7390
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background
While AI SDK v5 includes native
prepareStep
support for dynamic step-by-step configuration, it requiresLanguageModelV2
interface which many AI providers haven't yet implemented. Production applications still rely on the stable v4 environment and cannot migrate to v5 due to:LanguageModelV1
, including numerous third-party services that offer OpenAI-compatible APIs based on older interface standardsThis implementation backports the
prepareStep
functionality from v5 to v4, enabling advanced multi-step AI workflows with existingLanguageModelV1
providers.Summary
This PR implements
experimental_prepareStep
forstreamText
in AI SDK v4, based on the v5prepareStep
design but adapted to work withLanguageModelV1
providers. The implementation includes:Core Changes
PrepareStepFunction
type: Adapted from v5 implementation to work with LanguageModelV1PrepareStepResult
type: Defines step-specific configuration options (model, toolChoice, activeTools, system, messages)generateText
: Addedmessages
as a new experimental_prepareStep parameterstreamText
: Added experimental_prepareStep support for streamTextprepare-step.ts
: Core types and interfaces following v5 patternsKey Features (based on v5 design)
experimental_activeTools
Files Modified
packages/ai/core/generate-text/generate-text.ts
: Added experimental_prepareStep integrationpackages/ai/core/generate-text/stream-text.ts
: Added experimental_prepareStep support + stepNumber fixpackages/ai/core/generate-text/prepare-step.ts
: New file with types (based on v5 implementation)packages/ai/core/generate-text/stream-text.test.ts
: Comprehensive test coveragepackages/ai/core/generate-text/__snapshots__/stream-text.test.ts.snap
: Updated snapshotsVerification
New test case added