The choice: either take a library dependency, or solve the problem another way.
When would you? When wouldn’t you? Either way, how do you mitigate the risk of your choice?
What are some of the key factors that go into each such decision?
All else equal, what’s your preference?
For me, complexity and size vs. time. If it's a smallish bit of functionality I've written several times before (previous employers, etc), I can probably bash it out again in less time than it takes to find a 3rd-party lib, check its licensing, and review its codebase and history to figure out if it's likely to be secure and well-implemented.
If it's a big chunk of work and I know of a 3rd-party package that I know is suitable - FastAPI, Pydantic, etc - I'll use that.
@cazabon to my ears, a very sensible first-order weighing of costs and risks. And probably similar to how I decide.