langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
12.16k stars 2.05k forks source link

Catching the original OpenAI error #1547

Closed ofir5300 closed 11 months ago

ofir5300 commented 1 year ago

Hello, I am using ConversationChain with ChatOpenAI (gpt-4), ChatPromptTemplate & BufferMemory. I would like to implement error handling mechanism in order to show a meaningful error messages for my users.

However, when the chain encounters an error, it appears to throw an error related to the implementation of openai._generate in the langchain -js library. This issue occurs because the code doesn't catch OpenAI server errors and attempts to parse the response data regardless. Consequently, I'm running into the following error:

TypeError: data.choices is not iterable

Same goes for OpenAI llm (with gpt-3.5-turbo) where openai-chat.js running into error:

TypeError: Cannot read properties of undefined (reading '0')

Is this an implementation bug, or is there something missing in my configurations? I've tried using the handleLLMError callback, but it doesn't seem to make a difference since the thrown error is a mismatch either way. Any guidance or suggestions would be greatly appreciated.

ofir5300 commented 1 year ago

Can anyone assist? I cannot get meaningful errors neither can monitor OpenAI request failures

dosubot[bot] commented 11 months ago

Hi, @ofir5300! I'm Dosu, and I'm here to help the langchainjs team manage their backlog. I wanted to let you know that we are marking this issue as stale.

From what I understand, you opened this issue regarding error handling in the langchain-js library when using ConversationChain with ChatOpenAI. The current implementation doesn't catch OpenAI server errors and throws errors related to the implementation of openai._generate. You were seeking assistance on how to handle these errors and display meaningful error messages to users.

Before we close this issue, we wanted to check if it is still relevant to the latest version of the langchainjs repository. If it is, please let us know by commenting on this issue. Otherwise, feel free to close the issue yourself or it will be automatically closed in 7 days.

Thank you for your contribution!

ofir5300 commented 11 months ago

Hey, the issue indeed seem to be solved now. Thank you.

In addition - regarding using it on TypeScript, how can the returned error be parsed (as what type) in order to see internal error message?

dosubot[bot] commented 11 months ago

Thank you @ofir5300 for closing the issue! Your contribution is greatly appreciated.