openai/openai-node

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

keithwhor opened this issue · 5 comments

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

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.

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.

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!

Is there a way to resolve this problem?

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/]
  }
});