RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Fredoline Eruo
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /How-to
  5. /How to Handle Function Call Errors and Retries
HOW-TO · RAG

How to Handle Function Call Errors and Retries

intermediate·15 min·By Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

Function calling agent set up, Python 3.10+

What this does

Function calls can fail due to invalid arguments, network errors, or runtime exceptions. This guide implements retry logic, error reporting back to the LLM, and graceful fallback.

Steps

  • Wrap tool execution with try/except. Return error messages as tool results so the LLM can self-correct.
import json

def safe_execute(func, args):
    try:
        result = func(**args)
        return json.dumps({"success": True, "result": result})
    except Exception as e:
        return json.dumps({"success": False, "error": str(e)})
  • Feed errors back to the LLM. The model may retry with corrected arguments.
# Inside the tool loop
for tc in tool_calls:
    func = tool_map[tc.function.name]
    args = json.loads(tc.function.arguments)
    result = safe_execute(func, args)
    messages.append({
        "role": "tool",
        "tool_call_id": tc.id,
        "content": result
    })

# Ask LLM to fix the error
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages,
    tools=tools
)
  • Implement retry with exponential backoff. Limit the number of retry cycles.
import time

max_retries = 3
for attempt in range(max_retries):
    response = client.chat.completions.create(
        model="llama3.2",
        messages=messages,
        tools=tools
    )
    msg = response.choices[0].message
    if msg.finish_reason != "tool_calls":
        break  # LLM is satisfied
    # Execute tools with error handling
    all_ok = True
    for tc in msg.tool_calls:
        result = safe_execute(tool_map[tc.function.name], json.loads(tc.function.arguments))
        if "error" in result:
            all_ok = False
        messages.append({"role": "tool", "tool_call_id": tc.id, "content": result})
    if all_ok:
        break
    time.sleep(2 ** attempt)  # 1s, 2s, 4s
  • Validate arguments before execution. Catch schema violations early.
from jsonschema import validate, ValidationError

def validate_and_call(func, schema, args):
    try:
        validate(instance=args, schema=schema)
        return func(**args)
    except ValidationError as e:
        return {"error": f"Invalid arguments: {e.message}"}
    except Exception as e:
        return {"error": str(e)}
  • Fallback when retries are exhausted. Return a default or partial result.
if attempt == max_retries - 1:
    fallback_response = "I encountered an error while processing your request. Please try again with different parameters."
    messages.append({"role": "assistant", "content": fallback_response})

Verification

python -c "
import time, random
for i in range(3):
    try:
        if random.random() < 0.7: raise ValueError('fail')
        print('success')
        break
    except:
        time.sleep(0.1)
else:
    print('fallback')
# Expected: success or fallback
"

Common failures

  • LLM keeps retrying with the same bad arguments. The model may repeat the same mistake. Inject a hint in the error message like "Try using a valid email format."
  • Infinite retry loop. Without a max_retries limit, the agent may loop forever. Always cap retries.
  • Error response not in tool format. The error message must use role: "tool" with the correct tool_call_id. Otherwise, the LLM ignores it.
  • Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
  • Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.

Related guides

  • How to Define JSON Schema for Tool Calling
  • How to Use OpenAI Function Calling with Tools
← All how-to guidesCourses →