Skip to content

Commit b7dca59

Browse files
jnjpngLetta Bot
andauthored
fix: retry on 500 and 503 for gemini [LET-4185]
* handle 500 and 503 * timeout --------- Co-authored-by: Letta Bot <[email protected]>
1 parent eefd2fa commit b7dca59

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

letta/llm_api/google_vertex_client.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import List, Optional
44

55
from google import genai
6+
from google.genai import errors
67
from google.genai.types import (
78
FunctionCallingConfig,
89
FunctionCallingConfigMode,
@@ -67,11 +68,21 @@ async def request_async(self, request_data: dict, llm_config: LLMConfig) -> dict
6768
retry_count = 1
6869
should_retry = True
6970
while should_retry and retry_count <= self.MAX_RETRIES:
70-
response = await client.aio.models.generate_content(
71-
model=llm_config.model,
72-
contents=request_data["contents"],
73-
config=request_data["config"],
74-
)
71+
try:
72+
response = await client.aio.models.generate_content(
73+
model=llm_config.model,
74+
contents=request_data["contents"],
75+
config=request_data["config"],
76+
)
77+
except errors.APIError as e:
78+
# Retry on 503 and 500 errors as well, usually ephemeral from Gemini
79+
if e.code == 503 or e.code == 500:
80+
logger.warning(f"Received {e}, retrying {retry_count}/{self.MAX_RETRIES}")
81+
retry_count += 1
82+
continue
83+
raise e
84+
except Exception as e:
85+
raise e
7586
response_data = response.model_dump()
7687
is_malformed_function_call = self.is_malformed_function_call(response_data)
7788
if is_malformed_function_call:

tests/integration_test_send_message.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ def test_background_token_streaming_tool_call(
13341334
messages=messages_to_send,
13351335
stream_tokens=True,
13361336
background=True,
1337+
request_options={"timeout_in_seconds": 300},
13371338
)
13381339
verify_token_streaming = (
13391340
llm_config.model_endpoint_type in ["anthropic", "openai", "bedrock"] and "claude-3-5-sonnet" not in llm_config.model

0 commit comments

Comments
 (0)