High severity intermediate · Fix: 2-5 min

OpenAIError

openai.OpenAIError (function calling JSON schema invalid)

What this error means
OpenAI rejects the function calling request because the provided JSON schema for the function is invalid or malformed.

Stack trace

traceback
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
QUICK FIX
Validate and correct your JSON schema structure and types before passing it to the OpenAI function calling API.

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

1

The JSON schema is missing the required 'type' field for properties.

✓ Fix

Add the 'type' field (e.g., 'type': 'string') to every property in the JSON schema.

2

The JSON schema contains unsupported or invalid keywords not recognized by OpenAI.

✓ Fix

Remove unsupported keywords and ensure the schema uses only standard JSON Schema Draft 7 keywords supported by OpenAI.

3

The top-level schema 'parameters' field is not an object or is missing.

✓ Fix

Ensure the 'parameters' field is present and is a JSON object describing the function parameters.

4

The schema has syntax errors such as trailing commas or invalid JSON formatting.

✓ Fix

Use a JSON linter or parser to validate and fix syntax errors before sending the schema.

Code: broken vs fixed

Broken - triggers the error
python
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)
Fixed - works correctly
python
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)
Added the missing 'type' field in the JSON schema properties to comply with OpenAI's required schema format.

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.

Python 3.9+ · openai >=1.0.0 · tested on 1.x
Verified 2026-04
Verify ↗

Community Notes

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