feat(devgen/providers): normalize token limit error handling

Add token limit error detection and formatting across all provider modules to surface clear diagnostics when context windows are exceeded, improving error messages and preventing silent failures.

New Features
- Introduce `is_token_limit_error` checks in Anthropic, Gemini, Hugging Face, Ollama, OpenAI, and OpenRouter providers.
- Use `format_token_limit_error` to generate user‑friendly error messages for token limit overs.

Enhancements
- Centralize token limit error handling for consistent diagnostics across providers.
- Explicitly re‑raise `RuntimeError` in Hugging Face to preserve original behavior.

Bug Fixes
- None
This commit is contained in:
sankalp committed 2026-06-06 16:45:05 +05:45
1 parent d991af9c47
commit 8069895bc7
6 files changed
+37 -1

No files matched your search

+12
View File
@@ -1,5 +1,7 @@
import requests
from devgen.utils import format_token_limit_error, is_token_limit_error
class HuggingfaceProvider:
"""Generates content using Hugging Face Inference API."""
@@ -46,6 +48,10 @@ class HuggingfaceProvider:
f"Hugging Face model is loading: {err}. "
"Wait a minute and retry, or pick a different model."
)
if is_token_limit_error(err):
raise RuntimeError(
format_token_limit_error("Hugging Face", err)
) from err
raise RuntimeError(f"Hugging Face API error: {err}")
else:
raise RuntimeError(
@@ -55,6 +61,8 @@ class HuggingfaceProvider:
except requests.HTTPError as e:
status = e.response.status_code if e.response is not None else "?"
if is_token_limit_error(e):
raise RuntimeError(format_token_limit_error("Hugging Face", e)) from e
raise RuntimeError(
f"Hugging Face request failed (HTTP {status}): {e}. "
"Check the model id and your token permissions."
@@ -63,5 +71,9 @@ class HuggingfaceProvider:
raise RuntimeError(
f"Hugging Face network error: {e}. Check your connection."
) from e
except RuntimeError:
raise
except Exception as e:
if is_token_limit_error(e):
raise RuntimeError(format_token_limit_error("Hugging Face", e)) from e
raise RuntimeError(f"Hugging Face generation failed: {e}") from e