OpenAIError
openai.OpenAIError (function calling JSON schema invalid)
Stack trace
openai.OpenAIError: Invalid JSON schema for function calling: Schema validation failed at path $.parameters.properties.foo
at openai.api_requestor.APIRequestor._interpret_response (openai/api_requestor.py:123)
at openai.api_requestor.APIRequestor.request (openai/api_requestor.py:75)
at openai.OpenAI.chat.completions.create (openai/api_resources/chat.py:45)
at main.py:42 Why it happens
OpenAI's function calling feature requires a valid JSON schema to describe the function parameters. If the schema is malformed, missing required fields, or contains invalid types, the API rejects the request with this error. This often happens due to typos, incorrect schema structure, or unsupported JSON schema features.
Detection
Validate your JSON schema locally using a JSON schema validator before sending it to OpenAI. Log the schema payload and catch OpenAIError exceptions to detect schema issues early.
Causes & fixes
The JSON schema is missing the required 'type' field for properties.
Add the 'type' field (e.g., 'type': 'string') to every property in the JSON schema.
The JSON schema contains unsupported or invalid keywords not recognized by OpenAI.
Remove unsupported keywords and ensure the schema uses only standard JSON Schema Draft 7 keywords supported by OpenAI.
The top-level schema 'parameters' field is not an object or is missing.
Ensure the 'parameters' field is present and is a JSON object describing the function parameters.
The schema has syntax errors such as trailing commas or invalid JSON formatting.
Use a JSON linter or parser to validate and fix syntax errors before sending the schema.
Code: broken vs fixed
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
function_def = {
"name": "get_weather",
"description": "Get the weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": {"description": "City name"} # Missing 'type' here
},
"required": ["city"]
}
}
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's the weather in Boston?"}],
functions=[function_def], # This line triggers the error
function_call={"name": "get_weather"}
)
print(response) import os
from openai import OpenAI
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
function_def = {
"name": "get_weather",
"description": "Get the weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"} # Added 'type' field
},
"required": ["city"]
}
}
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What's the weather in Boston?"}],
functions=[function_def], # Fixed JSON schema
function_call={"name": "get_weather"}
)
print(response) Workaround
Catch the OpenAIError exception, log the full schema payload, and manually validate or simplify the schema to isolate the invalid part before retrying.
Prevention
Use a JSON schema validator during development to ensure your function calling schemas are valid and conform to OpenAI's supported subset before sending requests.