From ce9c343c0ad1c9588d420b35e34b78f2ab7f9b01 Mon Sep 17 00:00:00 2001 From: ftoppi Date: Thu, 4 Apr 2024 15:11:46 +0200 Subject: [PATCH] tasks.py: don't call Converter when model response is valid (#406) * tasks.py: don't call Converter when model response is valid Try to convert the task output to the expected Pydantic model before sending it to Converter, maybe the model got it right. --- src/crewai/task.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/crewai/task.py b/src/crewai/task.py index 43b9c26c2..273fdfefa 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -221,6 +221,16 @@ class Task(BaseModel): if self.output_pydantic or self.output_json: model = self.output_pydantic or self.output_json + + # try to convert task_output directly to pydantic/json + try: + exported_result = model.model_validate_json(result) + if self.output_json: + return exported_result.model_dump() + return exported_result + except Exception: + pass + llm = self.agent.function_calling_llm or self.agent.llm if not self._is_gpt(llm):