From 0186403891de9f147b2ec28f5cc21330b0ead5ea Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 11 Aug 2025 21:53:21 -0700 Subject: [PATCH] Limit retry to transient openai API errors. Return non-empty tool output --- .../processor/conversation/openai/utils.py | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index 6f8af68c..fb08205f 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -78,10 +78,8 @@ def _extract_text_for_instructions(content: Union[str, List, Dict, None]) -> str @retry( retry=( retry_if_exception_type(openai._exceptions.APITimeoutError) - | retry_if_exception_type(openai._exceptions.APIError) - | retry_if_exception_type(openai._exceptions.APIConnectionError) | retry_if_exception_type(openai._exceptions.RateLimitError) - | retry_if_exception_type(openai._exceptions.APIStatusError) + | retry_if_exception_type(openai._exceptions.InternalServerError) | retry_if_exception_type(ValueError) ), wait=wait_random_exponential(min=1, max=10), @@ -249,10 +247,8 @@ def completion_with_backoff( @retry( retry=( retry_if_exception_type(openai._exceptions.APITimeoutError) - | retry_if_exception_type(openai._exceptions.APIError) - | retry_if_exception_type(openai._exceptions.APIConnectionError) | retry_if_exception_type(openai._exceptions.RateLimitError) - | retry_if_exception_type(openai._exceptions.APIStatusError) + | retry_if_exception_type(openai._exceptions.InternalServerError) | retry_if_exception_type(ValueError) ), wait=wait_exponential(multiplier=1, min=4, max=10), @@ -415,10 +411,8 @@ async def chat_completion_with_backoff( @retry( retry=( retry_if_exception_type(openai._exceptions.APITimeoutError) - | retry_if_exception_type(openai._exceptions.APIError) - | retry_if_exception_type(openai._exceptions.APIConnectionError) | retry_if_exception_type(openai._exceptions.RateLimitError) - | retry_if_exception_type(openai._exceptions.APIStatusError) + | retry_if_exception_type(openai._exceptions.InternalServerError) | retry_if_exception_type(ValueError) ), wait=wait_random_exponential(min=1, max=10), @@ -529,10 +523,8 @@ def responses_completion_with_backoff( @retry( retry=( retry_if_exception_type(openai._exceptions.APITimeoutError) - | retry_if_exception_type(openai._exceptions.APIError) - | retry_if_exception_type(openai._exceptions.APIConnectionError) | retry_if_exception_type(openai._exceptions.RateLimitError) - | retry_if_exception_type(openai._exceptions.APIStatusError) + | retry_if_exception_type(openai._exceptions.InternalServerError) | retry_if_exception_type(ValueError) ), wait=wait_exponential(multiplier=1, min=4, max=10), @@ -764,7 +756,7 @@ def format_message_for_api(raw_messages: List[ChatMessage], api_base_url: str) - { "type": "function_call_output", "call_id": tool_call_id, - "output": part.get("content"), + "output": part.get("content") or "No output", } ) else: @@ -773,7 +765,7 @@ def format_message_for_api(raw_messages: List[ChatMessage], api_base_url: str) - "role": "tool", "tool_call_id": tool_call_id, "name": part.get("name"), - "content": part.get("content"), + "content": part.get("content") or "No output", } ) continue