fix: resolve type annotation mismatch in Task.context field

- Update context field type annotation from Optional[List[Task]] to Union[List[Task], _NotSpecified, None]
- Add ConfigDict(arbitrary_types_allowed=True) to Task model to support _NotSpecified type
- Add comprehensive tests covering the type annotation fix and sentinel behavior
- Fixes issue #3019 where context field default NOT_SPECIFIED didn't match type annotation

The fix maintains backward compatibility while making the type annotation accurate.
The NOT_SPECIFIED sentinel distinguishes between 'not passed' and 'explicitly passed None'.

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2025-06-17 09:09:40 +00:00
parent db1e9e9b9a
commit 59f4c71370
2 changed files with 119 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ from typing import (
from pydantic import (
UUID4,
BaseModel,
ConfigDict,
Field,
PrivateAttr,
field_validator,
@@ -39,7 +40,7 @@ from crewai.tasks.output_format import OutputFormat
from crewai.tasks.task_output import TaskOutput
from crewai.tools.base_tool import BaseTool
from crewai.utilities.config import process_config
from crewai.utilities.constants import NOT_SPECIFIED
from crewai.utilities.constants import NOT_SPECIFIED, _NotSpecified
from crewai.utilities.guardrail import process_guardrail, GuardrailResult
from crewai.utilities.converter import Converter, convert_to_model
from crewai.utilities.events import (
@@ -78,6 +79,8 @@ class Task(BaseModel):
used_tools: int = 0
tools_errors: int = 0
delegations: int = 0
model_config = ConfigDict(arbitrary_types_allowed=True)
i18n: I18N = I18N()
name: Optional[str] = Field(default=None)
prompt_context: Optional[str] = None
@@ -95,7 +98,7 @@ class Task(BaseModel):
agent: Optional[BaseAgent] = Field(
description="Agent responsible for execution the task.", default=None
)
context: Optional[List["Task"]] = Field(
context: Union[List["Task"], _NotSpecified, None] = Field(
description="Other tasks that will have their output used as context for this task.",
default=NOT_SPECIFIED,
)