Complete Python type hints for the Spotify Web API using Pydantic models
Project description
Spotipy Types
Complete Python type hints for the Spotify Web API using Pydantic v2 models.
Overview
This package provides complete Python type hints for the Spotify Web API by auto-generating Pydantic v2 models from the official OpenAPI schema. This enables full IDE autocomplete, type checking, and runtime validation when working with spotipy.
- 103 Pydantic v2 models auto-generated from the official Spotify OpenAPI schema
- Full IDE autocomplete for all API response fields
- Runtime validation of API responses with detailed error messages
- Type-safe development with mypy support
- JSON serialization for caching and storage
Installation
pip install spotipy-types
Requires Python 3.11+ and Pydantic v2.
Quick Start
Type-Safe Track Access
from spotipy_types import TrackObject
import spotipy
# Initialize spotipy as usual
sp = spotipy.Spotify(auth=token)
# Add type annotation for IDE autocomplete
track: TrackObject = sp.track("11dFghVXANMlKmJXsNCbNl")
# Full autocomplete support!
print(track.name) # "Cut To The Feeling"
print(track.artists[0].name) # "Carly Rae Jepsen"
print(track.duration_ms) # 207959
print(track.explicit) # False
Validation
from spotipy_types import TrackObject
# Runtime validation of API responses
raw_data = {
"id": "abc123",
"name": "Song Name",
"type": "track",
"uri": "spotify:track:abc123",
"duration_ms": 180000,
"explicit": False,
"popularity": 50,
}
track = TrackObject.model_validate(raw_data)
# ValidationError if data doesn't match schema
Search Results
from spotipy_types import PagingTrackObject
results = sp.search(q="radiohead", type="track")
tracks_page: PagingTrackObject = PagingTrackObject.model_validate(results['tracks'])
# Type-safe access to paginated results
for track in tracks_page.items:
print(f"{track.name} by {track.artists[0].name}")
Playlist Management
from spotipy_types import PlaylistObject, PlaylistTrackObject
playlist: PlaylistObject = sp.playlist("playlist_id")
tracks: list[PlaylistTrackObject] = playlist.tracks.items
for item in tracks:
track = item.track
added_by = item.added_by.display_name
print(f"{track.name} added by {added_by}")
Supported Models
Core Objects
TrackObject- Full track informationAlbumObject- Full album informationArtistObject- Full artist informationPlaylistObject- Full playlist informationEpisodeObject- Podcast episodeShowObject- Podcast showAudiobookObject- AudiobookChapterObject- Audiobook chapter
User Objects
PrivateUserObject- Current user profilePublicUserObject- Public user profile
Simplified Objects
SimplifiedTrackObjectSimplifiedAlbumObjectSimplifiedArtistObjectSimplifiedPlaylistObjectSimplifiedEpisodeObjectSimplifiedShowObject
Response Types
PagingObject- Paginated resultsCursorPagingObject- Cursor-based paginationAudioFeaturesObject- Track audio featuresAudioAnalysisObject- Detailed audio analysisRecommendationsObject- Recommendation results
Common Types
ImageObject- Album/artist imagesFollowersObject- Follower countsExternalUrlObject- External linksExternalIdObject- External IDs (ISRC, EAN, UPC)CopyrightObject- Copyright informationErrorObject- API error responses
Plus 70+ additional models covering all Spotify API responses.
Type Checking with mypy
# mypy will catch type errors
from spotipy_types import TrackObject
track: TrackObject = sp.track("some_id")
# This will fail mypy - id is a string, not int
print(track.id + 1) # Error: Unsupported operand types for + ("str" and "int")
# This works
track_id: str = track.id
Development
Regenerating Models
If you have an updated OpenAPI schema:
# Install dev dependencies
pip install -e ".[dev]"
# Generate models
python scripts/generate_models.py
# Or use Make
make generate
Running Tests
make test
# or
pytest tests/ -v
Type Checking
make lint
# or
mypy src/spotipy_types/
Versioning
The package version follows the Spotify Web API schema version. For example:
- Package version
2025.5.18corresponds to schema version2025.5.18
Pin to specific versions for reproducibility:
pip install spotipy-types==2025.5.18
Migration from Untyped Spotipy
Before (no types)
import spotipy
sp = spotipy.Spotify(auth=token)
track = sp.track("id") # Returns dict
print(track["name"]) # No autocomplete, typo-prone
After (with spotipy-types)
import spotipy
from spotipy_types import TrackObject
sp = spotipy.Spotify(auth=token)
track: TrackObject = sp.track("id") # Same call, typed result
print(track.name) # IDE autocomplete works!
Why Pydantic v2?
- Best-in-class Python data validation
- Excellent IDE support via type hints
- JSON Schema compatibility
- Active development and large ecosystem
- Performance - Pydantic v2 is significantly faster than v1
Requirements
- Python 3.11+
- Pydantic 2.0+
We require Python 3.11+ to use:
- Union operator syntax (
X | Yinstead ofUnion[X, Y]) - Better performance
- Modern typing features
- Cleaner generated code
Schema Source
Models are generated from the official Spotify Web API OpenAPI schema:
- Repository: https://github.com/sonallux/spotify-web-api
- Schema version: 2025.5.18
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
Note: This is a community-maintained package and is not officially affiliated with Spotify.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spotipy_types-2025.5.18.tar.gz.
File metadata
- Download URL: spotipy_types-2025.5.18.tar.gz
- Upload date:
- Size: 109.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c02df9198eceff825217f5eaf68ae56651f1f046b22d5db423b29b10ed27ba5
|
|
| MD5 |
3eea65e2723ef1caf619311ebdc790b6
|
|
| BLAKE2b-256 |
8348d8ba1ddb009f6c7faee4ab102ae58b4c73af72d1a2d27a9f0916b491c3ce
|
File details
Details for the file spotipy_types-2025.5.18-py3-none-any.whl.
File metadata
- Download URL: spotipy_types-2025.5.18-py3-none-any.whl
- Upload date:
- Size: 21.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15393d67ffebf366ed7af08b7d5cd0c4dd4bc885a6e090975cc768f5db928928
|
|
| MD5 |
639d25cbcf6a6c4766f658d70caa6c09
|
|
| BLAKE2b-256 |
86e94c5be86b1c1bfb4bd145e5ec89e7349dcb55a6ae834c8fc46590b7e21b4a
|