Fix task configuration with None context parameter in YAML #3697
+55
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix task configuration with None context parameter in YAML
Summary
Fixes #3696 - Tasks can now be configured with
context: None
in YAML without causing aKeyError
.Root Cause: When
context: None
is specified in YAML configuration files,yaml.safe_load()
converts it to the string'None'
instead of Python'sNone
object. The existing code attempted to iterate over this string character by character ('N'
,'o'
,'n'
,'e'
), causing aKeyError: 'N'
when trying to look up task names.The Fix: Added an
isinstance(context_list, list)
check increw_base.py:_map_task_variables()
before iterating the context list. This ensures only actual list values are processed, while None and other non-list values are silently ignored (the task's context remains unset).Changes:
src/crewai/project/crew_base.py
to add type check before iterating contexttest_task_with_none_context_from_yaml
with test configurations intests/config_none_context/
context: None
case and valid context list case work correctlyReview & Testing Checklist for Human
uv.lock
file in the development environment. Please verify:test_task_with_none_context_from_yaml
passes and actually reproduces the original issue when the fix is revertedcontext: None
in YAML and verify it works end-to-endcontext: [task1, task2]
) still work correctlycontext: null
,context: ""
,context: 123
) to ensure they don't cause crashesSuggested Test Plan
uv run pytest tests/test_project.py::test_task_with_none_context_from_yaml -vv
context: None
and run ituv run pytest tests -vv
Notes
isinstance
check is defensive and handles any non-list value, not just the string 'None'