ValueError
openai.error.ValueError: tool_calls response not returned by model
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.chat.completions.create(
File "/usr/local/lib/python3.9/site-packages/openai/api_resources/chat_completion.py", line 85, in create
raise ValueError("tool_calls response not returned by model")
ValueError: tool_calls response not returned by model Why it happens
When using function-calling with OpenAI's chat completions, the model is expected to return a 'tool_calls' field in its response to indicate which tool to invoke. This error occurs if the model's output does not include this field, often due to prompt misconfiguration, missing or incorrect function definitions, or using a model that does not support function-calling.
Detection
Check the response object for the presence of the 'tool_calls' field after the chat completion call; log the raw response if missing to detect this error before it crashes downstream.
Causes & fixes
The prompt does not clearly instruct the model to call a function or use tool-calling.
Add explicit instructions in the system or user prompt to trigger the model to call the desired function.
The functions parameter passed to the chat completion is missing or incorrectly formatted.
Ensure the 'functions' argument is a properly formatted list of function definitions matching OpenAI's function-calling schema.
Using a model that does not support function-calling or tool_calls response.
Switch to a model that supports function-calling, such as 'gpt-4o' or 'gpt-4o-mini'.
Code: broken vs fixed
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Call the weather function."}]
# Missing 'functions' parameter causes the error
)
print(response.choices[0].message.tool_calls) # This line raises ValueError from openai import OpenAI
import os
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"}
},
"required": ["location"]
}
}
]
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Call the weather function."}],
functions=functions # Added functions parameter
)
print(response.choices[0].message.tool_calls) # Now works correctly Workaround
Catch the ValueError exception, log the full raw response, and fallback to parsing the message content manually or retry with clearer function-calling instructions.
Prevention
Always provide explicit function definitions in the 'functions' parameter and use instruction-tuned models that support function-calling to guarantee the model returns the tool_calls response.