Async Python

HTTP Clients

Async HTTP Clients

Most AI APIs are accessed via HTTP. Let's learn to make efficient async HTTP requests.

HTTPX (Recommended)

Modern, async-first HTTP client:

import httpx

async with httpx.AsyncClient() as client:
    response = await client.post(url, json=data)

Key Features

  • Async support — Native async/await
  • Streaming — Handle streaming responses
  • Timeouts — Prevent hanging requests
  • Retries — Handle transient failures

Best Practices

  • Use context managers (async with)
  • Set reasonable timeouts
  • Handle exceptions properly
  • Reuse client instances

Try It Yourself

Run this code example to practice what you've learned.

example.py
1import asyncio
2# Install with: pip install httpx
3
4# Simulated HTTP client behavior for demo
5class MockAsyncClient:
6 """Mock client for demonstration."""
7
8 async def __aenter__(self):
9 return self
10
11 async def __aexit__(self, *args):
12 pass
13
14 async def post(self, url: str, json: dict, timeout: float = 30.0):
15 await asyncio.sleep(0.5) # Simulate network
16
17 class Response:
18 status_code = 200
19 def json(self):
20 return {
21 "choices": [{
22 "message": {
23 "content": f"Response to: {json.get('messages', [{}])[-1].get('content', '')}"
24 }
25 }]
26 }
27 return Response()
28
29async def chat_completion(messages: list[dict]) -> str:
30 """Make a chat completion request."""
31 async with MockAsyncClient() as client:
32 response = await client.post(
33 "https://api.openai.com/v1/chat/completions",
34 json={
35 "model": "gpt-4",
36 "messages": messages
37 },
38 timeout=30.0
39 )
40
41 if response.status_code == 200:
42 data = response.json()
43 return data["choices"][0]["message"]["content"]
44 else:
45 raise Exception(f"API error: {response.status_code}")
46
47async def main():
48 messages = [
49 {"role": "system", "content": "You are helpful."},
50 {"role": "user", "content": "Hello!"}
51 ]
52
53 response = await chat_completion(messages)
54 print(f"AI: {response}")
55
56asyncio.run(main())