Skip to content
MangoBoost

Function Calling Mistakes That Make LLMs Invent Results

Function Calling Mistakes That Make LLMs Invent Results

Many function calling bugs don't throw an error. The request succeeds, the model answers in fluent prose, and the answer is wrong because your code gave it an incomplete or ambiguous conversation.

We ran the common mistakes against two open models on Mango Inference to see what happens. Below is each mistake, what the models did, and a tool loop that avoids all of them.

How does function calling work?

The model never runs your function. You describe your functions in the tools parameter, the model replies with the name and arguments of the function it wants, your code runs it, and you send the result back so the model can answer.

Function calling loop: the user's question goes to your app, which sends it to the LLM with the tool list and today's date. The LLM returns tool_calls, your code validates the arguments and runs the function, and sends one tool result per call back to the LLM, which then writes the final answer.
Function calling loop: the user's question goes to your app, which sends it to the LLM with the tool list and today's date. The LLM returns tool_calls, your code validates the arguments and runs the function, and sends one tool result per call back to the LLM, which then writes the final answer.

Most of these bugs happen inside that loop, because every round trip depends on your code passing back exactly what the model asked for, in the right order. The request and response formats are in the Tool calling guide.

What happens if a tool result is missing?

The model may invent the missing result. In our test, the API returned no error.

We asked: "Check if Mango Bistro and Seoul Table both have a table for 4 tomorrow at 7pm." Both models made two check_availability calls in one turn, one per restaurant. We sent back only the first result (Mango Bistro: available) and asked for the answer.

  • DeepSeek V4.1 Flash: Runs: 5; Said both restaurants had a table: 5; Noticed and called the tool again: 0
  • GLM-5.3-Flash: Runs: 5; Said both restaurants had a table: 1; Noticed and called the tool again: 4

※ Measured on November 27, 2026, with English prompts, default settings (reasoning on), 5 runs per model.

Nobody had checked Seoul Table. A booking agent built this way tells the user a table exists when it doesn't.

This usually happens when a loop handles only tool_calls[0], or when one tool raises an exception and the code skips its result. Models can request several tools in one turn, and both models did so on every run here. Send one role: "tool" message for every call, matched by tool_call_id.

Why does the model call the tool with the wrong date?

Because it doesn't know today's date. When we asked "Check if Mango Bistro has a table for 4 tomorrow at 7pm" without saying what today was:

  • In 6 of 10 runs (5 per model), the model called the tool anyway with a date it made up, such as 2025-01-02 or 2026-07-01.
  • In the other 4, it asked the user for the date instead of calling the tool.

