From 8a4f3c8cfc12acb7810d1884bf3109914c0bbc3c Mon Sep 17 00:00:00 2001 From: lucasgomide Date: Tue, 1 Apr 2025 10:13:29 -0300 Subject: [PATCH] feat: cleanup Pydantic warning A several warnings were addressed following by https://docs.pydantic.dev/2.10/migration --- src/crewai/tools/base_tool.py | 15 +++++++-------- src/crewai/utilities/converter.py | 5 +++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.py index b3c0f997c..dc69b02a2 100644 --- a/src/crewai/tools/base_tool.py +++ b/src/crewai/tools/base_tool.py @@ -7,29 +7,27 @@ from pydantic import ( BaseModel, ConfigDict, Field, - PydanticDeprecatedSince20, create_model, - validator, + field_validator, ) from pydantic import BaseModel as PydanticBaseModel from crewai.tools.structured_tool import CrewStructuredTool -# Ignore all "PydanticDeprecatedSince20" warnings globally -warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20) - class BaseTool(BaseModel, ABC): class _ArgsSchemaPlaceholder(PydanticBaseModel): pass - model_config = ConfigDict() + model_config = ConfigDict(arbitrary_types_allowed=True) name: str """The unique name of the tool that clearly communicates its purpose.""" description: str """Used to tell the model how/when/why to use the tool.""" - args_schema: Type[PydanticBaseModel] = Field(default_factory=_ArgsSchemaPlaceholder) + args_schema: Type[PydanticBaseModel] = Field( + default_factory=_ArgsSchemaPlaceholder, validate_default=True + ) """The schema for the arguments that the tool accepts.""" description_updated: bool = False """Flag to check if the description has been updated.""" @@ -38,7 +36,8 @@ class BaseTool(BaseModel, ABC): result_as_answer: bool = False """Flag to check if the tool should be the final agent answer.""" - @validator("args_schema", always=True, pre=True) + @field_validator("args_schema", mode="before") + @classmethod def _default_args_schema( cls, v: Type[PydanticBaseModel] ) -> Type[PydanticBaseModel]: diff --git a/src/crewai/utilities/converter.py b/src/crewai/utilities/converter.py index 991185f4a..b16677ace 100644 --- a/src/crewai/utilities/converter.py +++ b/src/crewai/utilities/converter.py @@ -287,8 +287,9 @@ def generate_model_description(model: Type[BaseModel]) -> str: else: return str(field_type) - fields = model.__annotations__ + fields = model.model_fields field_descriptions = [ - f'"{name}": {describe_field(type_)}' for name, type_ in fields.items() + f'"{name}": {describe_field(field.annotation)}' + for name, field in fields.items() ] return "{\n " + ",\n ".join(field_descriptions) + "\n}"