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

OpenAI or Azure OpenAI API key not found (Cannot set apiKey - ENV works fine) #5713

Closed
5 tasks done
scottforte opened this issue Jun 10, 2024 · 3 comments
Closed
5 tasks done
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@scottforte
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

const { OpenAI } = require("@langchain/openai");

// WORKS FINE
new OpenAI({
  model,
  temperature,
});

// FAILS
new OpenAI({
  model,
  temperature,
  apiKey: `same exact key - ENV removed - yes, confirmed it's there in the code via console before calling`,
});

Error Message and Stack Trace (if applicable)

Error: OpenAI or Azure OpenAI API key not found\n' +
' at new OpenAIChat ([redacted]\node_modules\@langchain\openai\dist\legacy.cjs:235:19)\n' +
' at new OpenAI ([redacted]\node_modules\@langchain\openai\dist\llms.cjs:79:20)\n' +

rest redacted

Description

Setting the ENV: OPENAI_API_KEY the creating an model works fine, but passing a string fails.

This is only for new OpenAI()

new OpenAIEmbeddings() works fine to pass an apiKey string in

System Info

Windows
NPM 10.5.0
Node v20.12.2

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Jun 10, 2024
@scottforte
Copy link
Contributor Author

scottforte commented Jun 10, 2024

Problem is here:
\node_modules@langchain\openai\dist\legacy.cjs:230

fields?.openAIApiKey ?? (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
should be: fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");

PR: #5714

Copy link

dosubot bot commented Jun 10, 2024

Hey there, @scottforte! I'm here to help you with any bugs, questions, or contributions while we wait for a human maintainer. Let's tackle this together!

The issue you're encountering is due to the OpenAI class constructor not correctly handling the apiKey parameter. The constructor currently checks for fields?.openAIApiKey instead of fields?.apiKey.

The proposed change in PR #5714 addresses this by modifying the line in legacy.ts to correctly check for fields?.apiKey:

- fields?.openAIApiKey ?? (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
+ fields?.apiKey ?? (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");

This change ensures that the apiKey parameter passed directly in the code is correctly recognized and used by the OpenAI class, resolving the issue where setting the API key directly in the code fails while using environment variables works fine.

To continue talking to Dosu, mention @dosu.

@jacoblee93
Copy link
Collaborator

jacoblee93 commented Jun 13, 2024

Merged a fix in #5714, but also these are legacy classes that generally shouldn't be used. Have also updated docs to better reflect this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants