Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

fix: Catch transitive Azure errors

Clémence Lesné8d1508d2d2e7
diff --git a/persistence/ai_search.py b/persistence/ai_search.py
index 6fa9cd1..58990cf 100644
--- a/persistence/ai_search.py
+++ b/persistence/ai_search.py
@@ -1,5 +1,9 @@
from azure.core.credentials import AzureKeyCredential
-from azure.core.exceptions import HttpResponseError, ServiceRequestError
+from azure.core.exceptions import (
+ HttpResponseError,
+ ServiceRequestError,
+ ServiceResponseError,
+)
from azure.search.documents.aio import SearchClient
from azure.search.documents.models import VectorizableTextQuery
from contextlib import asynccontextmanager
@@ -13,6 +17,12 @@ from persistence.isearch import ISearch
from pydantic import TypeAdapter
from pydantic import ValidationError
from typing import AsyncGenerator, List, Optional
+from tenacity import (
+ retry,
+ stop_after_attempt,
+ wait_random_exponential,
+ retry_if_exception_type,
+)
_logger = build_logger(__name__)
@@ -29,6 +39,12 @@ class AiSearchSearch(ISearch):
self._config = config
super().__init__(cache)
+ @retry(
+ reraise=True,
+ retry=retry_if_exception_type(ServiceResponseError),
+ stop=stop_after_attempt(3),
+ wait=wait_random_exponential(multiplier=0.5, max=30),
+ )
async def training_asearch_all(
self, text: str, call: CallModel
) -> Optional[List[TrainingModel]]:
diff --git a/persistence/cosmos.py b/persistence/cosmos.py
index 52abdd8..c030245 100644
--- a/persistence/cosmos.py
+++ b/persistence/cosmos.py
@@ -1,3 +1,4 @@
+from azure.core.exceptions import ServiceResponseError
from azure.cosmos.aio import CosmosClient, ContainerProxy
from azure.cosmos.exceptions import CosmosHttpResponseError
from contextlib import asynccontextmanager
@@ -11,6 +12,12 @@ from persistence.istore import IStore
from pydantic import ValidationError
from typing import AsyncGenerator, List, Optional
from uuid import UUID
+from tenacity import (
+ retry,
+ stop_after_attempt,
+ wait_random_exponential,
+ retry_if_exception_type,
+)
_logger = build_logger(__name__)
@@ -23,6 +30,12 @@ class CosmosStore(IStore):
_logger.info(f"Using Cosmos DB {config.database}/{config.container}")
self._config = config
+ @retry(
+ reraise=True,
+ retry=retry_if_exception_type(ServiceResponseError),
+ stop=stop_after_attempt(3),
+ wait=wait_random_exponential(multiplier=0.5, max=30),
+ )
async def call_aget(self, call_id: UUID) -> Optional[CallModel]:
_logger.debug(f"Loading call {call_id}")
res = None
@@ -43,6 +56,12 @@ class CosmosStore(IStore):
_logger.error(f"Error accessing CosmosDB, {e}")
return res
+ @retry(
+ reraise=True,
+ retry=retry_if_exception_type(ServiceResponseError),
+ stop=stop_after_attempt(3),
+ wait=wait_random_exponential(multiplier=0.5, max=30),
+ )
async def call_aset(self, call: CallModel) -> bool:
data = jsonable_encoder(call.model_dump(), exclude_none=True)
data["id"] = str(call.call_id) # CosmosDB requires an id field
@@ -55,6 +74,12 @@ class CosmosStore(IStore):
_logger.error(f"Error accessing CosmosDB: {e}")
return False
+ @retry(
+ reraise=True,
+ retry=retry_if_exception_type(ServiceResponseError),
+ stop=stop_after_attempt(3),
+ wait=wait_random_exponential(multiplier=0.5, max=30),
+ )
async def call_asearch_one(self, phone_number: str) -> Optional[CallModel]:
_logger.debug(f"Loading last call for {phone_number}")
res = None
@@ -90,6 +115,12 @@ class CosmosStore(IStore):
_logger.error(f"Error accessing CosmosDB: {e}")
return res
+ @retry(
+ reraise=True,
+ retry=retry_if_exception_type(ServiceResponseError),
+ stop=stop_after_attempt(3),
+ wait=wait_random_exponential(multiplier=0.5, max=30),
+ )
async def call_asearch_all(self, phone_number: str) -> Optional[List[CallModel]]:
_logger.debug(f"Loading all calls for {phone_number}")
calls = []