openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.57k stars 3.15k forks source link

Error "cannot import name 'TypeAliasType' from 'typing_extensions'" when importing openai just after installation #816

Closed yuanqingye closed 10 months ago

yuanqingye commented 11 months ago

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

Describe the bug


ImportError                               Traceback (most recent call last)
Cell In[38], line 1
----> 1 import openai
      2 import os
      4 opai.api_key = os.getenv("OPENAI_API_KEY")

File D:\Anaconda\envs\py38\lib\site-packages\openai\__init__.py:8
      5 import os as _os
      6 from typing_extensions import override
----> 8 from . import types
      9 from ._types import NoneType, Transport, ProxiesTypes
     10 from ._utils import file_from_path

File D:\Anaconda\envs\py38\lib\site-packages\openai\types\__init__.py:5
      1 # File generated from our OpenAPI spec by Stainless.
      3 from __future__ import annotations
----> 5 from .edit import Edit as Edit
      6 from .image import Image as Image
      7 from .model import Model as Model

File D:\Anaconda\envs\py38\lib\site-packages\openai\types\edit.py:6
      3 from typing import List
      4 from typing_extensions import Literal
----> 6 from .._models import BaseModel
      7 from .completion_usage import CompletionUsage
      9 __all__ = ["Edit", "Choice"]

File D:\Anaconda\envs\py38\lib\site-packages\openai\_models.py:20
     18 import pydantic
     19 import pydantic.generics
---> 20 from pydantic.fields import FieldInfo
     22 from ._types import (
     23     Body,
     24     IncEx,
   (...)
     31     HttpxRequestFiles,
     32 )
     33 from ._utils import (
     34     is_list,
     35     is_given,
   (...)
     39     strip_not_given,
     40 )

File D:\Anaconda\envs\py38\lib\site-packages\pydantic\fields.py:24
     21 from pydantic_core import PydanticUndefined
     22 from typing_extensions import Literal, Unpack
---> 24 from . import types
     25 from ._internal import _decorators, _fields, _generics, _internal_dataclass, _repr, _typing_extra, _utils
     26 from .config import JsonDict

File D:\Anaconda\envs\py38\lib\site-packages\pydantic\types.py:33
     31 from annotated_types import BaseMetadata, MaxLen, MinLen
     32 from pydantic_core import CoreSchema, PydanticCustomError, core_schema
---> 33 from typing_extensions import Annotated, Literal, Protocol, TypeAlias, TypeAliasType, deprecated
     35 from ._internal import (
     36     _core_utils,
     37     _fields,
   (...)
     41     _validators,
     42 )
     43 from ._migration import getattr_migration

ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (D:\Anaconda\envs\py38\lib\site-packages\typing_extensions.py)

To Reproduce

import openai

then error happens the typing_extensions version is 4.8.0(latest)

Code snippets

No response

OS

windows

Python version

Python 3.8.16

Library version

openai v 1.2.4

RobertCraigie commented 11 months ago

This stack trace comes from pydantic, what pydantic version do you have installed?

yuanqingye commented 11 months ago

pydantic

2.5.0

RobertCraigie commented 11 months ago

Are you using databricks? There are known issues with their environment breaking things https://github.com/openai/openai-python/issues/751

yuanqingye commented 11 months ago

Are you using databricks? There are known issues with their environment breaking things #751

No, As far as I know, we didn't use databricks. Actually, I haven't heard of that before.

rattrayalex commented 11 months ago

What version of Anaconda are you on?

yuanqingye commented 11 months ago

What version of Anaconda are you on?

23.1.0

VibhuAg commented 11 months ago

Was able to fix this issue by upgrading to Python 3.11

yuanqingye commented 11 months ago

Was able to fix this issue by upgrading to Python 3.11

First, Thank you very much for point out the way to walk through

If possible, can you show me the reason for this bug?

reinvantveer commented 9 months ago

@rattrayalex @RobertCraigie can you re-open this issue? It's still an unresolved issue - a compatibility issue with Python versions before 3.11 where the pypi.org index promises support from 3.7.1 onwards.

