fix: Resolve type errors identified by mypy

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-04-10 15:56:24 +00:00
parent 44abceffaa
commit 2e5f0509bf
4 changed files with 31 additions and 10 deletions

View File

@@ -7,6 +7,19 @@ from crewai.utilities import I18N
i18n = I18N()
def _get_add_image_tool_name() -> str:
"""Safely get the tool name from i18n."""
tool_info = i18n.tools("add_image")
if isinstance(tool_info, dict):
return tool_info.get("name", "Add Image")
return "Add Image" # Default name if not a dict
def _get_add_image_tool_description() -> str:
"""Safely get the tool description from i18n."""
tool_info = i18n.tools("add_image")
if isinstance(tool_info, dict):
return tool_info.get("description", "Tool for adding images to the content")
return "Tool for adding images to the content" # Default description if not a dict
class AddImageToolSchema(BaseModel):
image_url: str = Field(..., description="The URL or path of the image to add")
@@ -18,10 +31,8 @@ class AddImageToolSchema(BaseModel):
class AddImageTool(BaseTool):
"""Tool for adding images to the content"""
name: str = Field(default_factory=lambda: i18n.tools("add_image")["name"]) # type: ignore
description: str = Field(
default_factory=lambda: i18n.tools("add_image")["description"]
) # type: ignore
name: str = Field(default_factory=_get_add_image_tool_name)
description: str = Field(default_factory=_get_add_image_tool_description)
args_schema: type[BaseModel] = AddImageToolSchema
def _run(

View File

@@ -81,13 +81,13 @@ class BaseAgentTool(BaseTool):
available_agents = [agent.role for agent in self.agents]
logger.debug(f"Available agents: {available_agents}")
agent = [ # type: ignore # Incompatible types in assignment (expression has type "list[BaseAgent]", variable has type "str | None")
matching_agents = [
available_agent
for available_agent in self.agents
if self.sanitize_agent_name(available_agent.role) == sanitized_name
]
logger.debug(
f"Found {len(agent)} matching agents for role '{sanitized_name}'"
f"Found {len(matching_agents)} matching agents for role '{sanitized_name}'"
)
except (AttributeError, ValueError) as e:
# Handle specific exceptions that might occur during role name processing
@@ -101,7 +101,7 @@ class BaseAgentTool(BaseTool):
error=str(e),
)
if not agent:
if not matching_agents:
# No matching agent found after sanitization
return self.i18n.errors("agent_tool_unexisting_coworker").format(
coworkers="\n".join(
@@ -113,7 +113,7 @@ class BaseAgentTool(BaseTool):
error=f"No agent found with role '{sanitized_name}'",
)
agent = agent[0]
agent: BaseAgent = matching_agents[0]
try:
task_with_assigned_agent = Task(
description=task,

View File

@@ -380,6 +380,7 @@ class ToolUsage:
else ToolCalling
)
converter = Converter(
agent=None, # Agent not needed here as function calling is supported/used
text=f"Only tools available:\n###\n{self._render()}\n\nReturn a valid schema for the tool, the tool name must be exactly equal one of the options, use this text to inform the valid output schema:\n\n### TEXT \n{tool_string}",
llm=self.function_calling_llm,
model=model,

View File

@@ -65,13 +65,18 @@ class TaskEvaluator:
instructions = f"{instructions}\n\nReturn only valid JSON with the following schema:\n```json\n{model_schema}\n```"
converter = Converter(
agent=self.original_agent, # Pass agent
llm=self.llm,
text=evaluation_query,
model=TaskEvaluation,
instructions=instructions,
)
return converter.to_pydantic()
result = converter.to_pydantic()
if isinstance(result, TaskEvaluation):
return result
else:
raise TypeError(f"Expected TaskEvaluation, got {type(result)}")
def evaluate_training_data(
self, training_data: dict, agent_id: str
@@ -134,6 +139,7 @@ class TaskEvaluator:
instructions = f"{instructions}\n\nThe json should have the following structure, with the following keys:\n{model_schema}"
converter = Converter(
agent=self.original_agent, # Pass agent
llm=self.llm,
text=evaluation_query,
model=TrainingTaskEvaluation,
@@ -141,4 +147,7 @@ class TaskEvaluator:
)
pydantic_result = converter.to_pydantic()
return pydantic_result
if isinstance(pydantic_result, TrainingTaskEvaluation):
return pydantic_result
else:
raise TypeError(f"Expected TrainingTaskEvaluation, got {type(pydantic_result)}")