Skip to content

Fix code example for Streaming section in Readme #903

@jamesev15

Description

@jamesev15

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

There are a couple of errors in the examples provided for the streaming section.

  1. First Issue
    This is the example code, the last line should be print(chunk.choices[0].delta.content)
from openai import OpenAI

client = OpenAI()

stream = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Say this is a test"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(part.choices[0].delta.content)
  1. Second Issue
    This is the example code, the last line should be print(chunk.choices[0].delta.content)
from openai import AsyncOpenAI

client = AsyncOpenAI()

stream = await client.chat.completions.create(
    prompt="Say this is a test",
    messages=[{"role": "user", "content": "Say this is a test"}],
    stream=True,
)
async for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(part.choices[0].delta.content)

To Reproduce

If you run the snippet provided, there are None values as result

from openai import OpenAI

client = OpenAI()

stream = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Say this is a test"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(part.choices[0].delta.content)+

Result:
None
None
None
None
None
None

Code snippets

No response

OS

linux

Python version

Python 3.10.12

Library version

openai v1.3.3

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