Search code examples
pythonlangchainvector-databasechromadb

How do you catch the duplicate id error when using langchain.vectorstores.Chroma.from_documents()


I use the following line to add langchain documents to a chroma database: Chroma.from_documents(docs, embeddings, ids=ids, persist_directory='db')

when ids are duplicates, I get this error: chromadb.errors.IDAlreadyExistsError

how do I catch the error? (duplicate ids are expected - I expect Chorma to not add them)

I've tried identifying the error in langchain documentation. Not sure how to catch it.


Solution

  • Here's an example of how you can handle exceptions raised by Chroma.from_documents() -

    try: 
        Chroma.from_documents(docs, embeddings, ids=ids, persist_directory='db')
    except chromadb.errors.IDAlreadyExistsError as id_error:
        print(f"this is an error message => {id_error}")
    

    For detailed explanation for handling exceptions in Python, checkout the Python Exception Handling docs