langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
84.75k stars 13.09k forks source link

community: improve error handling for missing choices and API errors #20936

Closed ssocean closed 1 week ago

ssocean commented 2 weeks ago

PR Title:

community: improve error handling for missing choices and API errors

PR Message:

Description: This update introduces clearer error handling in the response processing logic. Specifically, it adds explicit exceptions for cases where the API response lacks the choices field or contains error messages, enhancing the debuggability of the integration.

For instance, if there is an issue with the API response (possibly due to excessively long tokens, insufficient balance, or service unavailability, etc.), it previously triggered a "TypeError: 'NoneType' object is not iterable" error. Now, it generates a ValueError stating "This model's maximum context length is 16385 tokens. However, your messages resulted in 19223 tokens. Please reduce the length of the messages. (request id: xxx)."

20240427000058 20240427000104

Issue: N/A

Dependencies: None

Twitter handle: N/A

vercel[bot] commented 2 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **langchain** | ⬜️ Ignored ([Inspect](https://vercel.com/langchain/langchain/Es5XLciLHJbBjDjpFgc1Q6dnnEN7)) | [Visit Preview](https://langchain-git-fork-ssocean-master-langchain.vercel.app) | | Apr 30, 2024 4:21am |
baskaryan commented 2 weeks ago

this is a deprecated implementation, main implementation now lives in libs/partners/openai/langchain_openai/chat_models/base.py. can we make all changes there?

ssocean commented 2 weeks ago

this is a deprecated implementation, main implementation now lives in libs/partners/openai/langchain_openai/chat_models/base.py. can we make all changes there?

Thank you for pointing out the correct location for the implementation. I apologize for the oversight and for making changes in a deprecated file. I have now applied the necessary modifications to libs/partners/openai/langchain_openai/chat_models/base.py as suggested. Please let me know if there are any further adjustments needed~