Put the current date (and the user's time zone, if it matters) in the system message. In every run where the prompt included today's date, both models turned "tomorrow" into the correct date.

Is arguments a dictionary?

No. tool_calls[].function.arguments is a JSON-encoded string. You have to parse it with json.loads before you use it, and parsing can fail.

Treat the parsed result as untrusted input. The model can leave out a required field, send a date in the wrong format, or name a tool you never defined. Validate the arguments before you run anything, especially for tools that write data or spend money.

What should you do when a tool fails?

Send the error back to the model as the tool result instead of raising an exception. A message like {"error": "invalid arguments: ..."} lets the model correct its arguments or tell the user what went wrong. An exception that kills the loop leaves the user with nothing, and a silently skipped result leads to the invented answers above.

Also cap the number of rounds. A model that keeps calling tools will otherwise loop until it runs out of context or budget.

What does a correct tool loop look like?

This loop sends today's date, parses and validates arguments, appends the assistant message before the results, answers every tool call, returns errors to the model, and stops after five rounds:

python
import datetime
import json
import os

from openai import OpenAI

client = OpenAI(
    base_url="https://api.mangoboost.io/v1",
    api_key=os.environ["MANGOINFERENCE_API_KEY"],
)
MODEL = "deepseek-ai/DeepSeek-V4.1-Flash-1017"

tools = [{
    "type": "function",
    "function": {
        "name": "check_availability",
        "description": "Check whether a restaurant has a free table. Date YYYY-MM-DD, time HH:MM (24h).",
        "parameters": {
            "type": "object",
            "properties": {
                "restaurant": {"type": "string"},
                "date": {"type": "string"},
                "time": {"type": "string"},
                "party_size": {"type": "integer", "minimum": 1},
            },
            "required": ["restaurant", "date", "time", "party_size"],
        },
    },
}]

def check_availability(restaurant, date, time, party_size):
    # Replace with your booking system. Fake data for the example.
    return {"restaurant": restaurant, "available": restaurant != "Seoul Table"}

def run_tool(call):
    if call.function.name != "check_availability":
        return {"error": f"unknown tool: {call.function.name}"}
    try:
        args = json.loads(call.function.arguments)  # a JSON string, not a dict
        datetime.date.fromisoformat(args["date"])
        return check_availability(**args)
    except (json.JSONDecodeError, KeyError, TypeError, ValueError) as e:
        return {"error": f"invalid arguments: {e}"}  # let the model fix it

def ask(question, max_turns=5):
    today = datetime.date.today().isoformat()
    messages = [
        {"role": "system", "content": f"Today is {today}."},
        {"role": "user", "content": question},
    ]
    for _ in range(max_turns):
        resp = client.chat.completions.create(model=MODEL, messages=messages, tools=tools)
        msg = resp.choices[0].message
        if not msg.tool_calls:
            return msg.content
        messages.append(msg)  # the assistant turn that asked for the tools
        for call in msg.tool_calls:  # one result for every call
            messages.append({
                "role": "tool",
                "tool_call_id": call.id,
                "content": json.dumps(run_tool(call)),
            })
    raise RuntimeError("too many tool rounds")

print(ask("Can Mango Bistro and Seoul Table both seat 4 tomorrow at 7pm?"))

When we ran it on November 27, 2026, the model called the tool for both restaurants and answered that Mango Bistro was available and Seoul Table was not, matching the fake data.

The same loop works with the other models on Mango Inference; all four list tools in their supported_features from GET /v1/models. If you need the final answer itself in a fixed JSON shape, see Structured output.

To get an API key and run this loop, start with the Quickstart.

Building an agent that calls your own tools?

If you are planning an agent on top of your own APIs and want to talk through models, tool design or deployment options for your workload, contact us.

Related Posts

How to Retry 429 and 503 Errors From an LLM API
Guides

How to Retry 429 and 503 Errors From an LLM API

Learn more
How to Build a Voice AI Agent That Makes Phone Calls
Guides

How to Build a Voice AI Agent That Makes Phone Calls

Learn more
Why Agentic AI Breaks Classic Inference Infrastructure
Technical Insights

Why Agentic AI Breaks Classic Inference Infrastructure

Learn more
MangoBoost Launches Mango Inference, Emerging as a Key Partner Powering AMD's Enterprise AI Ecosystem
Technical Insights

MangoBoost Launches Mango Inference, Emerging as a Key Partner Powering AMD's Enterprise AI Ecosystem

Learn more
The First AMD Instinct™ Submission to MLPerf® Inference with Prefill/Decode-Disaggregated Deployment, and the Largest Multi-Region Cluster Ever Submitted
Benchmark Results

The First AMD Instinct™ Submission to MLPerf® Inference with Prefill/Decode-Disaggregated Deployment, and the Largest Multi-Region Cluster Ever Submitted

Learn more
First-ever Multi-region Heterogeneous Inference: MangoBoost Sets New Standards for Global AI Scaling in MLPerf Inference v6.0
Benchmark Results

First-ever Multi-region Heterogeneous Inference: MangoBoost Sets New Standards for Global AI Scaling in MLPerf Inference v6.0

Learn more
MangoBoost Pushes AI Scalability in MLPerf Training v5.1: First-ever Multi-node MI325X Results Highlight AMD's Scaling Capabilities
Benchmark Results

MangoBoost Pushes AI Scalability in MLPerf Training v5.1: First-ever Multi-node MI325X Results Highlight AMD's Scaling Capabilities

Learn more
Pioneering Multi-node Heterogeneous Inference: MangoBoost and AMD, in Collaboration with OEM Partners, Set Record Llama2-70B Performance in MLPerf Inference v5.1
Benchmark Results

Pioneering Multi-node Heterogeneous Inference: MangoBoost and AMD, in Collaboration with OEM Partners, Set Record Llama2-70B Performance in MLPerf Inference v5.1

Learn more
MangoBoost Demonstrates Record-Breaking Performance in AI Training Storage Solution with MLPerf® Storage v2.0
Benchmark Results

MangoBoost Demonstrates Record-Breaking Performance in AI Training Storage Solution with MLPerf® Storage v2.0

Learn more
MangoBoost Sets a New MLPerf Training v5.0 Standard for Multi-nodes Llama2-70B-LoRA on AMD Instinct™ MI300X GPU!
Benchmark Results

MangoBoost Sets a New MLPerf Training v5.0 Standard for Multi-nodes Llama2-70B-LoRA on AMD Instinct™ MI300X GPU!

Learn more
MangoBoost Sets The Highest MLPerf Inference v5.0 Result in History for Llama2-70B Offline!
Benchmark Results

MangoBoost Sets The Highest MLPerf Inference v5.0 Result in History for Llama2-70B Offline!

Learn more
MangoBoost Demonstrates Mango GPUBoost RDMA™ with MLPerf Training v4.1 on a Multi-node System Powered by AMD MI300X GPUs
Benchmark Results

MangoBoost Demonstrates Mango GPUBoost RDMA™ with MLPerf Training v4.1 on a Multi-node System Powered by AMD MI300X GPUs

Learn more
MangoBoost Demonstrates Turn-Key Storage Solution for AI Training with MLPerf® Storage v1.0
Benchmark Results

MangoBoost Demonstrates Turn-Key Storage Solution for AI Training with MLPerf® Storage v1.0

Learn more
MangoBoost Demonstrates a Turn-Key LLM Inference Serving Solution on AMD Instinct™ MI300X GPUs with the MLPerf® Inference v4.1 Llama Benchmark
Benchmark Results

MangoBoost Demonstrates a Turn-Key LLM Inference Serving Solution on AMD Instinct™ MI300X GPUs with the MLPerf® Inference v4.1 Llama Benchmark

Learn more