Skip to content

Fix async client streaming responses example in README.md #2370

Closed
@whichxjy

Description

@whichxjy

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

The current code example in README.md for streaming responses with the async client will raise an exception because it is missing an await and should use async for instead of for.

Current example:

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI()

async def main():
    stream = client.responses.create(
        model="gpt-4o",
        input="Write a one-sentence bedtime story about a unicorn.",
        stream=True,
    )

    for event in stream:
        print(event)

asyncio.run(main())

This will raise an exception.
It should be:

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI()

async def main():
    stream = await client.responses.create(
        model="gpt-4o",
        input="Write a one-sentence bedtime story about a unicorn.",
        stream=True,
    )

    async for event in stream:
        print(event)

asyncio.run(main())

To Reproduce

Code snippets

OS

Python version

Library version

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions