From 168c716c58b06dbe19a5c4ddfed4c83d9c8b39a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Est=C3=A9vez?= Date: Tue, 12 Nov 2024 20:23:24 -0500 Subject: [PATCH] fix: update error handling in request for litellm models (#4536) handle request exception for litellm models --- .../base/langflow/base/astra_assistants/util.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/base/astra_assistants/util.py b/src/backend/base/langflow/base/astra_assistants/util.py index fd36a7e03..13a79e01d 100644 --- a/src/backend/base/langflow/base/astra_assistants/util.py +++ b/src/backend/base/langflow/base/astra_assistants/util.py @@ -4,11 +4,13 @@ import json import os import pkgutil import threading +from json.decoder import JSONDecodeError import astra_assistants.tools as astra_assistants_tools import requests from astra_assistants import OpenAIWithDefaultKey, patch from astra_assistants.tools.tool_interface import ToolInterface +from requests.exceptions import RequestException from langflow.services.cache.utils import CacheMiss @@ -26,8 +28,14 @@ def get_patched_openai_client(shared_component_cache): url = "https://raw.githubusercontent.com/BerriAI/litellm/refs/heads/main/model_prices_and_context_window.json" -response = requests.get(url, timeout=10) -data = json.loads(response.text) +try: + response = requests.get(url, timeout=10) + response.raise_for_status() + data = json.loads(response.text) +except RequestException: + data = {} +except JSONDecodeError: + data = {} # Extract the model names into a Python list litellm_model_names = [model for model in data if model != "sample_spec"]