How do I stream chat completions with OpenAI’s Python API? #2462
Answered
by
MatteoMgr2008
Istituto-freudinttheprodev
asked this question in
Q&A
|
I'm using the official I tried this: response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello"}]
)But it waits until everything is returned. How can I make it stream the tokens one by one as they’re generated? |
Answered by
MatteoMgr2008
Jul 14, 2025
Replies: 2 comments 2 replies
|
Great question! To stream chat completions using the Here’s how you can do it: import openai
openai.api_key = "your-api-key"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Hello!"}],
stream=True # ✅ this enables streaming
)
for chunk in response:
if "choices" in chunk:
content = chunk["choices"][0]["delta"].get("content", "")
print(content, end="", flush=True)This will print the generated message token-by-token in real time. Let me know if that works — and feel free to mark this as the answer if it helps! ✅ |
1 reply
Answer selected by
Istituto-freudinttheprodev
|
here is a detailed explanation : https://cookbook.openai.com/examples/how_to_stream_completions |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great question!
To stream chat completions using the
openaiPython package, you need to setstream=Trueand then iterate over the events.Here’s how you can do it:
This will print the generated message token-by-token in real time.
Let me know if that works — and feel free to mark this as the answer if it helps! ✅