Reference · Snippet · python

OpenAI chat — streaming

openaichatstreaming

from openai import OpenAI

client = OpenAI()
stream = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Explain RAG briefly."}],
    stream=True,
)

for chunk in stream:
    part = chunk.choices[0].delta.content or ""
    print(part, end="", flush=True)