diff --git a/tests/test_empty_messages.py b/tests/test_empty_messages.py index efffa294a..564fb65a1 100644 --- a/tests/test_empty_messages.py +++ b/tests/test_empty_messages.py @@ -1,11 +1,12 @@ -from unittest.mock import patch +from unittest.mock import patch, MagicMock import pytest from crewai.llm import LLM -def test_empty_messages_validation(): +@patch('crewai.llm.LLM._prepare_completion_params') +def test_empty_messages_validation(mock_prepare): """ Test that LLM.call() raises a ValueError when an empty messages list is passed. This prevents the IndexError in LiteLLM's ollama_pt() function. @@ -17,6 +18,33 @@ def test_empty_messages_validation(): with pytest.raises(ValueError, match="Messages list cannot be empty"): llm.call(messages=None) + + mock_prepare.assert_not_called() + + +@patch('crewai.llm.LLM._prepare_completion_params') +def test_empty_string_message(mock_prepare): + """ + Test that LLM.call() raises a ValueError when an empty string message is passed. + """ + llm = LLM(model="gpt-3.5-turbo") + + with pytest.raises(ValueError, match="Messages list cannot be empty"): + llm.call(messages="") + + mock_prepare.assert_not_called() + + +@patch('crewai.llm.LLM._prepare_completion_params') +def test_invalid_message_format(mock_prepare): + """ + Test that LLM.call() raises a TypeError when a message with invalid format is passed. + """ + mock_prepare.side_effect = TypeError("Invalid message format") + llm = LLM(model="gpt-3.5-turbo") + + with pytest.raises(TypeError, match="Invalid message format"): + llm.call(messages=[{}]) @pytest.mark.vcr(filter_headers=["authorization"])