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

Can not use OpenAI SDK with Sentry Node agent: TypeError: getDefaultAgent is not a function #1010

Open
1 task done
keithwhor opened this issue Aug 25, 2024 · 5 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@keithwhor
Copy link

keithwhor commented Aug 25, 2024

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • This is an issue with the Node library

Describe the bug

Referenced previously here, closed without resolution: #903

This is a pretty big issue as it prevents usage of the SDK while using the latest Sentry monitoring package.

To Reproduce

  1. Install Sentry Node sdk via npm i @sentry/node --save
  2. Enter the following code;
import * as Sentry from '@sentry/node';

// Start Sentry
  Sentry.init({
    dsn: "https://your-sentry-url",
    environment: "your-env",
    tracesSampleRate: 1.0, //  Capture 100% of the transactions
  });
  1. Try to create a completion somewhere in the process after Sentry has been initialized:
const params = {
  model: model,
  stream: true,
  stream_options: {
    include_usage: true
  },
  messages
};
const completion = await openai.chat.completions.create(params);

Results in error:

TypeError: getDefaultAgent is not a function
    at OpenAI.buildRequest (file:///my-project/node_modules/openai/core.mjs:208:66)
    at OpenAI.makeRequest (file:///my-project/node_modules/openai/core.mjs:279:44)

Code snippets

(Included)

OS

All operating systems (macOS, Linux)

Node version

v20.10.0

Library version

v4.56.0

@rattrayalex
Copy link
Collaborator

I definitely agree this is not a good situation.

It is essentially a Sentry bug, and the workaround is documented in associated GitHub issue on their package:

The workaround for this issue for now is to exclude openai from being instrumented via the registerEsmLoaderHooks init option:

import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'DSN',
registerEsmLoaderHooks: { exclude: [/openai/] },
})
If you're using @sentry/node/preload you'll need to create your own preload file to pass this option: preload.mjs

import * as Sentry from '@sentry/node';

Sentry.preloadOpenTelemetry({
registerEsmLoaderHooks: { exclude: [/openai/] },
})

node --import ./preload.mjs ./my-app.mjs

which is confirmed by other users to fix the stacktrace reported here.

For more background, both Sentry and Datadog make use of import-in-the-middle, which is causing this issue. According to a contributor to import-in-the-middle, this is due to a fundamental flaw in that library.

A more general workaround was documented in the other thread, but I believe it may need to be implemented at the library level, so I shared details in the associated Datadog issue as well.

Note that I expect this problem to go away in the next major version of this library, when we switch from node-fetch to the built-in fetch and can remove most of these shims, which pretty fundamentally conflict with the underlying bug in import-in-the-middle. I'm hoping that happens within a month or so.

@rattrayalex rattrayalex closed this as not planned Won't fix, can't repro, duplicate, stale Aug 25, 2024
@rattrayalex
Copy link
Collaborator

Actually, I'll keep this issue open until the next major version, as a reminder to double-check that it does indeed resolve this problem.

@rattrayalex rattrayalex reopened this Aug 25, 2024
@keithwhor
Copy link
Author

Thanks for the quick response! And thanks for leaving this open, issue #903 was the first thing that popped up when googling so I think it'll be helpful for folks to see there's a resolution available from this library. Looking forward to the next version!

@BlazingTide
Copy link

Is there a way to resolve this problem?

@keithwhor
Copy link
Author

keithwhor commented Aug 25, 2024

As per Alex's comment from the Sentry package, this resolved the problem for me;

// Start Sentry
Sentry.init({
  dsn: "https://my-dsn",
  // add this to resolve bug
  registerEsmLoaderHooks: {
    exclude: [/openai/]
  }
});

mschfh added a commit to brave/cookiemonster that referenced this issue Nov 23, 2024
mschfh added a commit to brave/cookiemonster that referenced this issue Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants