Description
When using Cloudflare AI models that support tool/function calling, the API incorrectly returns a finish_reason of stop instead of tool_calls when the model decides to use a tool. The response contains the intended tool call in the message content, but because the finish_reason is not set correctly, standard OpenAI-compliant SDKs and automation platforms like n8n do not proceed to execute the tool. This effectively stops the workflow and breaks any automation that relies on tool usage.
This appears to be a departure from the expected behavior for OpenAI-compliant APIs and prevents the successful integration of Cloudflare AI models into tool-using applications.
Expected Behavior
When an AI model is called with a set of tools and decides to use one, the API response should have a finish_reason of tool_calls. The response message should also contain a tool_calls object detailing the function and arguments to be executed. This allows the client-side code (e.g., an n8n workflow or a LangChain agent) to execute the specified tool and then send the result back to the model to continue the process.
Actual Behavior
The Cloudflare AI API returns a finish_reason of stop. The content of the response message contains the tool call information, but there is no tool_calls object in the response. The workflow or SDK, seeing the stop reason, terminates the execution flow instead of calling the tool.
For example, the response chunk with the tool call has a finish_reason of stop:
JSON
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_...",
"type": "function",
"function": {
"name": "calculator",
"arguments": "{\"input\":\"4+4\"}"
}
}
]
}
This is incorrect and should be tool_calls to signal the next step to the client.
Steps to Reproduce
Configure an application or workflow (e.g., in n8n, or using a Python script with an OpenAI SDK) to use a Cloudflare AI model that supports function calling.
Provide the model with a tool, such as a simple calculator.
Send a prompt that requires the use of the tool (e.g., "What is 4 plus 4?").
Observe the raw response from the Cloudflare AI API.
Note that the finish_reason in the response is stop, and the tool call information is present in the message, but the workflow does not proceed to execute the tool.
Impact on Workflows 😥
This bug significantly impacts the usability of Cloudflare AI models in any automated workflow that requires tools. For platforms like n8n and for developers using standard OpenAI SDKs, the incorrect finish_reason breaks the entire "ReAct" (Reason and Act) loop that is fundamental to how AI agents operate with tools. This forces developers to manually parse the content of the response to check for tool calls, which is an unreliable and non-standard workaround.
Affected Models (Observed)
This behavior has been observed with the following Cloudflare models:
@cf/meta/llama-3.1-8b-instruct
@cf/meta/llama-3-8b-instruct-fp16
@cf/meta/llama-4-scout-17b-16e-instruct