AI / Core OpenAI Codex Application Fundamentals Interview Questions
How do you implement error handling in OpenAI API applications?
Robust error handling is essential for production OpenAI applications. The Python SDK raises typed exceptions that map to HTTP error codes, allowing fine-grained recovery strategies per error type.
import openai from openai import OpenAI import time client = OpenAI() def robust_api_call(prompt: str) -> str: """Production-ready API call with comprehensive error handling.""" try: response = client.responses.create( model="gpt-5.5", input=prompt, timeout=30.0, ) return response.output_text except openai.AuthenticationError as e: # 401 - Invalid API key raise ValueError("Invalid API key. Check OPENAI_API_KEY.") from e except openai.PermissionDeniedError as e: # 403 - Access denied to model or feature raise PermissionError(f"Permission denied: {e.message}") from e except openai.RateLimitError as e: # 429 - Rate limited: implement exponential backoff for attempt in range(5): wait = (2 ** attempt) + 0.1 time.sleep(wait) try: return client.responses.create(model="gpt-5.5", input=prompt).output_text except openai.RateLimitError: continue raise except openai.BadRequestError as e: # 400 - Invalid request (bad parameters, context overflow) if "context_length_exceeded" in str(e): raise ValueError("Prompt too long - reduce input size.") from e raise except openai.InternalServerError as e: # 500 - OpenAI server error: retry with backoff time.sleep(5) return client.responses.create(model="gpt-5.5", input=prompt).output_text except openai.APIConnectionError as e: # Network error: check connectivity raise ConnectionError("Network error connecting to OpenAI API.") from e except openai.APITimeoutError as e: # Request timed out raise TimeoutError("API request timed out.") from e
| Exception | HTTP code | When raised |
|---|---|---|
| AuthenticationError | 401 | Invalid or missing API key |
| PermissionDeniedError | 403 | Insufficient permissions for model/feature |
| RateLimitError | 429 | RPM or TPM limit exceeded |
| BadRequestError | 400 | Invalid parameters or context overflow |
| InternalServerError | 500 | Transient OpenAI server failure |
| APIConnectionError | N/A | Network connectivity failure |
| APITimeoutError | N/A | Request exceeded timeout setting |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
