diff --git a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py index 2f9b61f7b95..4c18591a6f5 100644 --- a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py +++ b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py @@ -581,7 +581,7 @@ class RedisUpdateBuffer: RedisPipelineRpushOperation(key=REDIS_TOOL_DISCOVERY_BUFFER_KEY, values=[safe_dumps(items)]) ] ) - except Exception as e: + except (RedisError, OSError, asyncio.TimeoutError) as e: verbose_proxy_logger.error( "Tool discovery - failed to push %d tool rows to Redis, restoring them for the next tick. Error: %s", len(items), @@ -590,6 +590,7 @@ class RedisUpdateBuffer: for item in items: tool_discovery_queue.add_update(item) + @with_service_target(SPEND_QUEUE_TARGET) async def get_all_tool_discoveries_from_redis_buffer(self) -> tuple[ToolDiscoveryQueueItem, ...]: """Drain every worker's tool rows and fold them to one row per tool_name.""" if self.redis_cache is None: @@ -605,6 +606,7 @@ class RedisUpdateBuffer: batches: Final = tuple(_TOOL_DISCOVERY_ITEMS.validate_json(entry) for entry in entries) return aggregate_tool_discoveries(item for batch in batches for item in batch) + @with_service_target(SPEND_QUEUE_TARGET) async def store_spend_logs_in_redis( self, rows: Sequence[SpendLogRow], diff --git a/litellm/proxy/db/db_transaction_queue/tool_discovery_queue.py b/litellm/proxy/db/db_transaction_queue/tool_discovery_queue.py index 68c41db9110..0a60ff77b27 100644 --- a/litellm/proxy/db/db_transaction_queue/tool_discovery_queue.py +++ b/litellm/proxy/db/db_transaction_queue/tool_discovery_queue.py @@ -4,18 +4,22 @@ tool with the number of times it was invoked since the previous flush. """ from collections.abc import Iterable -from typing import Final, cast +from typing import Final + +from pydantic import TypeAdapter from litellm._logging import verbose_proxy_logger from litellm.proxy._types import ToolDiscoveryQueueItem +_ITEM: Final = TypeAdapter(ToolDiscoveryQueueItem) + def _merged(existing: ToolDiscoveryQueueItem | None, item: ToolDiscoveryQueueItem) -> ToolDiscoveryQueueItem: """The first sighting keeps its origin and caller fields, later sightings only add to call_count.""" increment: Final = item.get("call_count", 1) if existing is None: - return cast(ToolDiscoveryQueueItem, {**item, "call_count": increment}) - return cast(ToolDiscoveryQueueItem, {**existing, "call_count": existing.get("call_count", 1) + increment}) + return _ITEM.validate_python({**item, "call_count": increment}) + return _ITEM.validate_python({**existing, "call_count": existing.get("call_count", 1) + increment}) def aggregate_tool_discoveries(items: Iterable[ToolDiscoveryQueueItem]) -> tuple[ToolDiscoveryQueueItem, ...]: