Serializing Pydantic Types

As per the answer here, we initialise our engine with a custom json serializer:

import json
import pydantic.json
 
 
def _custom_json_serializer(*args, **kwargs) -> str:
    """
    Encodes json in the same way that pydantic does.
    """
    return json.dumps(*args, default=pydantic.json.pydantic_encoder, **kwargs)
 
from sqlalchemy import create_engine
 
engine = create_engine(conn_string, json_serializer=_custom_json_serializer)
 

Notably pydantic.json.pydantic_encoder is now deprecated so there may be a more up to date, idiomatic way to do this