Skip to content
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

Raise a specific error for chat rate limits #24

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pytest = "^8.2.2"
python-dotenv = "^1.0.1"
pytest-asyncio = "^0.23.7"
mypy = "^1.10.0"
ruff = "^0.4.8"
ruff = "^0.5.0"

[build-system]
requires = ["poetry-core"]
Expand Down
23 changes: 23 additions & 0 deletions upstash_qstash/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,26 @@ def __init__(
self.limit = limit
self.remaining = remaining
self.reset = reset


class ChatRateLimitExceededError(QStashError):
def __init__(
self,
limit_requests: Optional[str],
limit_tokens: Optional[str],
remaining_requests: Optional[str],
remaining_tokens: Optional[str],
reset_requests: Optional[str],
reset_tokens: Optional[str],
):
super(
f"Exceeded chat rate limit: "
f"Request limit: {limit_requests}, remaining: {remaining_requests}, reset: {reset_requests}; "
f"token limit: {limit_tokens}, remaining: {remaining_tokens}, reset: {reset_tokens}"
)
self.limit_requests = limit_requests
self.limit_tokens = limit_tokens
self.remaining_requests = remaining_requests
self.remaining_tokens = remaining_tokens
self.reset_requests = reset_requests
self.reset_tokens = reset_tokens
35 changes: 30 additions & 5 deletions upstash_qstash/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import httpx

from upstash_qstash.errors import RateLimitExceededError, QStashError
from upstash_qstash.errors import (
RateLimitExceededError,
QStashError,
ChatRateLimitExceededError,
)


class RetryConfig(TypedDict, total=False):
Expand Down Expand Up @@ -40,10 +44,31 @@ def raise_for_non_ok_status(response: httpx.Response) -> None:
return

if response.status_code == 429:
limit = response.headers.get("Burst-RateLimit-Limit")
remaining = response.headers.get("Burst-RateLimit-Remaining")
reset = response.headers.get("Burst-RateLimit-Reset")
raise RateLimitExceededError(limit, remaining, reset)
headers = response.headers
if "x-ratelimit-limit-requests" in headers:
limit_requests = headers.get("x-ratelimit-limit-requests")
limit_tokens = headers.get("x-ratelimit-limit-tokens")
remaining_requests = headers.get("x-ratelimit-remaining-requests")
remaining_tokens = headers.get("x-ratelimit-remaining-tokens")
reset_requests = headers.get("x-ratelimit-reset-requests")
reset_tokens = headers.get("x-ratelimit-reset-tokens")
raise ChatRateLimitExceededError(
limit_requests=limit_requests,
limit_tokens=limit_tokens,
remaining_requests=remaining_requests,
remaining_tokens=remaining_tokens,
reset_requests=reset_requests,
reset_tokens=reset_tokens,
)

limit = headers.get("Burst-RateLimit-Limit")
remaining = headers.get("Burst-RateLimit-Remaining")
reset = headers.get("Burst-RateLimit-Reset")
raise RateLimitExceededError(
limit=limit,
remaining=remaining,
reset=reset,
)

raise QStashError(
f"Request failed with status: {response.status_code}, body: {response.text}"
Expand Down
Loading