openai/openai-python

Public

mirrored fromhttps://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
release-please--branches--main--changes--next

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/async_demo.py

30lines · modecode

1#!/usr/bin/env -S poetry run python
2
3import asyncio
4
5from openai import AsyncOpenAI
6
7# gets API Key from environment variable OPENAI_API_KEY
8client = AsyncOpenAI()
9
10
11async def main() -> None:
12 stream = await client.chat.completions.create(
13 model="gpt-5.5",
14 messages=[
15 {
16 "role": "user",
17 "content": "Say this is a test",
18 },
19 ],
20 stream=True,
21 )
22 async for chunk in stream:
23 if not chunk.choices:
24 continue
25
26 print(chunk.choices[0].delta.content, end="")
27 print()
28
29
30asyncio.run(main())
31