Fix #2740: Add validation for empty messages lists to prevent IndexError in LiteLLM's ollama_pt()

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-02 08:05:12 +00:00
parent 2902201bfa
commit 4de80f51e7
2 changed files with 46 additions and 4 deletions

View File

@@ -0,0 +1,30 @@
import pytest
from unittest.mock import patch
from crewai.llm import LLM
def test_empty_messages_validation():
"""
Test that LLM.call() raises a ValueError when an empty messages list is passed.
This prevents the IndexError in LiteLLM's ollama_pt() function.
"""
llm = LLM(model="gpt-3.5-turbo") # Any model will do for this test
with pytest.raises(ValueError, match="Messages list cannot be empty"):
llm.call(messages=[])
with pytest.raises(ValueError, match="Messages list cannot be empty"):
llm.call(messages=None)
@pytest.mark.vcr(filter_headers=["authorization"])
def test_ollama_model_empty_messages():
"""
Test that LLM.call() with an Ollama model raises a ValueError
when an empty messages list is passed.
"""
llm = LLM(model="ollama/llama3")
with pytest.raises(ValueError, match="Messages list cannot be empty"):
llm.call(messages=[])