langchainjs
langchainjs copied to clipboard
Broken OpenAI LLM implementation for node version 18
Issue
When running the vanilla OpenAI LLM function, it returns a 400 for node version 18.
To reproduce
The following code was used to create this error.
import { OpenAI } from "langchain/llms/openai";
export const run = async () => {
const model = new OpenAI({ modelName: "gpt-3.5-turbo"});
try {
const resA = await model.call(
"What would be a good company name a company that makes colorful socks?"
);
console.log({ resA });
} catch (e: any) {
console.log(e!.response!.data)
}
};
run()
Error from the response body
{
error: {
message: 'you must provide a model parameter',
type: 'invalid_request_error',
param: null,
code: null
}
}
As seen, even though a model parameter is provided it still returns an error asking to provide one.
Additional notes
This issue is not present in node v19 and v20.
I've been struggling with that one, thanks for the issue report.
@DarylRodrigo
I just run this code using v18.15.0
and it works. The only change to the code you provided was adding openAIApiKey
to the constructor. Do you have any more details on this?
@DarylRodrigo @JeremyFabrikapp I just ran the exact code snippet above using node v18.16.0
and it works. Can you confirm the exact version of Node you're using? And any more details about your setup
@nfcampos it seems like the issue doesn't occur on every 18.x version, i've tried the followings and so far found only one incompatibility :
- Node v18.0.0 (npm v8.6.0) ✅
- Node v18.1.0 (npm v8.8.0) ❌
- Node v18.4.0 (npm v8.12.1) ✅
- Node v18.16.0 (npm v9.5.1) ✅
I'm using NVM. I can push a fix to .nvmrc by setting specifically 18.16.0 rather than v18 or 20.
Using Node v18.16.0 and still getting same issue
Error: Request failed with status code 400 and body {
"error": {
"message": "you must provide a model parameter",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
@dqbd another one to look into, I cannot reproduce this
@jacobcwright can you confirm it's working on your side with node 20.x ? Also are you using a static Node install or nvm ?
I'm using nvm It does indeed work with Node v20.1.0 @JeremyFabrikapp
The issue stems from fetch
implementation found in Node 18.1.0 adding an additional suffix to the Content-Type
when passing a string as a body. The Content-Type
header will thus be application/json, text/plain;charset=UTF-8
instead of application/json
.
The issue seems to be gone in LangchainJS 0.0.71 and later, where the fetch adapter is not used when NodeJS is detected #1144 and the issue goes away when using other NodeJS 18.x versions. Consider upgrading either of them to fix this issue.
Closing this issue for now.
Note: if the issue occurs again, it might make sense to patch axios-fetch-adapter
to convert body into an Uint8Array instead, this will fix the issue even in NodeJS 18.1.0, but I'm not sure if this might break other environments as well.
Thanks @dqbd for the coverage!