18. Reading API Responses
JSON Response Structure
AI APIs return structured JSON. Extract data carefully:
response = {
"id": "chatcmpl-123",
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 12,
"total_tokens": 22
}
}
# Extract nested values
content = response["choices"][0]["message"]["content"]
usage = response["usage"]["total_tokens"]
Handling Missing Keys
# Safe extraction with defaults
def get_content(response):
try:
return response.get("choices", [{}])[0].get("message", {}).get("content", "")
except (KeyError, IndexError, TypeError):
return ""
# Or use .get() chaining
content = (
response
.get("choices", [{}])[0]
.get("message", {})
.get("content", "")
)
Batch Responses
AI APIs return lists of completions:
responses = [
{"choices": [{"message": {"content": "Response 1"}}]},
{"choices": [{"message": {"content": "Response 2"}}]},
{"choices": [{"message": {"content": "Response 3"}}]}
]
contents = [r["choices"][0]["message"]["content"] for r in responses]
Pagination
Large results come in pages:
all_results = []
page_token = None
while True:
params = {"limit": 100}
if page_token:
params["after"] = page_token
response = requests.get(url, headers=headers, params=params).json()
all_results.extend(response.get("data", []))
page_token = response.get("next_cursor")
if not page_token:
break
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Local verification checkpoint
Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.
Create a mock API response with nested structure (like an AI completion). Extract the response content, token usage, and model name using safe extraction methods. Handle the case where any key might be missing. Continue to Part 2: Chapters 19-36
# PART 2: Intermediate Python for AI