crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] Native tool calls are discarded if LLM returns a text response

Open ehansis opened this issue 8 months ago • 6 comments

Description

The Crew Agent Executor loop for models supporting native tools intentionally passes available_functions=None to get_llm_response in order to get the tool calls as list for external execution:

https://github.com/crewAIInc/crewAI/blob/cd42bcf035c7e5b50ca6317da712d99394c75b44/lib/crewai/src/crewai/agents/crew_agent_executor.py#L512-L528

However, if the LLM response contains a text response in addition to the tool calls, this text response is returned instead of the tool calls, because of available_functions being None. The intention probably would be to follow the path 6) below, but 5) is executed instead.

https://github.com/crewAIInc/crewAI/blob/cd42bcf035c7e5b50ca6317da712d99394c75b44/lib/crewai/src/crewai/llm.py#L1237-L1251

Fixing this might be as easy as switching 5) and 6), but I'm not sure if this breaks something else.

The plaintext response is passed back into the agent executor loop and handled as potential final answer, the tool calls are never executed.

https://github.com/crewAIInc/crewAI/blob/cd42bcf035c7e5b50ca6317da712d99394c75b44/lib/crewai/src/crewai/agents/crew_agent_executor.py#L546-L557

Steps to Reproduce

Sorry, I have no minimal reproduction example available at this time.

I ran a crew with native tool calls against "openrouter/anthropic/claude-haiku-4.5" and "openrouter/anthropic/claude-sonnet-4.6" with some custom tool definitions. This triggered LLM responses with both text response and ChatCompletionMessageToolCall definitions, which causes the described issue.

Expected behavior

Return the tool calls and execute them in the agent executor loop. Discard the text response content (I guess?).

Screenshots/Code snippets

See above

Operating System

Ubuntu 22.04

Python Version

3.12

crewAI Version

1.10.1

crewAI Tools Version

?

Virtual Environment

Venv

Evidence

LLM response with both text response and tool calls:

ModelResponse(id='redacted', created=1773061504, model='anthropic/claude-4.5-haiku-20251001', object='chat.completion', system_fingerprint=None, choices=[Choices(finish_reason='tool_calls', index=0, message=Message(content='I will search for the given query.', role='assistant', tool_calls=[ChatCompletionMessageToolCall(index=0, function=Function(arguments='{"mode": "redacted", "query": "redacted"}', name='code_search'), id='redacted', type='function')], function_call=None, provider_specific_fields={'refusal': None, 'reasoning': None}), provider_specific_fields={'native_finish_reason': 'tool_calls'})], usage=Usage(...), provider='Amazon Bedrock')

Possible Solution

See above

Additional context

See above

ehansis avatar Mar 09 '26 13:03 ehansis

Submitted a fix in #4806. The issue is that the text-return path (step 5) matches before the tool-calls-without-functions path (step 6) because not available_functions is True when the executor passes None. The fix reorders these checks so tool calls are returned first.

mvanhorn avatar Mar 11 '26 06:03 mvanhorn

This looks like a merge policy bug between assistant text and tool call payloads.

A robust approach is to parse response parts first, then apply deterministic precedence:

If any valid tool calls exist, preserve all of them. Keep assistant text as an additional content part, not a replacement. Only drop a tool call when schema validation fails, and surface that explicitly in logs.

Two regression tests are especially useful:

Single message containing both text and one tool call Single message containing text and multiple tool calls

Expected result in both cases is that tool calls are executed and text remains available for transcript or UI.

saschabuehrle avatar Mar 11 '26 15:03 saschabuehrle

Bump! This issue prevents me from upgrading CrewAI, I'm stuck on a pretty old version.

(Yes, it appears there are bunch of great AI-generated pull requests, but maybe an actual human, preferably a knowledgable one, can chime in...)

ehansis avatar Apr 01 '26 06:04 ehansis

Happy to dive in if anyone has feedback on my pr. Sorry youre still having this

mvanhorn avatar Apr 01 '26 06:04 mvanhorn

@mvanhorn Thanks for your reply. One potential issue I see with your fix is that the additional text response is discarded. It would be nice if we could keep it as additional context, similar to how thought content is handled in other places. But I don't see yet how this would nicely fit with the current architecture.

ehansis avatar Apr 01 '26 07:04 ehansis

Can confirm this is still present in crewai 1.14.3. We hit the exact same issue with DeepSeek models served via an OpenAI-compatible endpoint through LiteLLM.

DeepSeek always returns both content (reasoning text) and tool_calls in the same response. Due to the condition at _handle_non_streaming_response:

python if (not tool_calls or not available_functions) and text_response: return text_response

Since the executor passes available_functions=None, this always evaluates to True when text is present - tool_calls are silently discarded every time.

The agent appears to "finish" on the first LLM call, returning its planning text as the final answer, with tools never executed. This also affects streaming mode (stream=True) where only the first iteration's chunks are yielded before the stream ends prematurely.

We applied the same fix as PR #4819 locally (swap steps 5 and 6 so tool_calls are checked before text_response) and it resolved the issue - the agent now correctly executes multi-step tool call loops.

Would be great to see #4819 merged. This is a blocker for any model that includes reasoning in content alongside tool_calls.

yanjingzhaisun avatar May 15 '26 12:05 yanjingzhaisun