Skip to content

Commit ac28510

Browse files
onematchfoxekzhu
andauthored
Add support for "format": "json" in JSON schemas (#6846)
Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
1 parent 3b139c6 commit ac28510

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

python/packages/autogen-core/src/autogen_core/utils/_json_to_pydantic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
BaseModel,
1212
EmailStr,
1313
Field,
14+
Json,
1415
conbytes,
1516
confloat,
1617
conint,
@@ -82,6 +83,7 @@ class UnsupportedKeywordError(SchemaConversionError):
8283
"binary": conbytes(strict=True),
8384
"password": str,
8485
"path": str,
86+
"json": Json,
8587
}
8688

8789

python/packages/autogen-core/tests/test_json_to_pydantic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
UnsupportedKeywordError,
1212
_JSONSchemaToPydantic, # pyright: ignore[reportPrivateUsage]
1313
)
14-
from pydantic import BaseModel, EmailStr, Field, ValidationError
14+
from pydantic import BaseModel, EmailStr, Field, Json, ValidationError
1515

1616

1717
# ✅ Define Pydantic models for testing
@@ -44,6 +44,7 @@ class ComplexModel(BaseModel):
4444
user: User
4545
extra_info: Optional[Dict[str, Any]] = None # Optional dictionary
4646
sub_items: List[Employee] # List of Employees
47+
json_string: Optional[Json[Any]] = None # Optional JSON string
4748

4849

4950
@pytest.fixture
@@ -82,7 +83,7 @@ def sample_json_schema_complex() -> Dict[str, Any]:
8283
(sample_json_schema, "User", ["id", "name", "email", "age", "address"]),
8384
(sample_json_schema_recursive, "Employee", ["id", "name", "manager"]),
8485
(sample_json_schema_nested, "Department", ["name", "employees"]),
85-
(sample_json_schema_complex, "ComplexModel", ["user", "extra_info", "sub_items"]),
86+
(sample_json_schema_complex, "ComplexModel", ["user", "extra_info", "sub_items", "json_string"]),
8687
],
8788
)
8889
def test_json_schema_to_pydantic(
@@ -160,6 +161,7 @@ def test_json_schema_to_pydantic(
160161
{"id": str(uuid4()), "name": "Eve"},
161162
{"id": str(uuid4()), "name": "David", "manager": {"id": str(uuid4()), "name": "Frank"}},
162163
],
164+
"json_string": '{"foo": "bar"}',
163165
},
164166
),
165167
],
@@ -241,6 +243,7 @@ def test_valid_data_model(
241243
{"id": "invalid-uuid", "name": "Eve"}, # Invalid UUID
242244
{"id": str(uuid4()), "name": 123}, # Invalid name type
243245
],
246+
"json_string": '{"foo": "bar"', # Invalid JSON
244247
},
245248
),
246249
],

0 commit comments

Comments
 (0)