How to handle function call response
Quick answer
When using
tools= for function calls in the OpenAI API, check if response.choices[0].finish_reason is "tool_calls". Then parse the function call arguments from response.choices[0].message.tool_calls[0].function.arguments as JSON to handle the response properly. ERROR TYPE
code_error ⚡ QUICK FIX
Parse the function call arguments JSON from
response.choices[0].message.tool_calls[0].function.arguments before using it.Why this happens
When you invoke a chat completion with tools= to enable function calling, the model may respond with a tool_calls finish reason indicating it wants to call a function. If you try to access the function call arguments directly as a string without parsing, your code will not handle the response correctly.
Example broken code:
from openai import OpenAI
import os
import json
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tools = [{"type": "function", "function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"]
}
}}]
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[{"role": "user", "content": "What's the weather in NYC?"}]
)
if response.choices[0].finish_reason == "tool_calls":
# Incorrect: using raw string without parsing
args = response.choices[0].message.tool_calls[0].function.arguments
print("Function call arguments (raw):", args) output
Function call arguments (raw): {"location": "NYC"} The fix
Parse the function.arguments string as JSON to convert it into a Python dictionary. This allows you to safely extract parameters and use them in your function implementation.
Corrected code example:
from openai import OpenAI
import os
import json
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
tools = [{"type": "function", "function": {
"name": "get_weather",
"description": "Get weather for a location",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}},
"required": ["location"]
}
}}]
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[{"role": "user", "content": "What's the weather in NYC?"}]
)
if response.choices[0].finish_reason == "tool_calls":
tool_call = response.choices[0].message.tool_calls[0]
args = json.loads(tool_call.function.arguments)
location = args.get("location")
print(f"Calling get_weather with location: {location}")
# Implement your function call here
weather_result = f"Sunny in {location}"
print("Function result:", weather_result) output
Calling get_weather with location: NYC Function result: Sunny in NYC
Preventing it in production
- Always check
finish_reasonfortool_callsbefore accessing function call data. - Parse
function.argumentswithjson.loads()to avoid errors and safely extract parameters. - Implement retries and error handling around your function calls to handle malformed or unexpected data gracefully.
- Validate the parsed arguments against your function schema before use.
Key Takeaways
- Check response.choices[0].finish_reason for 'tool_calls' to detect function calls.
- Parse function call arguments JSON string with json.loads() before use.
- Validate and handle parsed arguments safely to avoid runtime errors.