Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with importing from @google/genai/node fails in Cloudflare Workers environment #324

Open
d3lm opened this issue Mar 19, 2025 · 3 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@d3lm
Copy link

d3lm commented Mar 19, 2025

Problem

When importing directly from @google/genai/node in a Cloudflare Worker, the application fails due to limited Node.js compatibility in the Workers environment.

The specific error occurs in the google-logging-utils package when checking if colors should be enabled. The function attempts to call stream.getColorDepth() on the proxied process.stderr object, resulting in:

Uncaught TypeError: Cannot convert object to primitive value

Investigation

The issue occurs in the isEnabled method of the Colours class in googleapis/gax-nodejs/logging-utils/src/colours.ts](https://github.com/googleapis/gax-nodejs/blob/main/logging-utils/src/colours.ts#L55) where it makes this check:

stream.getColorDepth() > 2

Since Cloudflare Workers only provide a polyfilled version of process.stderr using a Proxy, this function call fails.

Temporary Fix

I've temporarily patched the dependency by adding try/catch error handling:

@@ -28,10 +28,14 @@ class Colours {
      * @returns true if the stream should have colourization enabled
      */
     static isEnabled(stream) {
-        return (stream.isTTY &&
-            (typeof stream.getColorDepth === 'function'
-                ? stream.getColorDepth() > 2
-                : true));
+        try {
+            return (stream.isTTY &&
+                (typeof stream.getColorDepth === 'function'
+                    ? stream.getColorDepth() > 2
+                    : true));
+        } catch (error) {
+            return false;
+        }
     }
     static refresh() {
         Colours.enabled = Colours.isEnabled(process.stderr);

This patch gracefully handles the error by defaulting to disabled colors when the check fails.

@d3lm d3lm added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Mar 19, 2025
@amirh
Copy link
Collaborator

amirh commented Mar 25, 2025

I'm expecting more incompatibilities with the Cloudflare Workers js environment than just stream.getColorDepth.
I'm unfamiliar with Cloudflare Workers, is there a way to run their js runtime locally or on CI for testing/debugging without actually running a real worker?

Update: Gemini says miniflare

@amirh amirh added status:awaiting user response issues requiring a response from the user and removed status:awaiting user response issues requiring a response from the user labels Mar 25, 2025
@d3lm
Copy link
Author

d3lm commented Mar 25, 2025

Actually, we already use this in production and that was the only issue we ran into so far. But we had to patch that one dependency to make it work.

@d3lm
Copy link
Author

d3lm commented Mar 25, 2025

Yea, miniflare is with the older version of wrangler. These days, it uses workerd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants