Hamdy Doma commited on
Commit
34ee172
·
1 Parent(s): 3e9b680

Implement smart retry logic for OpenRouter to handle unsupported parameters

Browse files
Files changed (1) hide show
  1. providers/open_router/client.py +24 -0
providers/open_router/client.py CHANGED
@@ -33,6 +33,30 @@ class OpenRouterProvider(OpenAICompatibleProvider):
33
  thinking_enabled=self._is_thinking_enabled(request),
34
  )
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  def _handle_extra_reasoning(
37
  self, delta: Any, sse: SSEBuilder, *, thinking_enabled: bool
38
  ) -> Iterator[str]:
 
33
  thinking_enabled=self._is_thinking_enabled(request),
34
  )
35
 
36
+ def _get_retry_request_body(self, error: Exception, body: dict) -> Optional[dict]:
37
+ """Retry without reasoning/thinking if provider rejects the request format."""
38
+ from providers.exceptions import InvalidRequestError
39
+ import openai
40
+
41
+ # If it's a 400 Bad Request, it might be due to unsupported reasoning parameters
42
+ if isinstance(error, (InvalidRequestError, openai.BadRequestError)):
43
+ new_body = body.copy()
44
+ modified = False
45
+
46
+ # Remove reasoning from extra_body if present
47
+ if "extra_body" in new_body:
48
+ extra = new_body["extra_body"].copy()
49
+ if "reasoning" in extra:
50
+ logger.info("OPENROUTER_RETRY: Removing unsupported reasoning parameter")
51
+ del extra["reasoning"]
52
+ new_body["extra_body"] = extra
53
+ modified = True
54
+
55
+ if modified:
56
+ return new_body
57
+
58
+ return None
59
+
60
  def _handle_extra_reasoning(
61
  self, delta: Any, sse: SSEBuilder, *, thinking_enabled: bool
62
  ) -> Iterator[str]: