Skip to content

Commit

Permalink
Bug fix: "IndexError: list index out of range" caused by base.py line…
Browse files Browse the repository at this point in the history
… 556

# The old code is not handling the error properly.
# It is giving an error: IndexError: list index out of range when the key is expired or wrong.
# Other api errors might also not be handled properly.

# Please test with the following code

# Old code gives error: IndexError: list index out of range
# New code gives error on expired key: ValueError: Not enough available money, Please go to  recharge
#            and error on wrong key: ValueError: Please check sk-************6GzDaH key from the  platform..

from langchain_community.vectorstores import FAISS
from langchain_openai.embeddings import OpenAIEmbeddings
import os

os.environ['OPENAI_API_BASE'] = "https://api.agicto.cn/v1"
# os.environ['OPENAI_API_KEY'] = "sk-8Dxxl7SxhIHU8J3M6GzDaHfzgUjC3Cm21BSR8Dxx4EuctMJ2" # Expired key, out of money
os.environ['OPENAI_API_KEY'] = "sk-8Dxxl7SxhIHU8J3M6GzDaH" # Wrong key

vectorstore = FAISS.from_texts(
    [
        "some text",
        "some more text"
    ],
    embedding=OpenAIEmbeddings(),
)
  • Loading branch information
MartinChen1973 committed Jan 11, 2025
1 parent bbc3e3b commit 73ad987
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/partners/openai/langchain_openai/embeddings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,10 @@ async def empty_embedding() -> List[float]:
)
if not isinstance(average_embedded, dict):
average_embedded = average_embedded.model_dump()
_cached_empty_embedding = average_embedded["data"][0]["embedding"]
if (len(average_embedded["data"]) > 0):
_cached_empty_embedding = average_embedded["data"][0]["embedding"]
else:
raise ValueError(average_embedded["message"])
return _cached_empty_embedding

return [e if e is not None else await empty_embedding() for e in embeddings]
Expand Down

0 comments on commit 73ad987

Please sign in to comment.