gateway icon indicating copy to clipboard operation
gateway copied to clipboard

"Illegal invocation" error with Portkey SDK in Cloudflare Worker

Open x-creates opened this issue 6 months ago • 5 comments

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:

Steps to Reproduce:

  1. Initialize the Portkey client within a Cloudflare Worker's fetch handler using API and Virtual Keys sourced from environment variables.
  2. Construct a simple messages array for the chat completion.
  3. Call await portkey.chat.completions.create({ model: "gpt-4o-mini", messages: [...] }).
  4. 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.

x-creates avatar Jun 14 '25 13:06 x-creates

tagging @csgulati09 to take a look

narengogi avatar Jun 16 '25 14:06 narengogi

Hey!

I have identified the issue and trying to resolve it.

csgulati09 avatar Jun 17 '25 08:06 csgulati09

Quick check: when can this be merged? It's blocking me from trying the service

x-creates avatar Jun 23 '25 13:06 x-creates

Expect it to be merged within a day. Sorry for the delay.

csgulati09 avatar Jun 23 '25 13:06 csgulati09

We have made the Release v1.10.1 Can you update the package and verify?

csgulati09 avatar Jun 25 '25 09:06 csgulati09

Thanks for the fix. I have confirmed that it is now working.

x-creates avatar Jun 29 '25 02:06 x-creates