[Python] Excluding pydantic model fields only when returned as part of a FastAPI call

Discussão em 'Python' iniciado por Stack, Fevereiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    Context


    I have a very complex pydantic model with a lot of nested pydantic models. I would like to ensure certain fields are never returned as part of API calls, but I would like those fields present for internal logic.

    What I tried


    I first tried using pydantic's Field function to specify the exclude flag on the fields I didn't want returned. This worked, however functions in my internal logic had to override this whenever they called .dict() by calling .dict(exclude=None).

    Instead, I specified a custom flag return_in_api on the Field, with the goal being to only apply exclusions when FastAPI called .dict(). I tried writing a middleware to call .dict() and pass through my own exclude property based on which nested fields contained return_in_api=False. However FastAPI's middleware was giving me a stream for the response which I didn't want to prematurely resolve.

    Instead, I wrote a decorator that called .dict() on the return values of my route handlers with the appropriate exclude value.

    Problem


    One challenge is that whenever new endpoints get added, the person who added them has to remember to include this decorator, otherwise fields leak.

    Ideally I would like to apply this decorator to every route, but doing it through middleware seems to break response streaming.

    Continue reading...

Compartilhe esta Página