Fix #2220: Address PR feedback and fix failing tests

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-02-25 13:49:18 +00:00
parent 6d4c591eda
commit b7c988b3ac
2 changed files with 89 additions and 26 deletions

View File

@@ -1,21 +1,42 @@
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"
@pytest.mark.parametrize(
"invalid_model,error_message",
[
(3420, "Invalid model ID: 3420. Model ID cannot be a numeric value without a provider prefix."),
("3420", "Invalid model ID: 3420. Model ID cannot be a numeric value without a provider prefix."),
(3.14, "Invalid model ID: 3.14. Model ID cannot be a numeric value without a provider prefix."),
],
)
def test_invalid_numeric_model_ids(invalid_model, error_message):
"""Test that numeric model IDs are rejected."""
with pytest.raises(ValueError, match=error_message):
LLM(model=invalid_model)
@pytest.mark.parametrize(
"valid_model",
[
"openai/gpt-4",
"gpt-3.5-turbo",
"anthropic/claude-2",
],
)
def test_valid_model_ids(valid_model):
"""Test that valid model IDs are accepted."""
llm = LLM(model=valid_model)
assert llm.model == valid_model
def test_empty_model_id():
"""Test that empty model IDs are rejected."""
with pytest.raises(ValueError, match="Invalid model ID: ''. Model ID cannot be empty or whitespace."):
LLM(model="")
def test_whitespace_model_id():
"""Test that whitespace model IDs are rejected."""
with pytest.raises(ValueError, match="Invalid model ID: ' '. Model ID cannot be empty or whitespace."):
LLM(model=" ")