Skip to content
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: 9 additions & 0 deletions src/openai/lib/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def _ensure_strict_json_schema(
if is_dict(items):
json_schema["items"] = _ensure_strict_json_schema(items, path=(*path, "items"), root=root)

# tuples / positional arrays
# { 'type': 'array', 'prefixItems': [{...}, {...}] }
prefix_items = json_schema.get("prefixItems")
if is_list(prefix_items):
json_schema["prefixItems"] = [
_ensure_strict_json_schema(item, path=(*path, "prefixItems", str(i)), root=root)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent infinite expansion of recursive tuple refs

When a recursive Pydantic model contains a tuple element such as tuple[Annotated[Node, Field(description="child")]], this traversal reaches the $ref with its description, expands the referenced Node, and then walks into the same prefixItems entry again. This repeats until to_strict_json_schema() raises RecursionError; self- and mutually recursive tuple schemas therefore need explicit cycle handling rather than unconditional descent.

Useful? React with 👍 / 👎.

for i, item in enumerate(prefix_items)
]

# unions
any_of = json_schema.get("anyOf")
if is_list(any_of):
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from __future__ import annotations

from enum import Enum
from typing import Tuple
from typing_extensions import Annotated

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

Expand Down Expand Up @@ -169,6 +172,78 @@ def test_most_types() -> None:
)


class Location(BaseModel):
lat: float
long: float


class Route(BaseModel):
endpoints: Tuple[
Annotated[Location, Field(description="The starting point.")],
Annotated[Location, Field(description="The ending point.")],
]


@pytest.mark.skipif(PYDANTIC_V1, reason="`prefixItems` is only emitted by pydantic v2")
def test_tuple_prefix_items_ref_expansion() -> None:
# tuple elements are emitted under `prefixItems`; the strict transform must
# recurse into them the same way it does for `items`, otherwise a `$ref` that
# carries sibling keys (e.g. a field description) is left un-expanded and the
# API rejects the resulting schema.
assert to_strict_json_schema(Route) == snapshot(
{
"$defs": {
"Location": {
"properties": {
"lat": {"title": "Lat", "type": "number"},
"long": {"title": "Long", "type": "number"},
},
"required": ["lat", "long"],
"title": "Location",
"type": "object",
"additionalProperties": False,
}
},
"properties": {
"endpoints": {
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"description": "The starting point.",
"properties": {
"lat": {"title": "Lat", "type": "number"},
"long": {"title": "Long", "type": "number"},
},
"required": ["lat", "long"],
"title": "Location",
"type": "object",
"additionalProperties": False,
},
{
"description": "The ending point.",
"properties": {
"lat": {"title": "Lat", "type": "number"},
"long": {"title": "Long", "type": "number"},
},
"required": ["lat", "long"],
"title": "Location",
"type": "object",
"additionalProperties": False,
},
],
"title": "Endpoints",
"type": "array",
}
},
"required": ["endpoints"],
"title": "Route",
"type": "object",
"additionalProperties": False,
}
)


class Color(Enum):
RED = "red"
BLUE = "blue"
Expand Down