Critical severity intermediate · Fix: 5-10 min

RecursionError

builtins.RecursionError

What this error means
This error occurs when a function calling loop causes infinite recursion, exhausting the Python call stack and crashing the program.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    response = client.chat.completions.create(
  File "openai/api_resources/chat_completion.py", line 85, in create
    return self._client._post("/v1/chat/completions", params=params)
  File "openai/api_client.py", line 120, in _post
    return self._request("post", url, params)
  File "openai/api_client.py", line 98, in _request
    response = self._send_request(method, url, params)
  File "openai/api_client.py", line 75, in _send_request
    return self._handle_response(response)
  File "openai/api_client.py", line 88, in _handle_response
    raise RecursionError("maximum recursion depth exceeded in function calling loop")
RecursionError: maximum recursion depth exceeded in function calling loop
QUICK FIX
Add a recursion depth limit or base case check in your function calling handler to prevent infinite loops immediately.

Why it happens

Infinite recursion in function calling happens when a function calls itself directly or indirectly without a proper base case or termination condition, causing the Python call stack to overflow. In function-calling with LLMs, this often occurs when the model repeatedly triggers the same function call in a loop due to prompt or logic errors.

Detection

Monitor for RecursionError exceptions and track call stack depth or repeated identical function call patterns in logs to detect infinite recursion before crashing.

Causes & fixes

1

The function calling logic triggers the same function repeatedly without a termination condition.

✓ Fix

Add a base case or maximum recursion depth check in your function calling handler to stop repeated calls.

2

The LLM prompt or function calling schema causes the model to request the same function call in a loop.

✓ Fix

Refine the prompt and function calling schema to prevent the model from requesting repeated calls, including adding context or state to break loops.

3

Recursive function calls are not properly handled or limited in the client code invoking the LLM.

✓ Fix

Implement recursion depth limits or iterative alternatives in your client code to avoid unbounded recursion.

Code: broken vs fixed

Broken - triggers the error
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# This triggers infinite recursion by calling the same function repeatedly
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Call function A"}],
    functions=[{"name": "function_a", "parameters": {}}],
    function_call="function_a"  # causes infinite recursion
)
print(response)
Fixed - works correctly
python
from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Added recursion depth limit to prevent infinite recursion
MAX_RECURSION_DEPTH = 3
recursion_counter = 0

def call_function_with_limit(function_name):
    global recursion_counter
    if recursion_counter >= MAX_RECURSION_DEPTH:
        raise RecursionError("Maximum recursion depth reached")
    recursion_counter += 1
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": f"Call {function_name}"}],
        functions=[{"name": function_name, "parameters": {}}],
        function_call=function_name
    )
    return response

try:
    response = call_function_with_limit("function_a")
    print(response)
except RecursionError as e:
    print(f"Recursion error caught: {e}")
Added a recursion depth counter and limit to stop infinite recursive function calls, preventing stack overflow.

Workaround

Wrap the function calling logic in a try/except block catching RecursionError and abort the call or fallback to a safe response when recursion is detected.

Prevention

Design your function calling schema and prompts to include explicit termination conditions and track call history or recursion depth to avoid infinite loops.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04 · gpt-4o-mini, gpt-4o
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.