Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG FIX: Pydantic conversion logic for structured outputs is broken for models containing dictionaries #2003

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/openai/lib/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ def _ensure_strict_json_schema(
_ensure_strict_json_schema(definition_schema, path=(*path, "definitions", definition_name), root=root)

typ = json_schema.get("type")
if typ == "object" and "additionalProperties" not in json_schema:
properties = json_schema.get("properties")
if (
typ == "object" and
"additionalProperties" not in json_schema and
is_dict(properties) and
len(properties) > 0
):
json_schema["additionalProperties"] = False

# object types
# { 'type': 'object', 'properties': { 'a': {...} } }
properties = json_schema.get("properties")
if is_dict(properties):
json_schema["required"] = [prop for prop in properties.keys()]
json_schema["properties"] = {
Expand Down
75 changes: 74 additions & 1 deletion tests/lib/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations

from enum import Enum
from typing import Any, Dict

from pydantic import Field, BaseModel
from pydantic import Field, BaseModel, ConfigDict
from inline_snapshot import snapshot

import openai
Expand Down Expand Up @@ -235,3 +236,75 @@ def test_enums() -> None:
},
}
)


class GenerateToolCallArguments(BaseModel):
arguments: Dict[str, Any] = Field(description="The arguments to pass to the tool")


def test_dictionaries():
# JSON schema definitions for this pydantic model are the same in Pydantic 1.x and 2.x
assert openai.pydantic_function_tool(GenerateToolCallArguments)["function"] == snapshot(
{
'name': 'GenerateToolCallArguments',
'parameters': {
'additionalProperties': False,
'properties': {
'arguments': {
'description': 'The arguments to pass to the tool',
'title': 'Arguments',
'type': 'object',
},
},
'required': [
'arguments',
],
'title': 'GenerateToolCallArguments',
'type': 'object',
},
'strict': True,
}
)


class EmptyAllowExtras(BaseModel):
pass


if PYDANTIC_V2:
class EmptyForbidExtras(BaseModel):
model_config = ConfigDict(extra="forbid")
else:
class EmptyForbidExtras(BaseModel):
class Config:
extra = "forbid"


def test_empty_objects():
# JSON schema definitions for these pydantic models are the same in Pydantic 1.x and 2.x
assert openai.pydantic_function_tool(EmptyAllowExtras)["function"] == snapshot(
{
'name': 'EmptyAllowExtras',
'parameters': {
'properties': {},
'required': [],
'title': 'EmptyAllowExtras',
'type': 'object',
},
'strict': True,
}
)

assert openai.pydantic_function_tool(EmptyForbidExtras)["function"] == snapshot(
{
'name': 'EmptyForbidExtras',
'parameters': {
'additionalProperties': False,
'properties': {},
'required': [],
'title': 'EmptyForbidExtras',
'type': 'object',
},
'strict': True,
}
)