`/var/task/node_modules/openai/_shims/agent-node.mjs` import issue in T3 stack with Vercel Edge function
Problem
Confirm this is a Node library issue and not an underlying OpenAI API issue - [X] This is an issue with the Node library Describe the bug The SDK throws an import error with the default T3 stack config (https://create.t3.gg/) in Edge functions with the `pages` router. First reported in LangChainJS, reproed by me with the SDK by itself: https://github.com/hwchase17/langchainjs/issues/2558 Here is the trace: [code block] To Reproduce 1. Set up an app using the T3 Stack: https://create.t3.gg/ 2. Set up an endpoint under `pages/api/route.ts` 3. Import and use the OpenAI SDK (code below) 4. Deploy to Vercel 5. Ping the endpoint Code snippets [code block] OS Vercel Edge function Node version Edge Library version openai v4.5.0
Error Output
error with the default T3 stack config (https://create.t3.gg/) in Edge functions with the `pages` router.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix OpenAI SDK Import Error in Vercel Edge Functions
The OpenAI SDK relies on Node.js features that are not fully supported in the Vercel Edge runtime. Specifically, the import syntax and module resolution used in the SDK conflicts with the Edge environment, leading to import errors when deploying applications using the T3 stack.
Awaiting Verification
Be the first to verify this fix
- 1
Update OpenAI SDK
Check for the latest version of the OpenAI SDK that may have addressed compatibility issues with Edge functions. Update the SDK in your project.
bashnpm install openai@latest - 2
Use Dynamic Imports
Modify your code to use dynamic imports for the OpenAI SDK. This can help bypass the static import issues in the Edge environment.
typescriptconst { Configuration, OpenAIApi } = await import('openai'); - 3
Check for Edge Compatibility
Review the OpenAI SDK documentation for any notes on Edge compatibility. Ensure that you are using features supported by the Edge runtime.
- 4
Test Locally with Edge Emulation
Before deploying, test your application locally using Vercel CLI to emulate the Edge environment. This can help identify issues before deployment.
bashvercel dev - 5
Deploy and Monitor
After making the changes, deploy your application to Vercel and monitor the logs for any import errors or runtime issues.
bashvercel deploy
Validation
Confirm the fix by deploying the application and pinging the endpoint. Ensure that the OpenAI SDK functions correctly without throwing import errors. Check the Vercel logs for any remaining issues.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep