Work with Vercel AI SDK
Upstash Workflow integrates with the Vercel AI SDK to provide durable and reliable AI applications. This allows you to:
- Build resilient AI applications with automatic retries
- Manage AI operations with workflow steps
- Implement tools and function calling with durability
- Handle errors gracefully across your AI operations
- Handle long-running AI operations with extended timeouts
This guide will walk you through setting up and implementing AI features using Upstash Workflow’s durability guarantees with Vercel AI SDK’s capabilities.
Prerequisites
Before getting started, make sure you have:
- An OpenAI API key
- Basic familiarity with Upstash Workflow and Vercel AI SDK
- Vercel AI SDK version 4.0.12 or higher (required for ToolExecutionError handling)
Installation
Install the required packages:
Implementation
Creating OpenAI client
AI SDKs (Vercel AI SDK, OpenAI SDK etc.) uses the client’s default fetch implementation to make API requests, but allows you to provide a custom fetch implementation.
In the case of Upstash Workflow, we need to use the context.call
method to make HTTP requests. We can create a custom fetch implementation that uses context.call
to make requests. By using context.call
, Upstash Workflow is the one making the HTTP request and waiting for the response, even if it takes too long to receive response from the LLM.
The following code snippet can also be generalized to work with other LLM SDKs, such as Anthropic or Google.
Using OpenAI client to generate text
Now that we’ve created the OpenAI client, we can use it to generate the text.
For that, we’re going to create a new workflow endpoint that uses the payload as prompt to generate text using the OpenAI client.
We can either run the app locally or deploy it. Once the app is running, we can trigger the workflow using the following code:
The workflow will execute, and we can view the logs in the Workflow dashboard:
Advanced Implementation with Tools
Tools allow the AI model to perform specific actions during text generation. You can learn more about tools in the Vercel AI SDK documentation.
When using tools with Upstash Workflow, each tool execution must be wrapped in a workflow step.
The maxSteps
parameter must be greater than 1 when using tools to allow the model to process tool results and generate final responses. See the tool steps documentation for detailed explanation.
When called with the same prompt as above, we will see the following logs:
Important Considerations
When using Upstash Workflow with the Vercel AI SDK, there are several critical requirements that must be followed:
Step Execution Order
The most critical requirement is that generateText
cannot be called before any workflow step. Always have a step before generateText
. This could be a step which gets the prompt:
Error Handling Pattern
You must use the following error handling pattern exactly as shown. The conditions and their handling should not be modified:
Tool Implementation
When implementing tools:
- Each tool’s
execute
function must be wrapped in acontext.run()
call - Tool steps should have descriptive names for tracking
- Tools must follow the same error handling pattern as above
Example:
Was this page helpful?