From 8cf7228b6326cae5f88ebb86a0459edec3cc6dd1 Mon Sep 17 00:00:00 2001 From: Sahil Yadav Date: Fri, 15 May 2026 17:54:36 +0530 Subject: [PATCH] fix(openai): fail fast on unrecoverable RateLimitError codes (#901) (#904) --- .../model/text_completion/openai/llm.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py index 1cefcbe9..df1bfdd1 100755 --- a/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py +++ b/trustgraph-flow/trustgraph/model/text_completion/openai/llm.py @@ -104,7 +104,15 @@ class Processor(LlmService): return resp - except RateLimitError: + except RateLimitError as e: + try: + body = getattr(e, 'body', {}) + if isinstance(body, dict): + code = body.get('error', {}).get('code') + if code in ('insufficient_quota', 'invalid_api_key', 'account_deactivated'): + raise RuntimeError(f"OpenAI unrecoverable error: {code} - {body['error'].get('message', '')}") + except Exception: + pass # Leave rate limit retries to the base handler raise TooManyRequests() @@ -188,7 +196,16 @@ class Processor(LlmService): logger.debug("Streaming complete") - except RateLimitError: + except RateLimitError as e: + try: + body = getattr(e, 'body', {}) + if isinstance(body, dict): + code = body.get('error', {}).get('code') + if code in ('insufficient_quota', 'invalid_api_key', 'account_deactivated'): + logger.warning(f"Hit unrecoverable rate limit error during streaming: {code}") + raise RuntimeError(f"OpenAI unrecoverable error: {code} - {body['error'].get('message', '')}") + except Exception: + pass logger.warning("Hit rate limit during streaming") raise TooManyRequests()