Skip to content

feat(examples): add robust error handling script #2415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Abdul565656
Copy link

This PR adds a new example script, examples/error_handling.py, to demonstrate how to gracefully handle the common API errors documented in the README (AuthenticationError, NotFoundError, BadRequestError, etc.).

This is a common requirement for developers building robust applications, and having a dedicated, runnable example will be a valuable resource. The script is designed to run successfully even if the OPENAI_API_KEY is not set, making it easy for new users to test.

Key Features of the Example:

  • Shows how to use try...except for specific openai error types.
  • Demonstrates how to access useful debugging information from the exception object, such as status_code, request_id, and the error message from the response body.
  • Intentionally triggers errors to provide a working demonstration.
  • Gracefully skips tests that require a valid API key if one is not provided.

This addresses a gap in the current examples and should help new users get up to speed on best practices for production-ready code.

@Abdul565656 Abdul565656 requested a review from a team as a code owner June 17, 2025 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant