11import contextlib
2- import datetime
32import inspect
4- from enum import Enum
5- from typing import Any , Callable , NamedTuple , Optional , cast , get_type_hints
3+ from typing import Any , Callable , NamedTuple , Optional , get_type_hints
64
7- from pydantic import BaseModel , TypeAdapter
5+ from pydantic import TypeAdapter
86
97from workflowai .core .utils ._schema_generator import JsonSchemaGenerator
108
@@ -17,14 +15,6 @@ class SchemaDeserializer(NamedTuple):
1715 deserializer : Optional [Callable [[Any ], Any ]] = None
1816
1917
20- def _serialize_datetime (x : datetime .datetime ) -> str :
21- return x .isoformat ()
22-
23-
24- def _deserialize_datetime (x : str ) -> datetime .datetime :
25- return datetime .datetime .fromisoformat (x )
26-
27-
2818def _get_type_schema (param_type : type ):
2919 """Convert a Python type to its corresponding JSON schema type.
3020
@@ -47,31 +37,11 @@ def _get_type_schema(param_type: type):
4737 if param_type is bool :
4838 return SchemaDeserializer ({"type" : "boolean" })
4939
50- if param_type is datetime .datetime :
51- return SchemaDeserializer (
52- {"type" : "string" , "format" : "date-time" },
53- serializer = _serialize_datetime ,
54- deserializer = _deserialize_datetime ,
55- )
56-
57- if inspect .isclass (param_type ):
58- if issubclass (param_type , BaseModel ):
59- return SchemaDeserializer (
60- schema = param_type .model_json_schema (by_alias = True , schema_generator = JsonSchemaGenerator ),
61- serializer = lambda x : cast (BaseModel , x ).model_dump (mode = "json" ), # pyright: ignore [reportUnknownLambdaType]
62- deserializer = param_type .model_validate ,
63- )
64-
65- if issubclass (param_type , Enum ):
66- if not issubclass (param_type , str ):
67- raise ValueError (f"Non string enums are not supported: { param_type } " )
68- return SchemaDeserializer ({"type" : "string" , "enum" : [e .value for e in param_type ]})
69-
7040 # Attempting to build a type adapter with pydantic
7141 with contextlib .suppress (Exception ):
7242 adapter = TypeAdapter [Any ](param_type )
7343 return SchemaDeserializer (
74- schema = adapter .json_schema (),
44+ schema = adapter .json_schema (schema_generator = JsonSchemaGenerator ),
7545 deserializer = adapter .validate_python , # pyright: ignore [reportUnknownLambdaType]
7646 serializer = lambda x : adapter .dump_python (x , mode = "json" ), # pyright: ignore [reportUnknownLambdaType]
7747 )
0 commit comments