crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Blocked result_as_answer tool becomes the agent's final answer on the native tool path

Open subhashpolisetti opened this issue 1 month ago • 3 comments

Description

When a before_tool_call hook blocks a tool that has result_as_answer=True, AgentExecutor returns "Tool execution blocked by hook. Tool: " as the agent's final answer instead of handing the blocked result back to the model.

The shared native path (execute_single_native_tool_call) already excludes blocked calls from result_as_answer (#5697), and the ReAct path does too. AgentExecutor keeps its own copy of that logic and the check is missing there. Since #8192 made AgentExecutor the default executor, any agent using a function-calling LLM hits this.

Steps to Reproduce

  1. Register a PRE_TOOL_CALL hook that returns False.
  2. Give an agent a tool with result_as_answer=True and an LLM that supports function calling.
  3. Run the crew (Crew.kickoff()) or agent.kickoff(); the LLM calls the tool.
  4. The task output is the block message and the LLM is never called again.

Expected behavior

A blocked call never runs, so it has no result to return as the answer. The block message should go back to the model as a normal tool result and the agent should continue, matching the shared native path and the ReAct path.

Screenshots/Code snippets

import json
from typing import Any
from crewai import Agent, Crew, Task
from crewai.hooks.dispatch import InterceptionPoint, on
from crewai.llms.base_llm import BaseLLM
from crewai.tools import BaseTool


class FinalLookup(BaseTool):
    name: str = "final_lookup"
    description: str = "Returns the final answer."
    result_as_answer: bool = True

    def _run(self, term: str = "x") -> str:
        return f"value:{term}"


class ScriptedLLM(BaseLLM):
    def __init__(self) -> None:
        super().__init__(model="offline")
        self.calls = 0

    def supports_function_calling(self) -> bool:
        return True

    def call(self, messages: Any, **kwargs: Any) -> Any:
        self.calls += 1
        if self.calls == 1:
            return [{"id": "c1", "type": "function",
                     "function": {"name": "final_lookup", "arguments": json.dumps({"term": "a"})}}]
        return "Recovered without the blocked tool"

    async def acall(self, messages: Any, **kwargs: Any) -> Any:
        return self.call(messages, **kwargs)


@on(InterceptionPoint.PRE_TOOL_CALL)
def block(_ctx):
    return False


llm = ScriptedLLM()
agent = Agent(role="r", goal="g", backstory="b", llm=llm, tools=[FinalLookup()])
task = Task(description="Look it up", expected_output="a value", agent=agent)
print(repr(Crew(agents=[agent], tasks=[task]).kickoff().raw), llm.calls)

Operating System

Ubuntu 20.04

Python Version

3.10

crewAI Version

1.15.28 (main @ a8877f0cd)

crewAI Tools Version

N/A (not used in the repro)

Virtual Environment

Venv

Evidence

Output of the snippet above on main:

'Tool execution blocked by hook. Tool: final_lookup' 1

The task output is the block message and the LLM was called once. With the check in place, the agent continues and returns the model's next answer ('Recovered without the blocked tool' 2).

Possible Solution

Carry hook_blocked in the result of AgentExecutor._execute_single_native_tool_call and skip the result_as_answer finish in execute_native_tool when it is set, the same condition the shared helper uses.

Additional context

macOS 26. Reproduced on Python 3.13 with a uv-managed venv (3.13 is not in the version dropdown).

subhashpolisetti avatar Oct 20 '26 20:10 subhashpolisetti

PR #8577 addresses this. AgentExecutor carries its own copy of the result_as_answer logic and was missing the hook_blocked check that the shared native path (#5697) and the ReAct path already apply. Because a blocked call sets tool_failure to None by design, the existing "a failed tool must not become the final answer" guard did not fire, and the block notice was returned as the final answer without another model call.

The fix surfaces hook_blocked on the per-call execution result so the short-circuit is skipped. Regression test included.

HhH666-ux avatar Oct 28 '26 03:10 HhH666-ux

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar Nov 27 '26 12:11 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar Dec 02 '26 12:12 github-actions[bot]