[GH-ISSUE #975] Convert all Spotify response objects to Pydantic models #584

Open
opened 2026-02-27 23:23:29 +03:00 by kerem · 4 comments
Owner

Originally created by @MrinalJain17 on GitHub (May 13, 2023).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/975

Is your feature request related to a problem? Please describe.

  • Working with raw JSON response objects/dictionaries is tedious.
  • No concrete information about the type and structure of any particular response. One has to refer to the official Spotify API documentation.

Describe the solution you'd like

All the responses can be represented as Pydantic models. https://docs.pydantic.dev/latest/

  • Enables better developer experience (integrates well with IDEs)
  • Strong type checking (and validation!)

Describe alternatives you've considered

I've used Pydantic before, so biased towards the tool. There might be other options, but the core feature request stays the same -- well-structured and validated python objects in favour of raw JSON responses.

Additional context

This can be thought of as a more rigorous version of #695.

Originally created by @MrinalJain17 on GitHub (May 13, 2023). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/975 **Is your feature request related to a problem? Please describe.** - Working with raw JSON response objects/dictionaries is tedious. - No concrete information about the type and structure of any particular response. One has to refer to the official Spotify API documentation. **Describe the solution you'd like** All the responses can be represented as Pydantic models. https://docs.pydantic.dev/latest/ - Enables better developer experience (integrates well with IDEs) - Strong type checking (and validation!) **Describe alternatives you've considered** I've used Pydantic before, so biased towards the tool. There might be other options, but the core feature request stays the same -- well-structured and validated python objects in favour of raw JSON responses. **Additional context** This can be thought of as a more rigorous version of #695.
Author
Owner

@stefanondisponibile commented on GitHub (Jun 1, 2023):

This idea is nice, but I can see how creating (and maintaining) such schemas could be a little bit tedious.
I was trying to figure out if we could get the json schemas somewhere, and create the type hints out of them. This way we'd just have to keep in "sync" with the schemas.

However, the only thing I found were these. The repository seems archived though, does anyone know if there are any fresher ones?

<!-- gh-comment-id:1571469923 --> @stefanondisponibile commented on GitHub (Jun 1, 2023): This idea is nice, but I can see how creating (and maintaining) such schemas could be a little bit tedious. I was trying to figure out if we could get the json schemas somewhere, and create the type hints out of them. This way we'd just have to keep in "sync" with the schemas. However, the only thing I found were [these](https://github.com/spotify/web-api/tree/master/specifications/raml/schemas). The repository seems archived though, does anyone know if there are any fresher ones?
Author
Owner

@mase-git commented on GitHub (Jun 3, 2023):

Problem here is the dynamic structure response that changes over time according to the Spotify API changelog. You need to adapt manually every schema and mantains the latest versions with updated schema. This identifies a notable effort spent to contributors.

Unfortunately, Spotify didn't update response schema very well and it's a bit tedious mantains a good schema without occurs in some issues.

<!-- gh-comment-id:1574812159 --> @mase-git commented on GitHub (Jun 3, 2023): Problem here is the dynamic structure response that changes over time according to the Spotify API changelog. You need to adapt manually every schema and mantains the latest versions with updated schema. This identifies a notable effort spent to contributors. Unfortunately, Spotify didn't update response schema very well and it's a bit tedious mantains a good schema without occurs in some issues.
Author
Owner

@dieser-niko commented on GitHub (Jun 21, 2023):

Even though pydantic and similar approaches probably won't be implemented in spotipy, I just want to leave this OpenAPI specification here: https://developer.spotify.com/reference/web-api/open-api-schema.yaml
Apparently it also has some wrong parts, but I didn't look into it yet.

Found in this community post: https://community.spotify.com/t5/Spotify-for-Developers/OpenApi-Swagger-description-for-the-Web-API/m-p/5329753#messageview_1

<!-- gh-comment-id:1600501052 --> @dieser-niko commented on GitHub (Jun 21, 2023): Even though pydantic and similar approaches probably won't be implemented in spotipy, I just want to leave this OpenAPI specification here: https://developer.spotify.com/reference/web-api/open-api-schema.yaml Apparently it also has some wrong parts, but I didn't look into it yet. Found in this community post: https://community.spotify.com/t5/Spotify-for-Developers/OpenApi-Swagger-description-for-the-Web-API/m-p/5329753#messageview_1
Author
Owner

@stefanondisponibile commented on GitHub (Jun 21, 2023):

yep, @dieser-niko is right. If that can be of help for @MrinalJain17 or anyone in the future one option is converting that openapi schema to pydantic classes (pydantic docs, datamodel-code-generator). So ideally, you could have a thin layer to convert spotipy's responses to Pydantic objects. IMO this remains overkill to implement and remain in sync, but just in case someone needs it :)

<!-- gh-comment-id:1600518067 --> @stefanondisponibile commented on GitHub (Jun 21, 2023): yep, @dieser-niko is right. If that can be of help for @MrinalJain17 or anyone in the future one option is converting that openapi schema to pydantic classes ([pydantic docs](https://docs.pydantic.dev/latest/datamodel_code_generator/), [datamodel-code-generator](https://github.com/koxudaxi/datamodel-code-generator/)). So ideally, you could have a thin layer to convert spotipy's responses to Pydantic objects. IMO this remains overkill to implement and remain in sync, but just in case someone needs it :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#584
No description provided.