Pydantic Integration
Relevant source files
The following files were used as context for generating this wiki page:
Pydantic integration provides the core data validation and transformation layer for the faceit-python library. All data models inherit from a common base class to ensure strict type safety, automatic parsing of API responses, and runtime data integrity. This system converts raw JSON responses from FACEIT into validated Python objects with complex types like timestamps, UUIDs, and game-specific enums.
Sources: pyproject.toml:27, src/faceit/models/base.py, README.md:21
Core Architecture and Base Models
The project uses pydantic.BaseModel as the foundation for all data structures. The primary BaseModel class configures global behavior for models, such as prohibiting extra fields to maintain strict adherence to expected API schemas.
The diagram shows the inheritance hierarchy where specialized models derive from a central project-specific base.
Model Configuration
Data models utilize specific Pydantic features to handle the inconsistencies of the FACEIT API:
- Alias Management: Models use
Field(alias="...")to map snake_case Python attributes to the mixed casing used in API responses (e.g., mappingplayer_idtoplayer_idorskill_leveltoskill_level). - Validation Modes: The system uses
model_validator(mode="before")to sanitize or inject data before standard field validation occurs.
Sources: src/faceit/models/players/general.py:46-77, src/faceit/models/item_page.py:32-44
Validation Logic and Data Flow
The library integrates Pydantic validation directly into the API client resources. When a resource receives a response, it applies a validator based on the specific endpoint and game context.
The flow demonstrates how the BaseResource manages the transition from raw data to validated models.
Validation Components
| Component | Function | File Reference |
|---|---|---|
_validate_response | Executes the Pydantic model_validate call on raw responses. | src/faceit/api/base.py:108-135 |
MappedValidatorConfig | Maps specific keys (like GameID) to specialized model validators. | src/faceit/api/base.py:53-58 |
validate_call | Decorator used on API methods to enforce parameter types at runtime. | src/faceit/api/data/players.py:136 |
Sources: src/faceit/api/base.py:108-135, src/faceit/api/data/players.py:136
Custom Types and Advanced Parsing
Faceit-python implements specialized Pydantic types to handle domain-specific data formats found in the FACEIT ecosystem.
Specialized Types
- Timestamps:
TimestampMsandTimestampSecclasses inherit fromintand implement custom core schemas to support conversions between integers and UTCdatetimeobjects. - URL Handling:
LangFormattedAnyHttpUrluses aBeforeValidatorto remove language placeholders like{lang}from FACEIT URLs. - Skill Levels:
SkillLevelobjects are dynamically resolved during validation inGameInfomodels usingmodel_validator.
Sources: src/faceit/models/custom_types/common.py:46-85, src/faceit/models/players/general.py:56-77
Response Containers
The ResponseContainer class wraps dictionaries where the keys represent IDs. It uses a model_validator to inject the key back into the dictionary value as an injected_key, allowing models to access the identifier even when it was originally a key in a map.
Sources: src/faceit/models/custom_types/common.py:126-140
Paginated Collections (ItemPage)
Paginated responses are encapsulated in the ItemPage model. This model integrates Pydantic's Generic types to provide type-hinting for the items it contains.
Pagination Metadata
ItemPage transforms API pagination fields into a structured metadata property using @computed_field.
| Python Attribute | API Alias | Exclusion |
|---|---|---|
offset | start | exclude=True |
limit | end | exclude=True |
time_from | from | exclude=True |
time_to | to | exclude=True |
Sources: src/faceit/models/item_page.py:32-44
Normalization Logic
The _normalize_items class method uses a field_validator with mode="before". It ensures that paginated items are always returned as dictionaries, specifically unwrapping single-key nested dictionaries that sometimes appear in FACEIT's paginated lists.
Sources: src/faceit/models/item_page.py:175-197
Conclusion
Pydantic Integration ensures that all data entering the faceit-python library is sanitized and structured. By combining standard Pydantic models with custom validators for timestamps and dynamic game-specific model mapping, the library provides a reliable and type-safe interface for complex FACEIT data.
Sources: src/faceit/api/base.py, src/faceit/models/players/general.py