[Python] What do I need to do in pydantic to support a FastAPI endpoint involving a sparse...

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am aware of apparently similar questions, such as


    These questions do not cover my specific needs.

    I need to make a web api endpoint, using the python package FastAPI. I'd like to do this with pydantic data models. My end point needs to communicate a sparse matrix. My core logic uses scipy for some linear algebra. Pydantic does not support scipy types. I do not want to convert to some dense representation, this is wasteful.

    I could convert to some custom sparse representation that makes use of pure base types. But I think it would be nicer to instead implement all the "things" (not sure what word to use here) that pydantic needs to support the sparse matrix directly.

    From what I can tell by reading the documentation, I need to implement a bunch of "things" like validators and serializers. But the documentation does not seem to tell me what is the minimum set of "things" I need to implement in order for this to work with FastAPI and frankly I could do with more examples than the one they give.

    I have come up with the following transformer, but not sure if this is even useful for directly supporting a lil_matrix.

    def sparse_to_coo_dict(mat: lil_matrix) -> dict[str, Any]:
    coo = mat.tocoo()
    return {
    "format": "coo",
    "shape": coo.shape,
    "row": coo.row.tolist(),
    "col": coo.col.tolist(),
    "data": coo.data.tolist(),
    }


    For sure I could use this to translate my stuff into base types over the wire. But I don't need to worry about the pydantic "things" to do that. So I am assuming there is some fancier way involving specific features of pydantic than just converting to base types.

    So my question is: what exactly do I need to do to support a lil_matrix in pydantic to use with FastAPI?

    Continue reading...

Compartilhe esta Página