pythonintermediate
Async OpenAI Client in Python
Use the AsyncOpenAI client with asyncio to run concurrent chat completions without blocking.
pythonPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
LangChain Prompt Chain (Python)
Build a simple LLMChain with a prompt template and ChatOpenAI in LangChain.
Best for: prompt chaining
#langchain#openai
pythonadvanced
OpenAI Batch API for Cost Reduction
Submit large workloads via the OpenAI Batch API for 50% cost reduction with async processing.
Best for: batch inference
#openai#batch
pythonbeginner
OpenAI DALL-E Image Generation
Generate and save images using the DALL-E 3 API with quality and style control.
Best for: AI image creation
#openai#dall-e
pythonadvanced
OpenAI Assistants API with File Search
Create a persistent AI assistant with file search capability using the Assistants API v2.
Best for: document Q&A
#openai#assistants