mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-01 07:13:00 +00:00
fix: reject non-dict values in process_config with clear ValidationError
Fixes #4419. When an invalid type (e.g. a string) was passed for a field that expects a Pydantic model like BaseAgent, the process_config function would crash with AttributeError: 'str' object has no attribute 'get'. Now process_config raises ValueError for non-dict inputs, which Pydantic wraps into a proper ValidationError with a clear message like: 'BaseAgent expected a mapping/dictionary, got str' Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -14,7 +14,16 @@ def process_config(
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The updated values dictionary.
|
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", {})
|
config = values.get("config", {})
|
||||||
if not config:
|
if not config:
|
||||||
return values
|
return values
|
||||||
|
|||||||
@@ -1748,3 +1748,50 @@ def test_async_execution_fails():
|
|||||||
with pytest.raises(RuntimeError, match="boom!"):
|
with pytest.raises(RuntimeError, match="boom!"):
|
||||||
execution = task.execute_async(agent=researcher)
|
execution = task.execute_async(agent=researcher)
|
||||||
execution.result()
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user