gateway
gateway copied to clipboard
"Illegal invocation" error with Portkey SDK in Cloudflare Worker
What Happened?
Bug Description:
When using the portkey-ai SDK within a Cloudflare Worker, calls to portkey.chat.completions.create() result in a runtime error: {"error":"Server error","detail":"Illegal invocation: function called with incorrect \this` reference."}. This suggests a potential this` context issue within the SDK when operating in the Cloudflare Workers environment.
Environment:
- Portkey SDK Version:
[email protected] - Runtime: Cloudflare Worker (TypeScript)
Steps to Reproduce:
- Initialize the Portkey client within a Cloudflare Worker's
fetchhandler using API and Virtual Keys sourced from environment variables. - Construct a simple
messagesarray for the chat completion. - Call
await portkey.chat.completions.create({ model: "gpt-4o-mini", messages: [...] }). - The error occurs at this point.
Code Snippet (Illustrative):
import Portkey from 'portkey-ai';
// Define your Cloudflare Worker Environment interface
export interface Env {
PORTKEY_API_KEY: string; // Ensure these match your actual env var names
PORTKEY_VIRTUAL_KEY: string; // Ensure these match your actual env var names
// ... other env variables if relevant to the Portkey setup
}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
// Assuming the relevant logic is triggered by a POST request
if (request.method === 'POST') {
try {
const portkey = new Portkey({
apiKey: env.PORTKEY_API_KEY,
virtualKey: env.PORTKEY_VIRTUAL_KEY
});
const messages = [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Hello!" }
];
// This is where the error typically occurs
const completion = await portkey.chat.completions.create({
model: 'gpt-4o-mini', // Example model
messages: messages,
max_tokens: 100, // Example parameters
temperature: 0.7, // Example parameters
});
const messageContent = completion.choices?.[0]?.message?.content;
const result = typeof messageContent === 'string' ? messageContent.trim() : '';
return new Response(JSON.stringify({ result }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
} catch (e: any) {
console.error("Portkey SDK Error in Cloudflare Worker:", e.message, e.stack);
// The e.message is "Illegal invocation: function called with incorrect `this` reference."
return new Response(JSON.stringify({
error: 'Server error during Portkey call',
detail: e.message,
// Including e.stack is very helpful for debugging if available from logs
stack_trace: e.stack
}), {
status: 500,
headers: { 'Content-Type': 'application/json' },
});
}
}
// Fallback or other route handling
return new Response('Worker is running. Use POST for Portkey AI requests.');
},
};
What Should Have Happened?
The portkey.chat.completions.create() call should complete successfully without throwing an "Illegal invocation" error. The Cloudflare Worker should then return a 200 OK status with the chat completion response from the Portkey service, formatted as JSON. For example: {"result": "This is a sample response from the assistant."}.
Additional Notes:
The issue has been confirmed with [email protected]. The error appears to be independent of the specific model or messages used.
Any guidance on resolving this or workarounds for using the Portkey SDK within Cloudflare Workers would be appreciated.