litellm icon indicating copy to clipboard operation
litellm copied to clipboard

TypeError using Ollama embedding models

Open tsmdt opened this issue 1 year ago • 2 comments

After upgrading from 1.54.1 to 1.56.3 I am getting a TypeError: object dict can't be used in 'await' expression whenever I try to embed strings using Ollama. I tested OpenAI (text-embedding-3-small) which works fine, but the following example code errors out with Ollama:

import litellm

text = 'Research Data Charta'

response = litellm.embedding(input=text, model='ollama/nomic-embed-text:latest')

print(response)

Error:

Traceback (most recent call last):
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/main.py", line 3670, in embedding
    response = ollama_embeddings_fn(  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/llms/ollama/completion/handler.py", line 92, in ollama_embeddings
    return asyncio.run(
           ^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 686, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/llms/ollama/completion/handler.py", line 55, in ollama_aembeddings
    response_json = await response.json()
                    ^^^^^^^^^^^^^^^^^^^^^
TypeError: object dict can't be used in 'await' expression

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/thomas/Code/projects/dygest/embedtest.py", line 5, in <module>
    response = litellm.embedding(input=text, model='ollama/nomic-embed-text:latest')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/utils.py", line 993, in wrapper
    raise e
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/utils.py", line 874, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/main.py", line 3829, in embedding
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2190, in exception_type
    raise e
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/litellm_core_utils/exception_mapping_utils.py", line 2166, in exception_type
    raise APIConnectionError(
litellm.exceptions.APIConnectionError: litellm.APIConnectionError: object dict can't be used in 'await' expression
Traceback (most recent call last):
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/main.py", line 3670, in embedding
    response = ollama_embeddings_fn(  # type: ignore
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/llms/ollama/completion/handler.py", line 92, in ollama_embeddings
    return asyncio.run(
           ^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.12.8/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py", line 686, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/thomas/Code/projects/dygest/venv_dygest/lib/python3.12/site-packages/litellm/llms/ollama/completion/handler.py", line 55, in ollama_aembeddings
    response_json = await response.json()
                    ^^^^^^^^^^^^^^^^^^^^^
TypeError: object dict can't be used in 'await' expression

Any help would be appreciated, thanks!

tsmdt avatar Dec 28 '24 10:12 tsmdt

The problem exists in v1.55.12 as well.

svenseeberg avatar Dec 30 '24 11:12 svenseeberg

The await has to be removed from https://github.com/BerriAI/litellm/blob/24dd6559a68ddf2bbb9642c48c3482d0058b3e75/litellm/llms/ollama/completion/handler.py#L55

The await is already in the previous line.

svenseeberg avatar Dec 30 '24 12:12 svenseeberg