Anvil and Pydantic

Hi there, I am trying to use pydantic to write nice code but pydantic does not seem to be available at the client side and I am getting serialization errors

Cannot serialize return value from function. Cannot serialize <class ‘LeveeLogicNew.Globals.Project’> object at msg[‘response’]

Here are my classes

from typing import Dict, List
from pydantic import BaseModel

class Cpt(BaseModel):
  id: str
  project_id: str
  lat: float
  lon: float 
  location: str
  z: float
  name: str
  classification: Dict = {}

class Project(BaseModel):
  id: str
  code: str
  description:str
  reference_line: Dict = {}
  robertson: Dict = {}
  cpts: List[Cpt] = []

What is the problem for the serialization? Or shoud I be changing everything to dict’s myself… I was hoping to use Project(**project_dict) from the client side but that is impossible since this will need to import the typing and pydantic packages which are not available.

Regards,
Rob

I think this post is relevant to this topic

1 Like

I am now reading this Anvil Docs | Portable Classes and I think this should be the way to go for me… but thanks!

1 Like

You might also like to look at the serialization module of Anvil Extras

https://anvil-extras.readthedocs.io/en/latest/guides/modules/serialisation.html

And the more experimental kompot module in Anvil Labs:

2 Likes