fix(bedesten): return structured 429 response instead of raising
Bedesten API can intermittently return HTTP 429 Too Many Requests. Previously the tool raised, leaving the LLM with an unhandled error. Now search_bedesten_unified returns a dict with error="rate_limit_exceeded" and get_bedesten_document_markdown returns a BedestenDocumentMarkdown whose markdown_content describes the rate limit, so the model can inform the user and retry. Non-429 errors still propagate. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
7ed9c25687
commit
8f04010c57
+42
-4
@@ -1160,7 +1160,7 @@ For best results, use exact phrases with quotes for legal terms."""),
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = await bedesten_client_instance.search_documents(search_request)
|
response = await bedesten_client_instance.search_documents(search_request)
|
||||||
|
|
||||||
if response.data is None:
|
if response.data is None:
|
||||||
return {
|
return {
|
||||||
"decisions": [],
|
"decisions": [],
|
||||||
@@ -1170,11 +1170,11 @@ For best results, use exact phrases with quotes for legal terms."""),
|
|||||||
"searched_courts": court_types,
|
"searched_courts": court_types,
|
||||||
"error": "No data returned from Bedesten API"
|
"error": "No data returned from Bedesten API"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add null safety checks for response.data fields
|
# Add null safety checks for response.data fields
|
||||||
emsal_karar_list = response.data.emsalKararList if hasattr(response.data, 'emsalKararList') and response.data.emsalKararList is not None else []
|
emsal_karar_list = response.data.emsalKararList if hasattr(response.data, 'emsalKararList') and response.data.emsalKararList is not None else []
|
||||||
total_records = response.data.total if hasattr(response.data, 'total') and response.data.total is not None else 0
|
total_records = response.data.total if hasattr(response.data, 'total') and response.data.total is not None else 0
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"decisions": [d.model_dump() for d in emsal_karar_list],
|
"decisions": [d.model_dump() for d in emsal_karar_list],
|
||||||
"total_records": total_records,
|
"total_records": total_records,
|
||||||
@@ -1182,6 +1182,26 @@ For best results, use exact phrases with quotes for legal terms."""),
|
|||||||
"page_size": pageSize,
|
"page_size": pageSize,
|
||||||
"searched_courts": court_types
|
"searched_courts": court_types
|
||||||
}
|
}
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
if e.response.status_code == 429:
|
||||||
|
retry_after = e.response.headers.get("Retry-After", "")
|
||||||
|
logger.warning(f"Bedesten API rate limit (429) for search; retry-after={retry_after!r}")
|
||||||
|
return {
|
||||||
|
"decisions": [],
|
||||||
|
"total_records": 0,
|
||||||
|
"requested_page": pageNumber,
|
||||||
|
"page_size": pageSize,
|
||||||
|
"searched_courts": court_types,
|
||||||
|
"error": "rate_limit_exceeded",
|
||||||
|
"status_code": 429,
|
||||||
|
"retry_after": retry_after,
|
||||||
|
"message": (
|
||||||
|
"Bedesten API rate limit aşıldı (HTTP 429 Too Many Requests). "
|
||||||
|
"Lütfen kısa bir süre bekleyip aramayı tekrar deneyin."
|
||||||
|
),
|
||||||
|
}
|
||||||
|
logger.exception("Error in tool 'search_bedesten_unified'")
|
||||||
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Error in tool 'search_bedesten_unified'")
|
logger.exception("Error in tool 'search_bedesten_unified'")
|
||||||
raise
|
raise
|
||||||
@@ -1204,8 +1224,26 @@ async def get_bedesten_document_markdown(
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return await bedesten_client_instance.get_document_as_markdown(documentId)
|
return await bedesten_client_instance.get_document_as_markdown(documentId)
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
if e.response.status_code == 429:
|
||||||
|
retry_after = e.response.headers.get("Retry-After", "")
|
||||||
|
logger.warning(f"Bedesten API rate limit (429) for document {documentId}; retry-after={retry_after!r}")
|
||||||
|
message = (
|
||||||
|
"Bedesten API rate limit aşıldı (HTTP 429 Too Many Requests). "
|
||||||
|
"Lütfen kısa bir süre bekleyip belgeyi tekrar talep edin."
|
||||||
|
)
|
||||||
|
if retry_after:
|
||||||
|
message += f" Retry-After: {retry_after}"
|
||||||
|
return BedestenDocumentMarkdown(
|
||||||
|
documentId=documentId,
|
||||||
|
markdown_content=f"ERROR (rate_limit_exceeded, HTTP 429): {message}",
|
||||||
|
source_url=f"https://mevzuat.adalet.gov.tr/ictihat/{documentId}",
|
||||||
|
mime_type=None,
|
||||||
|
)
|
||||||
|
logger.exception("Error in tool 'get_bedesten_document_markdown'")
|
||||||
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception("Error in tool 'get_kyb_bedesten_document_markdown'")
|
logger.exception("Error in tool 'get_bedesten_document_markdown'")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user