Remove pydantic v1 compat (#4838)

Remove pydantic compat
This commit is contained in:
Jack Gerrits 2024-12-27 14:38:42 -05:00 committed by GitHub
parent a5681d73c6
commit ad7433e817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 55 deletions

View File

@ -21,12 +21,10 @@ from typing import (
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 get_function_schema(f: Callable[..., Any], *, name: Optional[str] = None, de
)
)
return model_dump(function)
return function.model_dump()
def normalize_annotated_type(type_hint: Type[Any]) -> Type[Any]:

View File

@ -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()