pythonintermediate

Async OpenAI Client in Python

Use the AsyncOpenAI client with asyncio to run concurrent chat completions without blocking.

python
import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI()

async def chat(prompt: str) -> str:
    response = await client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role':'user','content':prompt}],
        temperature=0.7,
    )
    return response.choices[0].message.content

async def run_batch(prompts: list[str]) -> list[str]:
    return await asyncio.gather(*[chat(p) for p in prompts])

if __name__ == '__main__':
    results = asyncio.run(run_batch(['What is 2+2?', 'Capital of France?']))
    for r in results:
        print(r)

Use Cases

  • concurrent LLM calls
  • batch inference
  • async AI pipelines

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.