-
Notifications
You must be signed in to change notification settings - Fork 3.9k
🐛 Bug: TypeError: 'beta' property undefined in OpenAIAdapter with openai v5.3.0 #1979
Description
♻️ Reproduction Steps
1. Node.js Environment:
Node version : v22.15.1
2. Project Setup:
Create a new Node.js project or use an existing one.
Install necessary dependencies and confirm installed versions: openai@5.3.0 and @copilotkit/runtime@1.8.14.:
npm install express cors openai@latest @copilotkit/runtime@latest dotenv
3. Environment Variables (.env file):
Create a .env file in your project root with the following:
Code snippet:
AZURE_OPENAI_BASE_URL=https://.openai.azure.com/
AZURE_OPENAI_API_KEY=YOUR_AZURE_OPENAI_API_KEY
AZURE_OPENAI_API_VERSION=2024-02-15-preview # Use your exact API version
PORT=5000
4. Server code
const express = require('express');
const app = express();
const { AzureOpenAI } = require('openai');
const { CopilotRuntime, OpenAIAdapter, copilotRuntimeNodeHttpEndpoint } = require('@copilotkit/runtime');
const END_POINT = process.env.AZURE_OPENAI_BASE_URL;
const DEPLOYMENT = "gpt-4o"; // Your Azure OpenAI deployment name for gpt-4o
const API_KEY = process.env.AZURE_OPENAI_API_KEY;
const API_VERSION = process.env.AZURE_OPENAI_API_VERSION;
const PORT = process.env.PORT || 3001;
const openai = new AzureOpenAI({
apiKey: API_KEY,
endpoint: END_POINT,
apiVersion: API_VERSION,
deployment: DEPLOYMENT,
});
const azureServiceAdapter = new OpenAIAdapter({ openai });
app.use('/copilotkit', async (req, res, next) => {
try {
const runtime = new CopilotRuntime();
const handler = copilotRuntimeNodeHttpEndpoint({
endpoint: '/copilotkit',
runtime,
serviceAdapter: azureServiceAdapter,
});
return handler(req, res);
} catch (error) {
console.error('Error in Copilotkit middleware:', error);
next(error);
}
});
app.listen(PORT, () => {
console.log(`Server running successfully on http://localhost:${PORT}/`);
});
5. Frontend Setup (Triggering the Endpoint):
Run the node server and trigger the endpoint "http://localhost:5000/copilotkit".
// Example in your React App:
<CopilotKit runtimeUrl="http://localhost:5000/copilotkit">
{/* Your app components */}
</CopilotKit>
✅ Expected Behavior
The CopilotKit runtime should successfully initialize, connect to the Azure OpenAI gpt-4o model via the openai v5.3.0 client, and process the request without encountering any internal errors, returning a valid response from the LLM.
❌ Actual Behavior
The server crashes and logs the errors, preventing interaction with the LLM. Error logs are in Logs section
Inspection of the referenced line (node_modules@copilotkit\runtime\dist\index.js:414:44)
This shows the code is attempting to call this.openai.beta.chat.completions.stream. The beta property is undefined because openai package versions 5.x.x no longer expose chat.completions.stream under a beta namespace; it's directly accessible under chat.completions.stream.
Temporary Workaround Implemented:
I have temporarily resolved this locally by manually editing node_modules@copilotkit\runtime\dist\index.js line 414 and changing this.openai.beta.chat.completions.stream to this.openai.chat.completions.stream. This workaround allows the application to function correctly, but it will be overwritten upon npm install or npm update.
For now able to get responses as expected from LLM.
𝌚 CopilotKit Version
@copilotkit/runtime@1.8.14📄 Logs (Optional)
[OpenAI] Error during API call: TypeError: Cannot read properties of undefined (reading 'completions')
at OpenAIAdapter.process (C:\Users\Nodeserver\node_modules\@copilotkit\runtime\dist\index.js:414:44)
at CopilotRuntime.processRuntimeRequest (C:\Users\Nodeserver\node_modules\node_modules\@copilotkit\runtime\dist\index.js:5105:43)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async CopilotResolver.generateCopilotResponse (C:\Users\Nodeserver\node_modules\node_modules\@copilotkit\runtime\dist\index.js:6280:138)
Error getting response: TypeError: Cannot read properties of undefined (reading 'completions')
at OpenAIAdapter.process (C:\Users\Nodeserver\node_modules\node_modules\@copilotkit\runtime\dist\index.js:414:44)
at CopilotRuntime.processRuntimeRequest (C:\Users\Nodeserver\node_modules\node_modules\@copilotkit\runtime\dist\index.js:5105:43)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async CopilotResolver.generateCopilotResponse (C:\Users\Nodeserver\node_modules\node_modules\@copilotkit\runtime\dist\index.js:6280:138)