Skip to content

feat(ai): implement experimental_prepareStep for streamText(v4) #7390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: v4
Choose a base branch
from

Conversation

yaonyan
Copy link

@yaonyan yaonyan commented Jul 18, 2025

Background

While AI SDK v5 includes native prepareStep support for dynamic step-by-step configuration, it requires LanguageModelV2 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:

  1. Limited provider ecosystem: Many providers only support LanguageModelV1, including numerous third-party services that offer OpenAI-compatible APIs based on older interface standards
  2. Production stability requirements: v5 is still in beta with ongoing breaking changes
  3. Immediate business needs: Multi-step agent architectures are required today

This implementation backports the prepareStep functionality from v5 to v4, enabling advanced multi-step AI workflows with existing LanguageModelV1 providers.

Summary

This PR implements experimental_prepareStep for streamText in AI SDK v4, based on the v5 prepareStep design but adapted to work with LanguageModelV1 providers. The implementation includes:

Core Changes

  • New PrepareStepFunction type: Adapted from v5 implementation to work with LanguageModelV1
  • New PrepareStepResult type: Defines step-specific configuration options (model, toolChoice, activeTools, system, messages)
  • Enhanced generateText: Added messages as a new experimental_prepareStep parameter
  • Enhanced streamText: Added experimental_prepareStep support for streamText
  • New prepare-step.ts: Core types and interfaces following v5 patterns

Key Features (based on v5 design)

  • Dynamic model switching: Change language models between steps
  • Tool management: Customize available tools per step using experimental_activeTools
  • Prompt customization: Override system prompts and messages for specific steps
  • Step-aware configuration: Access to step history and current step number

Files Modified

  • packages/ai/core/generate-text/generate-text.ts: Added experimental_prepareStep integration
  • packages/ai/core/generate-text/stream-text.ts: Added experimental_prepareStep support + stepNumber fix
  • packages/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 coverage
  • packages/ai/core/generate-text/__snapshots__/stream-text.test.ts.snap: Updated snapshots

Verification

New test case added

yaonyan added 5 commits July 18, 2025 11:33
…ration

- Add PrepareStepFunction and PrepareStepResult types for step-by-step customization
- Enable dynamic model, toolChoice, and activeTools modification per step
- Support system prompt and messages override during multi-step execution
- Implement experimental_prepareStep parameter in generateText and streamText
- Add comprehensive test coverage for experimental_prepareStep functionality
- Fix stepNumber calculation bug in stream-text.ts (use currentStep instead of recordedSteps.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant