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

TypeError: getDefaultAgent is not a function #903

Closed
1 task done
guidev opened this issue Jun 19, 2024 · 7 comments
Closed
1 task done

TypeError: getDefaultAgent is not a function #903

guidev opened this issue Jun 19, 2024 · 7 comments
Labels
bug Something isn't working

Comments

@guidev
Copy link

guidev commented Jun 19, 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

When using openai-node with langchain and datadog, ESM, I get the following error:

TypeError: getDefaultAgent is not a function
    at OpenAI.buildRequest (file:///Users/name/Progetti/nonworking/node_modules/openai/core.mjs:208:66)
    at OpenAI.makeRequest (file:///Users/name/Progetti/nonworking/node_modules/openai/core.mjs:279:44)
    at async file:///Users/name/Progetti/nonworking/node_modules/@langchain/openai/dist/chat_models.js:796:29
    at async RetryOperation._fn (/Users/name/Progetti/nonworking/node_modules/p-retry/index.js:50:12)

Node.js v22.3.0


To Reproduce

1. git clone https://github.com/guidev/nonworking/tree/node20

2. npm run start 

Code snippets

No response

OS

macOS

Node version

node 20, node 22

Library version

4.52.0

@dgellow
Copy link
Contributor

dgellow commented Jun 21, 2024

Hi @guidev,

Thanks for the bug report, and I appreciate the repo example to reproduce the issue. Looking into it.

@ericallam
Copy link

@dgellow this is an issue with import-in-the-middle, it somehow breaks the shims system. I've recreated the issue as a failing test in the import-in-the-middle repo here: nodejs/import-in-the-middle#113

@rattrayalex
Copy link
Collaborator

Thanks for reporting.

Closing this issue as it seems like an import-in-the-middle bug. Hopefully it gets fixed there.

It's possible we'll be able to substantially reduce or eliminate our shims systems once we drop node-fetch as a dependency, which could solve this, but we won't target it specifically.

Members of the community are welcome to share workaround here, but I encourage you to otherwise bring this up with import-in-the-middle or datadog.

@rattrayalex
Copy link
Collaborator

One possible workaround you could try would be to explicitly import 'openai/shims/node' or import 'openai/shims/web' before your first import … from 'openai'

@rattrayalex rattrayalex closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2024
@ericallam
Copy link

ericallam commented Jul 17, 2024

@rattrayalex this isn't solved by importing the shim manually, that just results in a different error. The way to fix this is with the latest version of import-in-the-middle and writing your own loader:

// loader.mjs
import { register } from "node:module";

register("import-in-the-middle/hook.mjs", import.meta.url, {
  parentURL: import.meta.url,
  data: { include: ["openai"]}, // this is the import bit here
});

Then running in node with node --import ./loader.mjs ...

@adam-on-tailwind
Copy link

FYI the loader.mjs workaround prevented the error in my project, but also stopped traces from being sent. The way I was able to fix this was by importing the openai web shims using the --import flag, enabling experimental fetch --experimental-fetch and supplying an http Agent to the openai client at initialization.

Start node like this:

node --import openai/shims/web --import dd-trace/register.js --require dd-trace/init --experimental-fetch src/main/index.js

When initializing the openai client:

import 'http'
import OpenAI from 'openai'

const openai = new OpenAI({
  httpAgent: new http.Agent()
})

And it all works! Same steps apply if you're using Anthropic btw.

@nakatsuka-k
Copy link

@tokisherbakov
I was able to resolve the issue by modifying it to make the request using Fetch as shown below.

      const openAiApiUrl = "https://api.openai.com/v1/chat/completions";
      
      const response = await fetch(openAiApiUrl, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          "Authorization": `Bearer ${appConfig.openAiKey}`,
        },
        body: JSON.stringify({
          model: "gpt-3.5-turbo",
          messages: [...messages],
          max_tokens: 256,
        }),
      });

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

6 participants