Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'override' from 'typing_extensions' with openai==1.2.0 #751

Closed
1 task done
glejdis opened this issue Nov 9, 2023 · 40 comments
Closed
1 task done

Comments

@glejdis
Copy link

glejdis commented Nov 9, 2023

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

ImportError Traceback (most recent call last)
File , line 4
2 from langchain.embeddings import OpenAIEmbeddings, AzureOpenAIEmbeddings
3 #import openai
----> 4 from openai import AzureOpenAI
5 def utils_embedding(input_chunk, OPENAI_API_KEY, deployment_str = "xxxxx, api_base = "xxxx"):
7 OPENAI_API_KEY = XXX)

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-853491b7-ec2b-411d-9c68-bdb8f2c9309a/lib/python3.10/site-packages/openai/init.py:6
3 from future import annotations
5 import os as _os
----> 6 from typing_extensions import override
8 from . import types
9 from ._types import NoneType, Transport, ProxiesTypes

ImportError: cannot import name 'override' from 'typing_extensions' (/databricks/python/lib/python3.10/site-packages/typing_extensions.py)

To Reproduce

import openai

Code snippets

No response

OS

windows

Python version

python v3.10.12

Library version

openai v1.2.0

@glejdis glejdis added the bug Something isn't working label Nov 9, 2023
@samedovzaur
Copy link

I'm getting AzureOpenAI importing error as well :(

@RobertCraigie
Copy link
Collaborator

I'm getting AzureOpenAI importing error as well :(

@samedovzaur Can you share an example snippet / error?

@samedovzaur
Copy link

I'm getting AzureOpenAI importing error as well :(

@samedovzaur Can you share an example snippet / error?

Opened a new issue called AzureOpenAI error, but let me paste it in here:
This is the code I've taken from microsoft Azure documentation:

import os
from openai import AzureOpenAI

llm = AzureOpenAI(
api_key = azureAPIKey_4,
api_version = apiVersion,
azure_endpoint = baseURL_4,
)

This is the error:
ImportError: cannot import name 'AzureOpenAI' from 'openai' (/usr/local/lib/python3.10/dist-packages/openai/init.py)

@RobertCraigie
Copy link
Collaborator

@glejdis what version of typing-extensions do you have installed?

We currently require >= 4.5 and version 4.5 includes the override symbol so I think you must be on an earlier version that's outside our requirements.

@glejdis
Copy link
Author

glejdis commented Nov 9, 2023

@RobertCraigie I have been using v4.5

@RobertCraigie
Copy link
Collaborator

@glejdis What does this command output for you?

python -c 'from importlib.metadata import version; print(version("typing_extensions"))'

@glejdis
Copy link
Author

glejdis commented Nov 9, 2023

@RobertCraigie 4.5.0

@RobertCraigie
Copy link
Collaborator

hmm are you running it in the same environment as your original report?

What if you replace the script you had before with this?

from importlib.metadata import version

print(version("typing_extensions"))

@glejdis
Copy link
Author

glejdis commented Nov 9, 2023

@RobertCraigie it is still 4.5.0 and yes I am on the same environment. I am running the code as a notebook in Databricks

@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Nov 9, 2023

Okay I'm not sure what could be causing your issue then because 4.5.0 definitely includes override:

  • This is the tag that corresponds to the v4.5.0 release
  • Here's the link to the override definition in the code

It also works for me at runtime on Python 3.10.13 and typing-extensions==4.5.0.

Can you try upgrading to the latest typing-extensions version? There may be a weird bug in their library somehow.

@glejdis
Copy link
Author

glejdis commented Nov 9, 2023

@RobertCraigie Thank you so much for your help but I already updated the typing-extensions package to the latest version but the error is still there.

@RobertCraigie
Copy link
Collaborator

Can you reproduce this issue outside of databricks?

@glejdis
Copy link
Author

glejdis commented Nov 9, 2023

An update: I replaced "OpenAIEmbeddings" function with "AzureOpenAIEmbeddings". And the previous error did not appear anymore. However, now I receive this error: NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

@rattrayalex
Copy link
Collaborator

Could you share a Replit or similar reproduction?

@snehaa2632000
Copy link

An update: I replaced "OpenAIEmbeddings" function with "AzureOpenAIEmbeddings". And the previous error did not appear anymore. However, now I receive this error: NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

where should this change be made?

@glejdis
Copy link
Author

glejdis commented Nov 10, 2023

@RobertCraigie @snehaa2632000 Here is the code

def utils_embedding(input_chunk, OPENAI_API_KEY, deployment_str = "text-embedding-ada-002", api_base = "https://xxx"):

