fix(backend): anthropic overloaded fallback failure (#929)

This commit is contained in:
Sharath 2025-07-11 18:36:15 +05:30 committed by GitHub
parent fcf9a97bd9
commit 8cbaf3d44a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 19 deletions

View File

@ -798,16 +798,20 @@ class ResponseProcessor:
logger.error(f"Error processing stream: {str(e)}", exc_info=True)
self.trace.event(name="error_processing_stream", level="ERROR", status_message=(f"Error processing stream: {str(e)}"))
# Save and yield error status message
err_content = {"role": "system", "status_type": "error", "message": str(e)}
err_msg_obj = await self.add_message(
thread_id=thread_id, type="status", content=err_content,
is_llm_message=False, metadata={"thread_run_id": thread_run_id if 'thread_run_id' in locals() else None}
)
if err_msg_obj: yield format_for_yield(err_msg_obj) # Yield the saved error message
# Re-raise the same exception (not a new one) to ensure proper error propagation
logger.critical(f"Re-raising error to stop further processing: {str(e)}")
self.trace.event(name="re_raising_error_to_stop_further_processing", level="ERROR", status_message=(f"Re-raising error to stop further processing: {str(e)}"))
err_content = {"role": "system", "status_type": "error", "message": str(e)}
if (not "AnthropicException - Overloaded" in str(e)):
err_msg_obj = await self.add_message(
thread_id=thread_id, type="status", content=err_content,
is_llm_message=False, metadata={"thread_run_id": thread_run_id if 'thread_run_id' in locals() else None}
)
if err_msg_obj: yield format_for_yield(err_msg_obj) # Yield the saved error message
# Re-raise the same exception (not a new one) to ensure proper error propagation
logger.critical(f"Re-raising error to stop further processing: {str(e)}")
self.trace.event(name="re_raising_error_to_stop_further_processing", level="ERROR", status_message=(f"Re-raising error to stop further processing: {str(e)}"))
else:
logger.error(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}", exc_info=True)
self.trace.event(name="anthropic_exception_overloaded_detected", level="ERROR", status_message=(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}"))
raise # Use bare 'raise' to preserve the original exception with its traceback
finally:

View File

@ -477,13 +477,20 @@ Here are the XML tools available with examples:
if not auto_continue:
break
except Exception as e:
# If there's an exception, log it, yield an error status, and stop execution
logger.error(f"Error in auto_continue_wrapper generator: {str(e)}", exc_info=True)
yield {
"type": "status",
"status": "error",
"message": f"Error in thread processing: {str(e)}"
}
if ("AnthropicException - Overloaded" in str(e)):
logger.error(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}", exc_info=True)
nonlocal llm_model
llm_model = f"openrouter/{llm_model}"
auto_continue = True
continue # Continue the loop
else:
# If there's any other exception, log it, yield an error status, and stop execution
logger.error(f"Error in auto_continue_wrapper generator: {str(e)}", exc_info=True)
yield {
"type": "status",
"status": "error",
"message": f"Error in thread processing: {str(e)}"
}
return # Exit the generator on any error
except Exception as outer_e:
# Catch exceptions from _run_once itself

View File

@ -172,7 +172,7 @@ def prepare_params(
# Apply cache control to the first 4 text blocks across all messages
cache_control_count = 0
max_cache_control_blocks = 4
max_cache_control_blocks = 3
for message in messages:
if cache_control_count >= max_cache_control_blocks:

View File

@ -6,8 +6,8 @@ LOGGING_LEVEL = logging.getLevelNamesMapping().get(
)
renderer = [structlog.processors.JSONRenderer()]
if ENV_MODE.lower() == "local".lower() or ENV_MODE.lower() == "staging".lower():
renderer = [structlog.dev.ConsoleRenderer()]
# if ENV_MODE.lower() == "local".lower() or ENV_MODE.lower() == "staging".lower():
# renderer = [structlog.dev.ConsoleRenderer()]
structlog.configure(
processors=[