API Requests
API requests are network calls made to external web endpoints to exchange data. In modern Python development, we use packages like `requests` for synchronous ca...
Definition
API requests are network calls made to external web endpoints to exchange data. In modern Python development, we use packages like requests for synchronous calls and httpx for both synchronous and asynchronous requests.
Why It Exists
Because foundation models are hosted as web services by providers like Google, OpenAI, or Anthropic, making network requests is a core task for an AI Engineer. You must know how to configure request headers, manage timeouts, handle network errors, and stream payloads efficiently.
How It Works
CLIENT-SERVER EXCHANGE
┌──────────────┐ ┌────────────────────┐
│ AI App │ ──[ Request ]──► │ Model Provider API │ (Gemini/OpenAI)
│ (Client) │ ◄──[ Response ]─ │ (Server) │
└──────────────┘ └────────────────────┘
- Headers: Authorization Bearer Key, Content-Type Application/JSON
- Payload: Prompt and Hyperparameters (JSON)
- Output: Text, tokens, or status messages (JSON)
1. **Constructing the Payload**: Build a JSON payload containing the prompt and parameters like temperature.
2. **Attaching Headers**: Set the `Authorization` header containing the API key and define the content-type.
3. **Execution**: Send the POST request. Wait for the server's status code (e.g., 200 OK, 429 Too Many Requests, or 500 Server Error) and process the returned payload.Real-World Example
Claude Desktop or any chat client communicates with backend APIs using HTTP POST requests, passing the prompt as a payload and displaying the response once it returns from the servers.
Python Example
Here is a production-style asynchronous API client written using httpx that fetches model completions from an external service safely:
import httpx
import asyncio
from typing import Dict, Any, Optional
class AsyncModelClient:
"""Asynchronous client for interacting with model service APIs."""
def __init__(self, base_url: str, api_key: str):
self.base_url = base_url
self.api_key = api_key
async def get_completion(self, prompt: str) -> Optional[Dict[str, Any]]:
headers = {
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json"
}
payload = {
"prompt": prompt,
"temperature": 0.7
}
# Use httpx.AsyncClient for non-blocking concurrent requests
async with httpx.AsyncClient() as client:
try:
response = await client.post(
f"{self.base_url}/completions",
json=payload,
headers=headers,
timeout=10.0
)
# Check for HTTP status errors (like 4xx, 5xx)
response.raise_for_status()
return response.json()
except httpx.HTTPStatusError as e:
print(f"HTTP Error occurred: {e.response.status_code} - {e.response.text}")
return None
except httpx.RequestError as e:
print(f"Network error occurred: {e}")
return None
# Demo
async def run_client():
client = AsyncModelClient("https://api.mockllmprovider.com/v1", "test_api_key")
# This call is non-blocking and will timeout in 10s if the mock domain is unresponsive
result = await client.get_completion("Explain quantum computing.")
print("Received API Result:", result)
if __name__ == "__main__":
asyncio.run(run_client())Interview Questions
- Beginner: What is the difference between synchronous and asynchronous HTTP requests?
- Intermediate: What library would you choose for asynchronous HTTP requests in modern Python, and why is
requestsnot suitable? - Advanced: How do you configure a request to handle streaming text tokens chunk-by-chunk in real-time?
Interview Answers
- Beginner: Synchronous requests freeze execution, blocking the program until the server returns a response. Asynchronous requests allow the program to continue executing other tasks while waiting for the network call to finish.
- Intermediate: I would use
httpxoraiohttp. The standardrequestslibrary is synchronous and blocking; calling it inside an async app blocks the thread and negates the benefits of using an event loop. - Advanced: By using streaming requests. In
httpx, you can useasync with client.stream("POST", url, json=payload) as response:and iterate over the response chunks usingasync for chunk in response.aiter_text():, printing or processing each token chunk as it is received.
Common Mistakes
- Omitting request timeouts: Making network requests without a timeout configuration. If the provider's server hangs, your application will hang indefinitely. Always set explicit connection and read timeouts.
Assignment
Write a script using httpx to make a GET request to a public API endpoint. Print the response status code, and print a custom warning if the request takes more than 1 second to complete.