RobertCraigie commented 9 months ago

The openai package works for me on Python 3.8, can you share any more details about your environment?

neighthan commented 7 months ago

This is probably an issue with pydantic or typing_extensions versions. In the pyproject.toml, it says one can use

    "pydantic>=1.9.0, <3",
    "typing-extensions>=4.7, <5",

but I got this same error in an environment with

though not in one with

There were several other differences between the envs, but this might be a start towards narrowing down versions of these libraries that work.

saurabhtiger commented 7 months ago

This is probably an issue with pydantic or typing_extensions versions. In the pyproject.toml, it says one can use

    "pydantic>=1.9.0, <3",
    "typing-extensions>=4.7, <5",

but I got this same error in an environment with

  • typing_extensions 4.10.0
  • pydantic 2.6.2

though not in one with

  • typing_extensions 4.8.0
  • pydantic 1.10.13

There were several other differences between the envs, but this might be a start towards narrowing down versions of these libraries that work.

This worked.

Marwen94 commented 6 months ago

I still see this issue : Python version: 3.11 Pydantic version: 2.7.0 typing-extensions version: 4.11.0 Error : ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/typing_extensions.py)

saurabhtiger commented 6 months ago

I still see this issue : Python version: 3.11 Pydantic version: 2.7.0 typing-extensions version: 4.11.0 Error : ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/typing_extensions.py)

Install below version of libraries: pydantic==1.10.11 typing_extensions==4.5.0

Marwen94 commented 6 months ago

I still see this issue : Python version: 3.11 Pydantic version: 2.7.0 typing-extensions version: 4.11.0 Error : ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/typing_extensions.py)

Install below version of libraries: pydantic==1.10.11 typing_extensions==4.5.0

Thanks @saurabhtiger, I managed to make it work with pydantic==1.10.11 typing_extensions==4.7.0 since openai latest version have a requirement typing_extensions >= 4.7.0

AgrawalTitiksha commented 5 months ago

while importing this libraries: import os from apikey import apikey

import streamlit as st from langchain.llms import OpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain, SequentialChain from langchain.memory import ConversationBufferMemory from langchain.utilities import WikipediaAPIWrapper

error occoured:

ImportError Traceback (most recent call last) Cell In[31], line 9 7 from langchain.prompts import PromptTemplate 8 from langchain.chains import LLMChain, SequentialChain ----> 9 from langchain.memory import ConversationBufferMemory 10 from langchain.utilities import WikipediaAPIWrapper

File ~\anaconda3\lib\site-packages\langchain\memory__init__.py:29 1 """Memory maintains Chain state, incorporating context from past runs. 2 3 Class hierarchy for Memory: (...) 27 AIMessage, BaseMessage, HumanMessage 28 """ # noqa: E501 ---> 29 from langchain_community.chat_message_histories import ( 30 AstraDBChatMessageHistory, 31 CassandraChatMessageHistory, 32 ChatMessageHistory, 33 CosmosDBChatMessageHistory, 34 DynamoDBChatMessageHistory, 35 ElasticsearchChatMessageHistory, 36 FileChatMessageHistory, 37 MomentoChatMessageHistory, 38 MongoDBChatMessageHistory, 39 PostgresChatMessageHistory, 40 RedisChatMessageHistory, 41 SingleStoreDBChatMessageHistory, 42 SQLChatMessageHistory, 43 StreamlitChatMessageHistory, 44 UpstashRedisChatMessageHistory, 45 XataChatMessageHistory, 46 ZepChatMessageHistory, 47 ) 49 from langchain.memory.buffer import ( 50 ConversationBufferMemory, 51 ConversationStringBufferMemory, 52 ) 53 from langchain.memory.buffer_window import ConversationBufferWindowMemory

File ~\anaconda3\lib\site-packages\langchain_community\chat_message_histories__init.py:137, in getattr(name) 135 def getattr__(name: str) -> Any: 136 if name in _module_lookup: --> 137 module = importlib.import_module(_module_lookup[name]) 138 return getattr(module, name) 139 raise AttributeError(f"module {name} has no attribute {name}")

File ~\anaconda3\lib\importlib__init__.py:126, in import_module(name, package) 124 break 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level)

File ~\anaconda3\lib\site-packages\langchain_community\chat_message_histories\sql.py:6 3 from abc import ABC, abstractmethod 4 from typing import Any, List, Optional ----> 6 from sqlalchemy import Column, Integer, Text, create_engine 8 try: 9 from sqlalchemy.orm import declarative_base

File ~\anaconda3\lib\site-packages\sqlalchemy__init.py:12 8 from future__ import annotations 10 from typing import Any ---> 12 from . import util as _util 13 from .engine import AdaptedConnection as AdaptedConnection 14 from .engine import BaseRow as BaseRow

File ~\anaconda3\lib\site-packages\sqlalchemy\util__init__.py:15 12 from typing import TYPE_CHECKING 14 from . import preloaded as preloaded ---> 15 from ._collections import coerce_generator_arg as coerce_generator_arg 16 from ._collections import coerce_to_immutabledict as coerce_to_immutabledict 17 from ._collections import column_dict as column_dict

File ~\anaconda3\lib\site-packages\sqlalchemy\util_collections.py:38 35 import weakref 37 from ._has_cy import HAS_CYEXTENSION ---> 38 from .typing import is_non_string_iterable 39 from .typing import Literal 40 from .typing import Protocol

File ~\anaconda3\lib\site-packages\sqlalchemy\util\typing.py:56 54 from typing_extensions import TypeGuard as TypeGuard # 3.10 55 from typing_extensions import Self as Self # 3.11 ---> 56 from typing_extensions import TypeAliasType as TypeAliasType # 3.12 58 _T = TypeVar("_T", bound=Any) 59 _KT = TypeVar("_KT")

ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (C:\Users\titik\anaconda3\lib\site-packages\typing_extensions.py)

how to solve this??

saurabhtiger commented 5 months ago

how to solve this??

refer to above comments

NIisar-shah commented 4 months ago

i am facing the same issue; and i have seen above comments. the issue is bit different kindly help me with it,thanks.

iImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (/opt/conda/lib/python3.10/site-packages/typing_extensions.py)

downgrading typing extension and pydantic shows the following errors:

albumentations 1.4.10 requires pydantic>=2.7.0, but you have pydantic 1.10.11 which is incompatible. albumentations 1.4.10 requires pip install typing_extensions==4.9.0, but you have typing_extensions==4.5.0 which is incompatible.

NIisar-shah commented 4 months ago

if i go with the typing_extensions==4.5.0 and pydantic>=2.7.0, it shows the first error typeAlias error

SthePasso commented 6 days ago

pydantic==1.10.11 typing_extensions==4.7.0 I tried to install this previous versions and it didn't like it. So I installed: !pip install pydantic>=2.5.0 and !pip install typing_extensions>=4.6.0 Then I restarted the Kernel from my env and it did work!

NIisar-shah commented 5 days ago

thanks alot

Get Outlook for Androidhttps://aka.ms/AAb9ysg


From: Sthefanie Passo @.> Sent: Tuesday, October 15, 2024 5:16:26 AM To: openai/openai-python @.> Cc: Sherazi Syed Nisar Yousaf @.>; Comment @.> Subject: Re: [openai/openai-python] Error "cannot import name 'TypeAliasType' from 'typing_extensions'" when importing openai just after installation (Issue #816)

pydantic==1.10.11 typing_extensions==4.7.0 I tried to install this previous versions and it didn't like it. So I installed: !pip install pydantic>=2.5.0 and !pip install typing_extensions>=4.6.0 Then I restarted the Kernel from my env and it did work!

— Reply to this email directly, view it on GitHubhttps://github.com/openai/openai-python/issues/816#issuecomment-2412357272, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQJUGDYZJPNIUGVRCB4NAJLZ3QYCVAVCNFSM6AAAAAA7KH4ZWKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMJSGM2TOMRXGI. You are receiving this because you commented.Message ID: @.***>