diff --git a/lib/crewai/src/crewai/utilities/config.py b/lib/crewai/src/crewai/utilities/config.py index 95a542c5e..d558d6dd0 100644 --- a/lib/crewai/src/crewai/utilities/config.py +++ b/lib/crewai/src/crewai/utilities/config.py @@ -14,7 +14,16 @@ def process_config( Returns: The updated values dictionary. + + Raises: + ValueError: If values is not a dictionary (e.g. when an invalid type + is passed for a field that expects a Pydantic model). """ + if not isinstance(values, dict): + raise ValueError( + f"{model_class.__name__} expected a mapping/dictionary, " + f"got {type(values).__name__}" + ) config = values.get("config", {}) if not config: return values diff --git a/lib/crewai/tests/test_task.py b/lib/crewai/tests/test_task.py index 9a0010d89..140098e82 100644 --- a/lib/crewai/tests/test_task.py +++ b/lib/crewai/tests/test_task.py @@ -1748,3 +1748,50 @@ def test_async_execution_fails(): with pytest.raises(RuntimeError, match="boom!"): execution = task.execute_async(agent=researcher) execution.result() + + +def test_task_with_invalid_agent_string_raises_validation_error(): + with pytest.raises(ValidationError) as exc_info: + Task( + description="Test task", + expected_output="Test output", + agent="not_an_agent", + ) + assert "BaseAgent expected a mapping/dictionary, got str" in str(exc_info.value) + + +def test_task_with_invalid_agent_int_raises_validation_error(): + with pytest.raises(ValidationError) as exc_info: + Task( + description="Test task", + expected_output="Test output", + agent=123, + ) + assert "BaseAgent expected a mapping/dictionary, got int" in str(exc_info.value) + + +def test_task_with_invalid_agent_list_raises_validation_error(): + with pytest.raises(ValidationError) as exc_info: + Task( + description="Test task", + expected_output="Test output", + agent=[1, 2, 3], + ) + assert "BaseAgent expected a mapping/dictionary, got list" in str(exc_info.value) + + +def test_task_with_none_agent_is_valid(): + task = Task( + description="Test task", + expected_output="Test output", + agent=None, + ) + assert task.agent is None + + +def test_task_without_agent_is_valid(): + task = Task( + description="Test task", + expected_output="Test output", + ) + assert task.agent is None