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
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ def _extract_field_type(self, key: str, value: Dict[str, Any], model_name: str,
constraints["min_length"] = value["minItems"]
if "maxItems" in value:
constraints["max_length"] = value["maxItems"]
item_schema = value.get("items", {"type": "string"})
# Per JSON Schema, an array without "items" (or with an empty items schema,
# which is what Pydantic emits for ``List[Any]``) puts no constraint on the
# type of its elements. Assuming strings here would reject valid data.
item_schema = value.get("items", {})
if "$ref" in item_schema:
item_type = self.get_ref(item_schema["$ref"].split("/")[-1])
elif item_schema.get("type") == "object" and "properties" in item_schema:
Expand All @@ -272,7 +275,7 @@ def _extract_field_type(self, key: str, value: Dict[str, Any], model_name: str,
else:
item_type_name = item_schema.get("type")
if item_type_name is None:
item_type = str
item_type = Any
elif item_type_name not in TYPE_MAPPING:
raise UnsupportedKeywordError(
f"Unsupported or missing item type `{item_type_name}` for array field `{key}` in `{model_name}`"
Expand Down
48 changes: 48 additions & 0 deletions python/packages/autogen-core/tests/test_json_to_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from uuid import UUID, uuid4

import pytest

from autogen_core.utils._json_to_pydantic import (
FORMAT_MAPPING,
TYPE_MAPPING,
Expand Down Expand Up @@ -1042,3 +1043,50 @@ def test_nested_arrays_with_object_schemas() -> None:
assert alice.name == "Alice" # type: ignore[attr-defined]
assert alice.role == "Senior Developer" # type: ignore[attr-defined]
assert alice.skills == ["Python", "JavaScript", "Docker"] # type: ignore[attr-defined]


def test_array_without_items_accepts_any_element_type() -> None:
"""An array schema whose items are unconstrained accepts any element type.

Pydantic describes ``List[Any]`` as an array with an empty items schema, and such a
schema must convert to a list of any type instead of a list of strings.
"""

class Payload(BaseModel):
values: List[Any]
tags: List[Any] = []

schema = Payload.model_json_schema()
assert schema["properties"]["values"]["type"] == "array"
assert schema["properties"]["values"].get("items", {}) == {}

Model = _JSONSchemaToPydantic().json_schema_to_pydantic(schema, "PayloadModel")
data = {"values": [1, True, "text", 4.5, {"a": 1}], "tags": []}
instance = Model(**data)
assert instance.values == data["values"] # type: ignore[attr-defined]
assert instance.tags == [] # type: ignore[attr-defined]

# An array property that omits "items" entirely is equally unconstrained.
bare: Dict[str, Any] = {
"type": "object",
"properties": {"nums": {"type": "array"}},
"required": ["nums"],
}
BareModel = _JSONSchemaToPydantic().json_schema_to_pydantic(bare, "BareArrayModel")
assert BareModel(**{"nums": [1, 2, 3]}).nums == [1, 2, 3] # type: ignore[attr-defined]


def test_constrained_array_without_items_keeps_constraints() -> None:
"""``minItems``/``maxItems`` still apply when the element type is unconstrained."""
schema: Dict[str, Any] = {
"type": "object",
"properties": {"xs": {"type": "array", "minItems": 1, "maxItems": 2}},
"required": ["xs"],
}
Model = _JSONSchemaToPydantic().json_schema_to_pydantic(schema, "ConstrainedArrayModel")

assert Model(**{"xs": [1, "two"]}).xs == [1, "two"] # type: ignore[attr-defined]
with pytest.raises(ValidationError):
Model(**{"xs": []})
with pytest.raises(ValidationError):
Model(**{"xs": [1, 2, 3]})