OPENAI_API_KEY = dbutils.secrets.get(scope = "tokens", key = "xxx")
openai.api_type = "azure"
openai.api_base = "https://xxx"
openai.api_version = "2023-08-01-preview"
openai.api_key = OPENAI_API_KEY
os.environ["OPENAI_API_TYPE"] = openai.api_type
os.environ["OPENAI_API_BASE"] = openai.api_base
os.environ["OPENAI_API_VERSION"] = openai.api_version
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

with get_openai_callback() as cb:
    # embeddings
    embeddings = AzureOpenAIEmbeddings(deployment = deployment_str,
                            model="text-embedding-ada-002",
                            api_key = openai.api_key,
                            api_version=openai.api_version
                            )
    embedding = embeddings.embed_query(input_chunk)
total_tokens = cb.total_tokens
prompt_tokens = cb.prompt_tokens
completion_tokens = cb.completion_tokens
cost_per_chunk = utils_calculate_costs(deployment_name = deployment_str, prompt_tokens = prompt_tokens, completion_tokens = completion_tokens)

return embedding, cost_per_chunk

OPENAI_API_KEY = dbutils.secrets.get(scope = "tokens", key = "xxx")
parsed_chunk = "Hi how are you?"
embedding, embedding_costs = utils_embedding(parsed_chunk, OPENAI_API_KEY = OPENAI_API_KEY, api_base = "https://xxx", deployment_str = "text-embedding-ada-002")

@snehaa2632000
Copy link

I am also getting the same issue on Databricks:
Error -

----> 1 from openai import OpenAI

File /databricks/python_shell/dbruntime/PythonPackageImportsInstrumentation/__init__.py:171, in _create_import_patch.<locals>.import_patch(name, globals, locals, fromlist, level)
    166 thread_local._nest_level += 1
    168 try:
    169     # Import the desired module. If you're seeing this while debugging a failed import,
    170     # look at preceding stack frames for relevant error information.
--> 171     original_result = python_builtin_import(name, globals, locals, fromlist, level)
    173     is_root_import = thread_local._nest_level == 1
    174     # `level` represents the number of leading dots in a relative import statement.
    175     # If it's zero, then this is an absolute import.

File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3765dafa-be7e-4bea-991a-3ff3971c3373/lib/python3.9/site-packages/openai/__init__.py:6
      3 from __future__ import annotations
      5 import os as _os
----> 6 from typing_extensions import override
      8 from . import types
      9 from ._types import NoneType, Transport, ProxiesTypes

ImportError: cannot import name 'override' from 'typing_extensions' (/databricks/python/lib/python3.9/site-packages/typing_extensions.py)

Current specs :

  • successfully upgraded typing-extensions to 4.8.0
  • successfully installed openai==1.2.2

@RobertCraigie
Copy link
Collaborator

@snehaa2632000 can you try importing it yourself and if that fails, report an issue to databricks as this appears to be unrelated to the openai package. It also looks like they're doing import patching.

from typing_extensions import override

@snehaa2632000
Copy link

@snehaa2632000 can you try importing it yourself and if that fails, report an issue to databricks as this appears to be unrelated to the openai package. It also looks like they're doing import patching.

from typing_extensions import override

It still fails if I try importing myself. I will raise an issue to Databricks.

@glejdis
Copy link
Author

glejdis commented Nov 10, 2023

Could you share a Replit or similar reproduction?

The error still persists. This is the error: NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}}

@RobertCraigie
Copy link
Collaborator

@glejdis can you share a full code snippet? The snippet you shared doesn't include imports.

@glejdis
Copy link
Author

glejdis commented Nov 10, 2023

@RobertCraigie @snehaa2632000 Here is the code

def utils_embedding(input_chunk, OPENAI_API_KEY, deployment_str = "text-embedding-ada-002", api_base = "https://xxx"):

OPENAI_API_KEY = dbutils.secrets.get(scope = "tokens", key = "xxx")
openai.api_type = "azure"
openai.api_base = "https://xxx"
openai.api_version = "2023-08-01-preview"
openai.api_key = OPENAI_API_KEY
os.environ["OPENAI_API_TYPE"] = openai.api_type
os.environ["OPENAI_API_BASE"] = openai.api_base
os.environ["OPENAI_API_VERSION"] = openai.api_version
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY

with get_openai_callback() as cb:
    # embeddings
    embeddings = AzureOpenAIEmbeddings(deployment = deployment_str,
                            model="text-embedding-ada-002",
                            api_key = openai.api_key,
                            api_version=openai.api_version
                            )
    embedding = embeddings.embed_query(input_chunk)
