Recently, I have been developing a FastAPI application which relies on the excellent Pydantic package for data validation and serialization.

I will give a very short intro, in case you are not familiar with Pydantic. If we develop our Python class objects as derived from Pydantic’s BaseModel class, then we can have very helpful things like type validation, type hinting, JSON data serialization and so on for our class. e.g.

If you are dealing with scientific or numerical data in Python, naturally you will use Numpy arrays. But how to handle Numpy arrays within Pydantic BaseModel?

Numpy array as an ‘Annotated’ type

We can define a custom type for our Numpy arrays using the Annotated type. This will wrap around Numpy’s original ndarray class. But we need to provide two additional things:

A function to convert a provided string input to Numpy array - a before validation method

A function to serialize a provided Numpy array into List/string - a custom serialization method

Sample code for this custom datatype MyNumPyArray creation is given below:

Now, you can include numpy arrays in Pydantic classes as given below: