RecursionError
builtins.RecursionError
Stack trace
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 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
The function calling logic triggers the same function repeatedly without a termination condition.
Add a base case or maximum recursion depth check in your function calling handler to stop repeated calls.
The LLM prompt or function calling schema causes the model to request the same function call in a loop.
Refine the prompt and function calling schema to prevent the model from requesting repeated calls, including adding context or state to break loops.
Recursive function calls are not properly handled or limited in the client code invoking the LLM.
Implement recursion depth limits or iterative alternatives in your client code to avoid unbounded recursion.
Code: broken vs fixed
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) 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}") 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.