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

Introduce failing test to recreate openai shim issue #113

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ericallam
Copy link

@ericallam ericallam commented Jun 20, 2024

Using iitm 1.8.1 in an ESM project that imports the openai package leads to the following exception:

TypeError: getDefaultAgent is not a function

getDefaultAgent is part of the openai shims system, which automatically sets certain platform/runtime specific values using module side-effects. I've recreated a simplified version of this issue in a test file (I may have put the test file in the wrong place, please advise and I can move it if needed). You can see the test pass with the following:

node --require ./test/version-check.js  test/hook/side-effects.mjs

And fail with the loader added:

node --require ./test/version-check.js --experimental-loader ./test/generic-loader.mjs test/hook/side-effects.mjs

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ undefined
- 'node'
    at file:///Users/eric/code/triggerdotdev/opensource/import-in-the-middle/test/hook/side-effects.mjs:4:1
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: 'node',
  operator: 'strictEqual'
}

I'm not exactly sure what the issue is, but it does look like possibly the second import of registry.mjs in the shims file maybe just uses a module cache, instead of re-importing the module:

import * as shims from "./registry.mjs";
import * as auto from "./runtime.mjs";

if (!shims.kind) {
  shims.setShims(auto.getRuntime());
}

export * from "./registry.mjs";

@ericallam
Copy link
Author

Seems like other people are running into this issue in the wild, as seen in this issue here: openai/openai-node#903

@timfish
Copy link
Contributor

timfish commented Jun 24, 2024

I'm looking into this! Thanks for the test case.

@timfish
Copy link
Contributor

timfish commented Jun 24, 2024

@ericallam do you happen to have the openai code that causes TypeError: getDefaultAgent is not a function?

@ericallam
Copy link
Author

@timfish full reproduction with OpenAI SDK here: https://github.com/ericallam/iitm-openai

This is the bit of code I copied from them in the test case: https://github.com/openai/openai-node/blob/master/src/_shims/index.mjs

@guidev
Copy link

guidev commented Jul 5, 2024

Any update? this makes Datadog unusable in any node project that uses the openai sdk...

@timfish
Copy link
Contributor

timfish commented Jul 5, 2024

The issue with openai highlights a fundamental flaw in import-in-the-middle (#38) that probably can't be solved with the current Node loaders API.

I have no idea how DataDog initialises import-in-the-middle but this PR adds the ability to programatically skip wrapping of incompatible modules to work around this issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants