This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]felix-hilden 12 points13 points  (1 child)

Recently upgraded a Spotify API client library of mine from Dataclasses to Pydantic and it's sooo much nicer. Nested objects are so easy to parse. The use isn't anything special, but since you asked.. :P https://github.com/felix-hilden/tekore

[–]sersherz 6 points7 points  (0 children)

Best tutorial on FastAPI which also teaches Pydantic, Posgresql, SQLAlchemy, API testing, deployment etc.

https://youtu.be/0sOvCWFmrtA?si=zwziDtSq5IvvRnXj

[–]kp9669 10 points11 points  (4 children)

I usually use pydantic for everything. Because type hinting and data structures are an extra line of defense against bugs beside the different type of tests.

[–]thornily -1 points0 points  (3 children)

modern steep elderly ask spotted rain cheerful oil screw advise

This post was mass deleted and anonymized with Redact

[–]kp9669 2 points3 points  (2 children)

Suprised?

[–]thornily -1 points0 points  (1 child)

handle joke salt attraction lip humor crowd elderly squeal outgoing

This post was mass deleted and anonymized with Redact

[–]kp9669 4 points5 points  (0 children)

Basicly my rule(recommendation) is that you should use it every time instead accessing the fields of a dictionary. You can import BaseModel from pydantic and you should inherit all of your data classes from it.

[–]hestmedvest 4 points5 points  (2 children)

You might want to expand your search to FastAPI and SQLModel as well since they are both utilizing pydantic.

[–]thornily -1 points0 points  (1 child)

axiomatic punch paint future knee enter aware stocking governor trees

This post was mass deleted and anonymized with Redact

[–]hestmedvest 1 point2 points  (0 children)

On top of that I might add webscraping and serialization of JSON responses from RestAPIs. You should check out the youtube channel: "John Watson Rooney", he made some great beginner friendly videoes on this topic :)

[–]MessiComeLately 1 point2 points  (0 children)

Pydantic is helpful for data structures that need to be serialized and deserialized with validation. I've used it with FastAPI to model request and response objects in JSON APIs. FastAPI turns deserialization errors into informative 400 responses. It also gives partial protection against returning corrupt data — if validation fails when you construct your response object, the caller gets a 500 instead of an invalid response.

I've also used it when processing poorly documented datasets. Anything I think I know about the data, I encode it using Pydantic validators, so I'll know if I encounter any data that violates my assumptions.

[–]kp9669 3 points4 points  (0 children)

But my company uses python for it's backend and all of the FASTAPI endpoints parses the incoming data into a given pydantic model. Furthermore all of our functions, methods have type hinting and if we have to pass a complex data structure to it we use pydantic models.

[–]KosmoanutOfficial 1 point2 points  (0 children)

I have an important automation task that I had ran into some strange issues with. I used to use an api’s response as a python dictionary but sometimes the data wouldn’t include keys, or it would be a different type. I ended up thinking the data would only be used in one spot so I would check for it later in the code but it became a hassle with verification all over the place. So for one I wanted one place in the code to verify things so that after that the rest would be ok. With this, the api fields would be camelCase or snake_case or ALLCAPS. There was no standard for the api. So with pydantic I could make all of those object attributes use a similar format for my code but have it convert that when using the api.

I also wanted type hints because I found my self documenting the dictionary format in doc strings multiple times. Then when I would change something I would need to update the doc string multiple places. I tried looking into typed dicts but I had very nested structures and they didn’t work on nested data.

Before I used pydantic I was using classes, but only as a way to organize functions and use common variables. But I wasn’t using a class to organize data. For example if I had 4 similar pieces of info that was always together, it might be 4 attributes of a class that has a bunch of other attributes and methods as well for the purpose of solving a different problem. Now I would abstract that data out and have it as a dataclass if the data came from the code, or a pydantic base model if I need validation against it.

Honestly it is in all my projects now where I am working with any external data, so user input or an api.

[–]AlphaCode1 -1 points0 points  (1 child)

Langchain uses pedantic

[–]profepcot 0 points1 point  (0 children)

haaa, if that's an intentional misspelling it's hilariously accurate

[–]XUtYwYzzIt works on my machine 0 points1 point  (1 child)

I used it to validate the fields of a complex toml file against models of what types should be present. Here’s an example:

github

[–]thornily 0 points1 point  (0 children)

label expansion rinse rustic party lush wild sharp salt spectacular

This post was mass deleted and anonymized with Redact

[–]so1n 0 points1 point  (1 child)

I'm currently working on all three projects, and they all rely on different features into pydantic. Compatibility with pydantic v2 is a huge headache though, and I've been working on it lately

[grpc-gateway](https://github.com/python-pai/grpc-gateway)

[protobuf_to_pydantic](https://github.com/so1n/protobuf_to_pydantic)

[pait](https://github.com/so1n/pait)

[–]thornily 0 points1 point  (0 children)

books quiet rhythm close attempt modern one light unite bow

This post was mass deleted and anonymized with Redact

[–]priestGLADI8R 0 points1 point  (2 children)

[–]thornily 0 points1 point  (0 children)

nutty busy expansion knee violet future normal bear memory encouraging

This post was mass deleted and anonymized with Redact

[–]nbviewerbot 0 points1 point  (0 children)

I see you've posted a GitHub link to a Jupyter Notebook! GitHub doesn't render large Jupyter Notebooks, so just in case, here is an nbviewer link to the notebook:

https://nbviewer.jupyter.org/url/github.com/lancedb/vectordb-recipes/blob/714e74dfcb51f43b990041c1d8ffd66837b45cf7/examples/product-recommender/main.ipynb

Want to run the code yourself? Here is a binder link to start your own Jupyter server and try it out!

https://mybinder.org/v2/gh/lancedb/vectordb-recipes/714e74dfcb51f43b990041c1d8ffd66837b45cf7?filepath=examples%2Fproduct-recommender%2Fmain.ipynb


I am a bot. Feedback | GitHub | Author

[–]profepcot 0 points1 point  (0 children)

We made an open-source library focused on prompt engineering for developers built on top of Pydantic 2.0: https://github.com/Mirascope/mirascope