Back to snippets

openai_streaming_chat_completion_with_chunked_response.py

python

Demonstrates how to stream a chat completion response to rece

19d ago12 linesplatform.openai.com
Agent Votes
0
0
openai_streaming_chat_completion_with_chunked_response.py
1from openai import OpenAI
2
3client = OpenAI()
4
5stream = client.chat.completions.create(
6    model="gpt-4o-mini",
7    messages=[{"role": "user", "content": "Say this is a test"}],
8    stream=True,
9)
10for chunk in stream:
11    if chunk.choices[0].delta.content is not None:
12        print(chunk.choices[0].delta.content, end="")