From 6d4c591eda71d1cc825b9062361c1375cf6e8f0f Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:40:19 +0000 Subject: [PATCH] Fix #2220: Add validation for numeric model IDs in LLM class Co-Authored-By: Joe Moura --- src/crewai/llm.py | 7 +++++++ tests/unit/test_llm.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/unit/test_llm.py diff --git a/src/crewai/llm.py b/src/crewai/llm.py index 5d6a0ccf5..e574435aa 100644 --- a/src/crewai/llm.py +++ b/src/crewai/llm.py @@ -137,6 +137,13 @@ class LLM: self.context_window_size = 0 self.kwargs = kwargs + # Validate model ID to ensure it's not a numeric value without a provider prefix + if isinstance(self.model, (int, float)) or (isinstance(self.model, str) and self.model.isdigit()): + raise ValueError( + f"Invalid model ID: {self.model}. Model ID cannot be a numeric value without a provider prefix. " + "Please specify a valid model ID with a provider prefix, e.g., 'openai/gpt-4'." + ) + litellm.drop_params = True litellm.set_verbose = False self.set_callbacks(callbacks) diff --git a/tests/unit/test_llm.py b/tests/unit/test_llm.py new file mode 100644 index 000000000..73f215bc3 --- /dev/null +++ b/tests/unit/test_llm.py @@ -0,0 +1,21 @@ +import pytest + +from crewai.llm import LLM + + +def test_numeric_model_id_validation(): + # Test with integer model ID + with pytest.raises(ValueError, match="Invalid model ID: 3420. Model ID cannot be a numeric value without a provider prefix."): + LLM(model=3420) + + # Test with string numeric model ID + with pytest.raises(ValueError, match="Invalid model ID: 3420. Model ID cannot be a numeric value without a provider prefix."): + LLM(model="3420") + + # Test with valid model ID + llm = LLM(model="openai/gpt-4") + assert llm.model == "openai/gpt-4" + + # Test with valid model ID that contains numbers + llm = LLM(model="gpt-3.5-turbo") + assert llm.model == "gpt-3.5-turbo"