Open
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python library
Describe the bug
There's a bug in OpenAI's python client logic for translating pydantic models with dictionaries into structured outputs JSON schema definitions: dictionaries are always required to be empty in the resulting JSON schema, rendering the dictionary outputs significantly less useful since the LLM is never allowed to populate them
I've filed a small PR to fix this and introduce test coverage: #2003
To Reproduce
import json
from typing import Any, Dict
import pydantic
from openai.lib._pydantic import to_strict_json_schema
class GenerateToolCallArguments(pydantic.BaseModel):
arguments: Dict[str, Any] = pydantic.Field(description="The arguments to pass to the tool")
print(json.dumps(to_strict_json_schema(GenerateToolCallArguments), indent=4))
Observe that the output inserts additionalProperties: False
into the resulting JSON schema definition, meaning that the dictionary must always be empty:
{
"properties": {
"arguments": {
"description": "The arguments to pass to the tool",
"title": "Arguments",
"type": "object",
# THE INSERTION OF THIS LINE IS A BUG
"additionalProperties": false
}
},
"required": [
"arguments"
],
"title": "GenerateToolCallArguments",
"type": "object",
"additionalProperties": false
}
Code snippets
No response
OS
macOS
Python version
Python v3.10.12
Library version
1.59.6
Activity
dbczumar commentedon Jan 10, 2025
Tagging @RobertCraigie for visibility, just in case (saw that you've been active on recent issues) :)
BrunoScaglione commentedon Jan 11, 2025
I'm having the same issue, can confirm that models with dictionaries is the root problem. But i checked the documentation again, and they do talk about only allowing additionalProperties=false.
dbczumar commentedon Jan 15, 2025
@RobertCraigie Any updates or additional thoughts here?
dvschuyl commentedon Jan 24, 2025
I have also encountered the same issue. After some tinkering, I found some more types that resulted in errors. The only buildin collection type that doesn't seem to be affected is the
list
.My code (python 3.13.1):
dbczumar commentedon Feb 19, 2025
@RobertCraigie Any updates or additional thoughts here?
choudhary-akash commentedon May 14, 2025
@dbczumar @RobertCraigie
The behaviour of this bug seems to have changed now. Although previously, it used to return additionalProperties: false for such dictionary fields, but with pydantic v2.11+, it now returns additionalProperties: true. This causes the following error now:
'additionalProperties' is required to be supplied and to be false.
From pydantic v2.11, such fields like Dict[str, Any] are represented in json schema like this:
{ "additionalProperties": true, "type": "object" }
whereas in older versions of pydantic, i.e. till v2.10.6, such a field was represented like this:
{ "type": "object" }
To Reproduce
OS
macOS
Python version
3.13.3
OpenAI Library Version
1.78.1
Pydantic Version
2.11.4