From b62c908626a91870f92379ae31fdb822101948b7 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Wed, 12 Feb 2025 21:52:03 +0000 Subject: [PATCH] fix: Add better error handling for litellm model prices fetch - Add content-type check for JSON response - Add proper error handling for all exceptions - Add clear error messages using click.secho - Return None on any error condition Fixes #2116 Co-Authored-By: Joe Moura --- src/crewai/cli/provider.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/crewai/cli/provider.py b/src/crewai/cli/provider.py index 529ca5e26..a8b40f9b9 100644 --- a/src/crewai/cli/provider.py +++ b/src/crewai/cli/provider.py @@ -166,6 +166,12 @@ def fetch_provider_data(cache_file): try: response = requests.get(JSON_URL, stream=True, timeout=60) response.raise_for_status() + + # Add content-type check + if not response.headers.get('content-type', '').startswith('application/json'): + click.secho("Error: Expected JSON response but got different content type", fg="red") + return None + data = download_data(response) with open(cache_file, "w") as f: json.dump(data, f) @@ -174,6 +180,8 @@ def fetch_provider_data(cache_file): click.secho(f"Error fetching provider data: {e}", fg="red") except json.JSONDecodeError: click.secho("Error parsing provider data. Invalid JSON format.", fg="red") + except Exception as e: + click.secho(f"Unexpected error fetching provider data: {e}", fg="red") return None