-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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 when invoke ZhipuAI chatmodel with invalid api key #4997
TypeError when invoke ZhipuAI chatmodel with invalid api key #4997
Comments
Hey @jason-wangting, great to see you diving into the depths of LangChain again! 🚀 Based on the error message and stack trace you provided, it seems that the error is occurring because the error handling logic in the The To resolve this issue, you should modify the error handling logic in the async completionWithRetry(
request: ChatCompletionRequest,
stream: boolean,
signal?: AbortSignal,
onmessage?: (event: MessageEvent) => void
) {
const makeCompletionRequest = async () => {
const response = await fetch(this.apiUrl, {
method: "POST",
headers: {
...(stream ? { Accept: "text/event-stream" } : {}),
Authorization: `Bearer ${this.zhipuAIApiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify(request),
signal,
});
// Check if the response status code indicates an error
if (!response.ok) {
// Extract error message from response body if possible
const errorBody = await response.json().catch(() => null);
const errorMessage = errorBody?.message || "An error occurred while making the request.";
throw new Error(`Request failed with status ${response.status}: ${errorMessage}`);
}
// ... rest of the method
};
return this.caller.call(makeCompletionRequest);
} This modification ensures that if the response status code indicates an error (e.g., 401 Unauthorized for an invalid API key), an error is thrown with a message that includes the status code and any error message provided in the response body. This approach allows for more accurate and informative error handling, especially in cases where the error format returned by You can find more information about this in the LangChain JS repository. Please let me know if this helps or if you need further assistance. Sources
|
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I am trying to use
ChatZhipuAI
in my project.Code is here:
You will get a TypeError if you run the code.
You can find the error point here: zhipuai.ts#327
The reason is that ZhipuChatModel will return
{error: {code: 1002, message: 'error message'}}
not{ code: 1002, message: 'error message' }
when you invoke with invalid api key.So the error will not be caught by zhipuai.ts#324, lead to the TypeError.
System Info
"langchain": "^0.1.31",
"@langchain/community": "0.0.44",
The text was updated successfully, but these errors were encountered: