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

OpenAI RateLimitError #257

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion graphiti_core/llm_client/openai_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from .config import DEFAULT_MAX_TOKENS, LLMConfig
from .errors import RateLimitError, RefusalError

import asyncio

logger = logging.getLogger(__name__)

DEFAULT_MODEL = 'gpt-4o-mini'
Expand Down Expand Up @@ -134,13 +136,33 @@ async def generate_response(
retry_count = 0
last_error = None

def _parse_retry_after(error_msg: str) -> float:
"""Extract retry time from OpenAI error message"""
try:
# Find time in milliseconds (e.g., "Please try again in 479ms")
import re
match = re.search(r'Please try again in (\d+)ms', error_msg)
if match:
return float(match.group(1)) / 1000 # Convert ms to seconds

# Fallback to exponential backoff if no time found
return min(5 * (2 ** retry_count), max_delay)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_delay is not defined. This will cause a NameError. Define max_delay or replace it with a valid variable.

except:
return 5.0 # Default fallback delay

while retry_count <= self.MAX_RETRIES:
try:
response = await self._generate_response(messages, response_model, max_tokens)
return response
except (RateLimitError, RefusalError):
except RefusalError as e:
# These errors should not trigger retries
raise
# Handle rate limits with backoff
except RateLimitError as e:
retry_after = _parse_retry_after(e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_parse_retry_after expects a string, but e is an exception object. Pass str(e) instead.

logger.warning(f"Rate limit hit. Retrying in {retry_after}s (attempt {retry_count + 1}/{self.MAX_RETRIES})")
await asyncio.sleep(retry_after)
retry_count += 1
except (openai.APITimeoutError, openai.APIConnectionError, openai.InternalServerError):
# Let OpenAI's client handle these retries
raise
Expand Down
Loading