Joe
11/27/2024, 2:45 PM@flow()
fails if I pass an argument that is either a dataclass or a Pydantic model that contains a numpy array. Here is the code for a minimal repro:
from dataclasses import dataclass
import numpy as np
from prefect import flow
from pydantic import BaseModel
@dataclass
class T1:
data: np.ndarray
@flow(name="test1", validate_parameters=True)
def test_flow1(t: T1):
print(t)
class T2(BaseModel):
data: np.ndarray
@flow(name="test2", validate_parameters=True)
def test_flow2(t: T2):
print(t)
test_flow1(T1(data=np.random.rand(2, 2)))
test_flow2(T2(data=np.random.rand(2, 2)))
Bianca Hoch
11/27/2024, 3:45 PMJoe
11/27/2024, 3:49 PMBianca Hoch
11/27/2024, 5:12 PMBianca Hoch
11/27/2024, 5:14 PM3.1.4
has fixes that address some breaking changes with a recent pydantic releaseBianca Hoch
11/27/2024, 5:14 PMBianca Hoch
11/27/2024, 5:21 PMfrom dataclasses import dataclass
import numpy as np
from prefect import flow
from pydantic import BaseModel, ConfigDict
@dataclass
class T1:
data: np.ndarray
@flow(name="test1", validate_parameters=True)
def test_flow1(t: T1):
print(t)
class T2(BaseModel):
data: np.ndarray
model_config = ConfigDict(arbitrary_types_allowed=True)
@flow(name="test2", validate_parameters=True)
def test_flow2(t: T2):
print(t)
test_flow1(T1(data=np.random.rand(2, 2)))
test_flow2(T2(data=np.random.rand(2, 2)))
Bianca Hoch
11/27/2024, 6:16 PMraceback (most recent call last):
File "/Users/joe/code/feature-library/features/repro.py", line 13, in <module>
@flow(name="test1", validate_parameters=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/prefect/flows.py", line 1597, in flow
Flow(
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/prefect/flows.py", line 334, in __init__
ValidatedFunction(self.fn, config={"arbitrary_types_allowed": True})
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/v1/decorator.py", line 126, in __init__
self.create_model(fields, takes_args, takes_kwargs, config)
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/prefect/_internal/pydantic/v2_validated_func.py", line 93, in create_model
self.model = create_model(
^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 1673, in create_model
return meta(
^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 226, in __new__
complete_model_class(
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_model_construction.py", line 658, in complete_model_class
schema = cls.__get_pydantic_core_schema__(cls, handler)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 697, in __get_pydantic_core_schema__
return handler(source)
^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 84, in __call__
schema = self._handler(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 612, in generate_schema
schema = self._generate_schema_inner(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 881, in _generate_schema_inner
return self._model_schema(obj)
^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 693, in _model_schema
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1073, in _generate_md_field_schema
common_field = self._common_field_schema(name, field_info, decorators)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1261, in _common_field_schema
schema = self._apply_annotations(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2051, in _apply_annotations
schema = get_inner_schema(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 84, in __call__
schema = self._handler(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2032, in inner_handler
schema = self._generate_schema_inner(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 886, in _generate_schema_inner
return self.match_type(obj)
^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 984, in match_type
return self._dataclass_schema(obj, None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1765, in _dataclass_schema
args = sorted(
^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1766, in <genexpr>
(self._generate_dc_field_schema(k, v, decorators) for k, v in fields.items()),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1090, in _generate_dc_field_schema
common_field = self._common_field_schema(name, field_info, decorators)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 1261, in _common_field_schema
schema = self._apply_annotations(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2051, in _apply_annotations
schema = get_inner_schema(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_schema_generation_shared.py", line 84, in __call__
schema = self._handler(source_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 2032, in inner_handler
schema = self._generate_schema_inner(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 886, in _generate_schema_inner
return self.match_type(obj)
^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 997, in match_type
return self._unknown_type_schema(obj)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/joe/Library/Caches/pypoetry/virtualenvs/feature-library-5-bR7ZDt-py3.12/lib/python3.12/site-packages/pydantic/_internal/_generate_schema.py", line 515, in _unknown_type_schema
raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for <class 'numpy.ndarray'>. Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.
If you got this error by calling handler(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.
For further information visit <https://errors.pydantic.dev/2.10/u/schema-for-unknown-type>
Joe
11/27/2024, 6:38 PMBianca Hoch
11/27/2024, 7:05 PMConfigDict
line to the data class isn't necessary (example above has been adjusted).Joe
11/27/2024, 7:05 PMBianca Hoch
11/27/2024, 7:06 PMJoe
11/27/2024, 7:06 PMJoe
11/27/2024, 7:08 PMBianca Hoch
11/27/2024, 7:08 PM