openai / openai-python

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

Missing import for asyncio in README example code #1825

Closed okamiRvS closed 1 week ago

okamiRvS commented 3 weeks ago

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

Describe the bug

Hi there,

I noticed a small typo in the example code provided in the README file. The code snippet uses asyncio.run(main()) but is missing the import statement for asyncio. This could cause confusion for users running the example as it will result in a NameError: name 'asyncio' is not defined.

To Reproduce

Here's the updated code with the necessary import:

from openai import AsyncOpenAI
import asyncio  # Add this import

client = AsyncOpenAI()

async def main():
    stream = await client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Say this is a test"}],
        stream=True,
    )
    async for chunk in stream:
        print(chunk.choices[0].delta.content or "", end="")

asyncio.run(main())

Code snippets

No response

OS

-

Python version

-

Library version

-

baslia commented 1 week ago

Hey, I think this is solved now.

okamiRvS commented 1 week ago

I still see the absence of the asynco import in the second snippet of "streaming resppnse"

baslia commented 1 week ago

My bad, I just made a PR for that: https://github.com/openai/openai-python/pull/1858

RobertCraigie commented 1 week ago

thanks @baslia, this was fixed in #1858