diff --git a/python/packages/autogen-core/src/autogen_core/_function_utils.py b/python/packages/autogen-core/src/autogen_core/_function_utils.py index a6803d085e48..7f11e458a276 100644 --- a/python/packages/autogen-core/src/autogen_core/_function_utils.py +++ b/python/packages/autogen-core/src/autogen_core/_function_utils.py @@ -21,12 +21,10 @@ get_origin, ) -from pydantic import BaseModel, Field, create_model # type: ignore +from pydantic import BaseModel, Field, TypeAdapter, create_model # type: ignore from pydantic_core import PydanticUndefined from typing_extensions import Literal -from ._pydantic_compat import model_dump, type2schema - logger = getLogger(__name__) T = TypeVar("T") @@ -141,7 +139,7 @@ def get_parameter_json_schema(k: str, v: Any, default_values: Dict[str, Any]) -> A Pydanitc model for the parameter """ - schema = type2schema(v) + schema = TypeAdapter(v).json_schema() if k in default_values: dv = default_values[k] schema["default"] = dv @@ -293,7 +291,7 @@ def f( ) ) - return model_dump(function) + return function.model_dump() def normalize_annotated_type(type_hint: Type[Any]) -> Type[Any]: diff --git a/python/packages/autogen-core/src/autogen_core/_pydantic_compat.py b/python/packages/autogen-core/src/autogen_core/_pydantic_compat.py deleted file mode 100644 index c29ccb70c067..000000000000 --- a/python/packages/autogen-core/src/autogen_core/_pydantic_compat.py +++ /dev/null @@ -1,50 +0,0 @@ -# File based from: https://github.com/microsoft/autogen/blob/47f905267245e143562abfb41fcba503a9e1d56d/autogen/_pydantic.py -# Credit to original authors - - -from typing import Any, Dict, Tuple, Type, Union, get_args - -from pydantic import BaseModel -from pydantic.version import VERSION as PYDANTIC_VERSION -from typing_extensions import get_origin - -__all__ = ("model_dump", "type2schema") - -PYDANTIC_V1 = PYDANTIC_VERSION.startswith("1.") - - -def type2schema(t: Type[Any] | None) -> Dict[str, Any]: - if PYDANTIC_V1: - from pydantic import schema_of # type: ignore - - if t is None: - return {"type": "null"} - elif get_origin(t) is Union: - return {"anyOf": [type2schema(tt) for tt in get_args(t)]} - elif get_origin(t) in [Tuple, tuple]: - prefixItems = [type2schema(tt) for tt in get_args(t)] - return { - "maxItems": len(prefixItems), - "minItems": len(prefixItems), - "prefixItems": prefixItems, - "type": "array", - } - - d = schema_of(t) # type: ignore - if "title" in d: - d.pop("title") - if "description" in d: - d.pop("description") - - return d - else: - from pydantic import TypeAdapter - - return TypeAdapter(t).json_schema() - - -def model_dump(model: BaseModel) -> Dict[str, Any]: - if PYDANTIC_V1: - return model.dict() # type: ignore - else: - return model.model_dump()