Handler for exceptions in Python with OpenRouter.ai
Project description
OpenRouter Exception Handler
A Python module distributed via pip that automatically handles exceptions using intelligent responses generated by OpenRouter.ai.
🚀 Installation
pip install openrouter-exception-handler
📌 Features
- Automatically captures Python exceptions during execution.
- Sends exception details to OpenRouter.ai.
- Displays intelligent responses from OpenRouter.ai directly in your stdout.
- Facilitates AI-assisted debugging, reducing the time needed for error diagnosis.
- Optionally decorates entire classes so all their methods automatically benefit from exception handling.
🔧 Quick Usage (Function-Level)
Simply decorate your functions with @exception_handler:
from openrouter_exception_handler.exception_handler import exception_handler
@exception_handler
def divide(a, b):
return a / b
divide(10, 0) # Will trigger ZeroDivisionError
Example Output
❌ Exception captured:
Traceback (most recent call last):
File "example.py", line 6, in wrapper
return func(*args, **kwargs)
File "example.py", line 10, in divide
return a / b
ZeroDivisionError: division by zero
AI Response:
This exception occurs because you're trying to divide by zero.
Validate your inputs before performing division to prevent this error.
💡 Class-Level Usage
To automatically capture exceptions in all methods of a class (including staticmethod and classmethod), use @class_exception_handler:
from openrouter_exception_handler.exception_handler import class_exception_handler
@class_exception_handler
class MyClass:
def instance_method(self, x):
# Will raise ZeroDivisionError if x == 0
return 10 / x
@staticmethod
def static_method(x):
return 10 / x
@classmethod
def class_method(cls, x):
return 10 / x
obj = MyClass()
obj.instance_method(0) # Exception will be caught and sent to OpenRouter
MyClass.static_method(0) # Same behavior
MyClass.class_method(0) # Same behavior
By default, dunder methods (like
__init__,__str__, etc.) are not wrapped. If you want to handle those as well, adjust the class decorator code to include them.
🔑 Configuration
Define your OpenRouter.ai API key in the environment variable APIKEY_OPENROUTER:
export APIKEY_OPENROUTER="your_api_key_here"
📦 Installation
Install easily using pip:
pip install openrouter-exception-handler
📌 Requirements
- Python >= 3.6
- requests
📄 License
MIT
Here's a simple guide to create an API key on OpenRouter
-
Sign Up or Log In Go to openrouter.ai and create an account, or log in if you already have one.
-
Go to Your User/Developer Settings Look for a section like Developer Settings, API Keys, or Settings in your account dashboard.
-
Generate a New Key
- Click Generate New Key (or a similar button) to create your new API key.
- Copy the generated API key. Note that for security reasons, you usually only get to see the key once.
-
Save Your API Key
- Store it in a safe place (e.g., an environment variable or a password manager).
- Avoid committing it directly to a public repository or version control.
Tip: Once you have your API key, set it in your environment (e.g.,
OPENROUTER_API_KEY), so your application can access it securely without exposing it in the source code.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openrouter_exception_handler-0.4.4.tar.gz.
File metadata
- Download URL: openrouter_exception_handler-0.4.4.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
108ca2c92f5e9e93c6b4301119a3ea6521c4ac1bd05de8d9a86bd50046c5ee28
|
|
| MD5 |
b8d7fe4c0f991d8d243adf09c0f2f20f
|
|
| BLAKE2b-256 |
f50413afd5e49b81cd3be8d2f7afd589c66f4a2e13efb873122949921d8c91e7
|
File details
Details for the file openrouter_exception_handler-0.4.4-py3-none-any.whl.
File metadata
- Download URL: openrouter_exception_handler-0.4.4-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1c131841521028755bdae3078653068b7d26b7730de44bb4ac85d5858d94730
|
|
| MD5 |
f5f2acb550f55a2f84d58c7fc1d338d3
|
|
| BLAKE2b-256 |
1cef3a7ed8d91f7002afff9cabcf0062388c04878aa0002c526763be850e408e
|