Error "cannot import name 'TypeAliasType' from 'typing_extensions'" when importing openai just after installation
Shows how to fix error "cannot import name 'TypeAliasType' from 'typing_extensions'" when importing openai just after installation. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Confirmed workarounds: pin pydantic==1.10.11 with typingextensions==4.7.0, or upgrade to pydantic>=2.5.0 with typingextensions>=4.6.0 and restart the kernel.
Steps
- What was reported:
Issue openai/openai-python#816 (closed, 21 comments): ### Confirm this is an issue with the Python library and not an underlying OpenAI API
- - [X] This is an issue with the Python library
- ---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[38], line 1
----> 1 import openai
2 import os
4 opai.api_key value [redacted]
4. 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...
5. What works:
[Marwen94 (commenter)]: The TypeAliasType import error when importing openai comes from a typing_extensions/pydantic version mismatch. Confirmed workarounds: pin pydantic==1.10.11 with typing_extensions==4.7.0, or upgrade to pydantic>=2.5.0 with typing_extensions>=4.6.0 and restart the kernel.
## When to use
You are seeing this: What was reported: Issue openai/openai-python#816 (closed, 21 comments): ### Confirm this is an issue with the Python library and not an underlying OpenAI API - [X] This is an issue with the Python library Use this skill when you run into "Error "cannot import name 'TypeAliasType' from 'typing_extensions'" when importing openai just after installation".
## When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
## Versions
Versions mentioned in the source: pydantic 1.10.11, typing_extensions 4.7.0, pydantic 2.5.0, typing_extensions 4.6.0. If you are on something much newer or older, the details may have shifted.
## Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.