chroma-core / chroma

the AI-native open-source embedding database
https://www.trychroma.com/
Apache License 2.0
14.64k stars 1.22k forks source link

[Bug]: Cannot import chroma due to `TypeError: Instance and class checks can only be used with @runtime_checkable protocols` #2806

Open mirekphd opened 5 days ago

mirekphd commented 5 days ago

What happened?

Users tried to import chromadb in our AI containers (latestml/ml-gpu-py311-cuda118-hf:TAG), but the import failed with TypeError (see below for stack trace).

Versions

Chroma 0.5.3 Python 3.11.10 Ubuntu 22.04

Note: the reason why we cannot upgrade chromadb from 0.5.3 to either 0.5.4 or 0.5.5 is that both of these newer versions are blocklisted by another package as seen by this error message displayed by pip during such attempt:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
langchain-chroma 0.1.4 requires chromadb!=0.5.4,!=0.5.5,<0.6.0,>=0.4.0, but you have chromadb 0.5.5 which is incompatible.

Relevant log output

Reproducible example using Docker run:

$ docker run --rm -it --name test latestml/ml-gpu-py311-cuda118-hf:20240916-1725 python -c "import chromadb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/conda/lib/python3.11/site-packages/chromadb/__init__.py", line 3, in <module>
    from chromadb.api.client import Client as ClientCreator
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/__init__.py", line 7, in <module>
    from chromadb.api.models.Collection import Collection
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/models/Collection.py", line 4, in <module>
    from chromadb.api.models.CollectionCommon import CollectionCommon
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/models/CollectionCommon.py", line 14, in <module>
    import chromadb.utils.embedding_functions as ef
  File "/opt/conda/lib/python3.11/site-packages/chromadb/utils/embedding_functions/__init__.py", line 46, in <module>
    _all_classes.update(_import_all_efs())
                        ^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/chromadb/utils/embedding_functions/__init__.py", line 37, in _import_all_efs
    and isinstance(attr, EmbeddingFunction)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/typing_extensions.py", line 639, in __instancecheck__
    raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols

Local run (inside the above containers):

(base) jovyan@004209e9b72b:~$ python
Python 3.11.10 | packaged by conda-forge | (main, Sep 10 2024, 11:01:28) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import chromadb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.11/site-packages/chromadb/__init__.py", line 3, in <module>
    from chromadb.api.client import Client as ClientCreator
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/__init__.py", line 7, in <module>
    from chromadb.api.models.Collection import Collection
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/models/Collection.py", line 4, in <module>
    from chromadb.api.models.CollectionCommon import CollectionCommon
  File "/opt/conda/lib/python3.11/site-packages/chromadb/api/models/CollectionCommon.py", line 14, in <module>
    import chromadb.utils.embedding_functions as ef
  File "/opt/conda/lib/python3.11/site-packages/chromadb/utils/embedding_functions/__init__.py", line 46, in <module>
    _all_classes.update(_import_all_efs())
                        ^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/chromadb/utils/embedding_functions/__init__.py", line 37, in _import_all_efs
    and isinstance(attr, EmbeddingFunction)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/typing_extensions.py", line 639, in __instancecheck__
    raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
>>> 
mirekphd commented 5 days ago

One workaround I found is uninstalling the package that pins chromadb to an older version and upgrading chromadb to latest, thus:

$ pip uninstall langchain-chroma
Found existing installation: langchain-chroma 0.1.4
Uninstalling langchain-chroma-0.1.4:
  Would remove:
    /opt/conda/lib/python3.11/site-packages/langchain_chroma-0.1.4.dist-info/*
    /opt/conda/lib/python3.11/site-packages/langchain_chroma/*
Proceed (Y/n)? y
  Successfully uninstalled langchain-chroma-0.1.4

$ pip install chromadb==0.5.5

$ pip show chromadb
Name: chromadb
Version: 0.5.5
Summary: Chroma.
Home-page: https://github.com/chroma-core/chroma
Author: 
Author-email: Jeff Huber <jeff@trychroma.com>, Anton Troynikov <anton@trychroma.com>
License: 
Location: /opt/conda/lib/python3.11/site-packages
[..]

$ python -c "import chromadb; print(chromadb.__version__)"
0.5.5
jeffchuber commented 5 days ago

@mirekphd making sure i have this right - latest chroma works ok? we would consider that a "valid workaround" if so...

mirekphd commented 5 days ago

On its own and in general: if one can upgrade chromadb to its latest version - it is not even a problem that your users would encounter, but the last 2 versions including the latest i.e. the working one are for some reason blocklisted by LangChain... so effectively you cannot upgrade it to latest (if users want to use chromadb through LangChain).

tazarov commented 5 days ago

@jeffchuber, this is how LC have defined their version string in 0.1.4 of the langchain-chroma package:

https://github.com/langchain-ai/langchain/blob/a319a0ff1d17057d750f7e4a8fee98aa8f68703c/libs/partners/chroma/pyproject.toml#L50

It might be so that 0.5.4 and 0.5.5 break backward compatibility.