total_tokens = cb.total_tokens
prompt_tokens = cb.prompt_tokens
completion_tokens = cb.completion_tokens
cost_per_chunk = utils_calculate_costs(deployment_name = deployment_str, prompt_tokens = prompt_tokens, completion_tokens = completion_tokens)

return embedding, cost_per_chunk

OPENAI_API_KEY = dbutils.secrets.get(scope = "tokens", key = "xxx") parsed_chunk = "Hi how are you?" embedding, embedding_costs = utils_embedding(parsed_chunk, OPENAI_API_KEY = OPENAI_API_KEY, api_base = "https://xxx", deployment_str = "text-embedding-ada-002")

@RobertCraigie I shared it here :)

@RobertCraigie
Copy link
Collaborator

Sorry it wasn't clear, that code doesn't include imports, I'm not sure where AzureOpenAIEmbeddings is coming from.

@glejdis
Copy link
Author

glejdis commented Nov 10, 2023

from langchain.embeddings import AzureOpenAIEmbeddings

@RobertCraigie
Copy link
Collaborator

Okay I think this is a langchain issue then, if you aren't on the latest version I'd recommend upgrading to the latest and testing again. If it still doesn't work then please open an issue on the langchain repository.

@RobertCraigie RobertCraigie removed the bug Something isn't working label Nov 10, 2023
@RobertCraigie
Copy link
Collaborator

I'm going to close this issue as the reported ImportError is a databricks bug and the current issue isn't related to this SDK.

@RobertCraigie RobertCraigie closed this as not planned Won't fix, can't repro, duplicate, stale Nov 10, 2023
@ondrejhavlicek
Copy link

This occurs in databricks runtime 13.x (and also 14.x with slightly different error), when installing openai using !pip install openai. If I switch to %pip install openai, everything works fine.. In runtime 12.2 it apparently does not matter if one uses !pip or %pip.

@rattrayalex
Copy link
Collaborator

Should this be reported to databricks? Is there an issue tracker you could link to here with an issue?

@alejandrowassermannbfa
Copy link

If you specify an older version of openai it works. This version was suggested by chatgpt itself and it did the trick
%pip install openai==0.27.0

@bounaux
Copy link

bounaux commented Nov 28, 2023

after changing the openai version to 0.27.0, saw another error:
ImportError: cannot import name 'AsyncOpenAI' from 'openai' (/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.10/site-packages/openai/init.py)

@loicmarie
Copy link

Entered this issue in Databricks notebook as well, was able to fix it by running:
dbutils.library.restartPython()
Just after the %pip install

@ondrejhavlicek
Copy link

I confirm that in runtime 13.3 ML it works like this

%pip install openai --upgrade

dbutils.library.restartPython()

The "upgrade" is needed because otherwise it keeps preinstalled version 0.27.8

@davies-w
Copy link

I'm now having this issue in a fairly vanilla Google Colab Notebook (it was working last week, now it isn't!)

@rattrayalex
Copy link
Collaborator

@davies-w if from typing_extensions import override does not work in the Colab notebook, please report that issue to Google Colab. If it does, and you still get the error mentioned in the title of this issue, please open a new issue with full details of your environment, your code, and the error you're seeing.

@907Resident
Copy link

This occurs in databricks runtime 13.x (and also 14.x with slightly different error), when installing openai using !pip install openai. If I switch to %pip install openai, everything works fine.. In runtime 12.2 it apparently does not matter if one uses !pip or %pip.

Using the %pip install openai worked for me in databricks (14.0) after trying other installation methods. I am not certain why that is the case. I previously tried rolling back the openai version and that did not work; only when I used the magic command for some reason I was able to bypass the ImportError.

@aymane1997
Copy link

hello I solve it by updating my version for typing-extensions and openai
pip install --force-reinstall typing-extensions==4.5
pip install --force-reinstall openai==1.8
I hope it helps :)

@lbcommer
Copy link

Just
dbutils.library.restartPython()
after installing the libraries worked for me.

@neurosageai
Copy link

@glejdis What does this command output for you?

python -c 'from importlib.metadata import version; print(version("typing_extensions"))'

4.3.0

@joshdunnlime
Copy link

Just dbutils.library.restartPython() after installing the libraries worked for me.

Yep. Worked for me too. Thanks!!!

@nadavc2c
Copy link

nadavc2c commented Apr 6, 2024

Hi, For me what helped is using
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
(change the path as required to be where you have the package).
This way, when you import it uses the correct package, and not Databricks / Splunk path /databricks/python/lib/python3.10/site-packages/typing_extensions.py which may be incompatible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests