Error while importing OpenAI "from open import OpenAI"

Hi, thanks a lot for your advice. I am using OpenAI 1.8.0 in colab and typing_extensions 4.7. Weirdly, I just tried again one more time before replying here, and I didn’t get an error and was able to install OpenAI…without separately installing typing_extensions. Magic? Someone fixed a bug? Who knows. I’m just relieved I can carry on.

1 Like

So I guess my highly technical advice to others is “just keep trying.” :joy:

Any updates on this? I have the latest version of OpenAI (1.9.0) and typing_extensions (4.9.0) running on Python 3.10.12 but still getting the same error
ImportError: cannot import name ‘Iterator’ from ‘typing_extensions’ (/usr/local/lib/python3.10/dist-packages/typing_extensions.py)

This is still an issue, latests versin of openai (1.0.9) and typing_extensions(4.9.0). When running same error as everyone

ImportError: cannot import name ‘Iterator’ from ‘typing_extensions’ (/usr/local/lib/python3.10/dist-packages/typing_extensions.py)

I suspect you meant 1.9.0 openai · PyPI :laughing:

If you’re really super irrevocably stuck, consider just using requests in the meantime.

code
import requests
import json
import os

# Ensure you have your OpenAI API key set in the environment variables
openai_api_key = os.getenv("OPENAI_API_KEY")
if openai_api_key is None:
    raise ValueError("OpenAI API key is not set in environment variables.")

url = "https://api.openai.com/v1/chat/completions"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {openai_api_key}"
}

data = {
    "model": "gpt-4-1106-preview",  # Keep only this model
    "temperature": 1, 
    "max_tokens": 256,
    "logit_bias": {1734:-100},
    "messages": [
        {
            "role": "system", 
            "content": "You are the new bosmang of Tycho Station, a tru born and bred belta. You talk like a belta, you act like a belta. The user is a tumang."
        },
        {
            "role": "user",
            "content": "how do I become a beltalowda like you?"
        }
    ],
    "stream": True,  # Changed to True to enable streaming
}

response = requests.post(url, headers=headers, json=data, stream=True)

if response.status_code == 200:
    for line in response.iter_lines():
        if line:
            decoded_line = line.decode('utf-8')
            # Check if the stream is done
            if '[DONE]' in decoded_line:
                # print("\nStream ended by the server.")
                break
            json_str = decoded_line[len('data: '):]
            try:
                json_response = json.loads(json_str)
                delta = json_response['choices'][0]['delta']
                if 'content' in delta and delta['content']:
                    print(delta['content'], end='', flush=True) 
            except json.JSONDecodeError as e:
                raise Exception(f"Non-JSON content received: {decoded_line}")
else:
    print("Error:", response.status_code, response.text)```

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 :slight_smile:

2 Likes

I changed the import of the file that is trying to import Iterator from typing_extensions in this way (On Googlo Colab):

%%writefile /usr/local/lib/python3.10/dist-packages/openai/_utils/_streams.py
from typing import Any
from typing_extensions import AsyncIterator
from typing import Iterator # import Iterator from the correct library

def consume_sync_iterator(iterator: Iterator[Any]) -> None:
    for _ in iterator:
        ...

async def consume_async_iterator(iterator: AsyncIterator[Any]) -> None:
    async for _ in iterator:
        ...

And that works for me.

3 Likes

I had this issue and after several tries here is what worked.
I run the code in Google Colab.

Python: 3.10.12
OpenAI: 1.10.0
typing_extensions: 4.9.0

Here is the code:

!pip install --upgrade openai 

import os
os.environ["OPENAI_API_KEY"] = "YOUR_KEY"

from openai import OpenAI
client = OpenAI()
2 Likes

I actually have no idea what this code snippet does, but it also seems to have worked for me in Google Colab. :smiley::anguished:

Same here exactly…

1 Like

Solution: https://stackoverflow.com/questions/77922817/importerror-cannot-import-name-iterator-from-typing-extensions/77922914#77922914

This resolves my issue in Colab and the error no longer occurs. Thank you!