mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 01:28:14 +00:00
Fix result_as_answer=True bypassing conversion
- Add ToolAnswerResult wrapper class to signal when tool results should bypass conversion - Modify agent.execute_task to return ToolAnswerResult when result_as_answer=True - Update task._export_output to skip conversion for ToolAnswerResult instances - Add comprehensive tests covering the fix and edge cases - Fixes issue #3335 where tools with result_as_answer=True still triggered LLM conversion Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
@@ -477,7 +477,8 @@ class Agent(BaseAgent):
|
||||
# result_as_answer set to True
|
||||
for tool_result in self.tools_results: # type: ignore # Item "None" of "list[Any] | None" has no attribute "__iter__" (not iterable)
|
||||
if tool_result.get("result_as_answer", False):
|
||||
result = tool_result["result"]
|
||||
from crewai.tools.tool_types import ToolAnswerResult
|
||||
result = ToolAnswerResult(tool_result["result"])
|
||||
crewai_event_bus.emit(
|
||||
self,
|
||||
event=AgentExecutionCompletedEvent(agent=self, task=task, output=result),
|
||||
|
||||
@@ -38,6 +38,7 @@ from crewai.security import Fingerprint, SecurityConfig
|
||||
from crewai.tasks.output_format import OutputFormat
|
||||
from crewai.tasks.task_output import TaskOutput
|
||||
from crewai.tools.base_tool import BaseTool
|
||||
from crewai.tools.tool_types import ToolAnswerResult
|
||||
from crewai.utilities.config import process_config
|
||||
from crewai.utilities.constants import NOT_SPECIFIED, _NotSpecified
|
||||
from crewai.utilities.guardrail import process_guardrail, GuardrailResult
|
||||
@@ -424,11 +425,14 @@ class Task(BaseModel):
|
||||
)
|
||||
|
||||
pydantic_output, json_output = self._export_output(result)
|
||||
|
||||
raw_result = result.result if hasattr(result, 'result') else result
|
||||
|
||||
task_output = TaskOutput(
|
||||
name=self.name,
|
||||
description=self.description,
|
||||
expected_output=self.expected_output,
|
||||
raw=result,
|
||||
raw=raw_result,
|
||||
pydantic=pydantic_output,
|
||||
json_dict=json_output,
|
||||
agent=agent.role,
|
||||
@@ -695,8 +699,11 @@ Follow these guidelines:
|
||||
return copied_task
|
||||
|
||||
def _export_output(
|
||||
self, result: str
|
||||
self, result: Union[str, ToolAnswerResult]
|
||||
) -> Tuple[Optional[BaseModel], Optional[Dict[str, Any]]]:
|
||||
if isinstance(result, ToolAnswerResult):
|
||||
return None, None
|
||||
|
||||
pydantic_output: Optional[BaseModel] = None
|
||||
json_output: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
@@ -7,3 +7,13 @@ class ToolResult:
|
||||
|
||||
result: str
|
||||
result_as_answer: bool = False
|
||||
|
||||
|
||||
class ToolAnswerResult:
|
||||
"""Wrapper for tool results that should be used as final answers without conversion."""
|
||||
|
||||
def __init__(self, result: str):
|
||||
self.result = result
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.result
|
||||
|
||||
Reference in New Issue
Block a user