pydantic-autotag
A lightweight Python library that simplifies working with tagged unions in Pydantic by automatically adding discriminator fields to your models and annotating union types. Using discriminators makes deserializing union types dramatically faster since it only has to deserialize the value as the correct type without trying all the other possible types.
Installation
Install from PyPI:
uv add pydantic-autotag
or
pip install pydantic-autotag
Features
- Automatic tagging: Add discriminator fields to Pydantic models with a simple decorator
- Tagged unions: Convert regular unions to Pydantic tagged unions with a single function call
- Custom field names: Use any field name for discrimination (defaults to "type")
- Custom tags: Use any function to generate the tag value for each class
Usage
from typing import TypeAlias, TYPE_CHECKING
from pydantic import BaseModel
from pydantic_autotag import autotagged_union
class Foo(BaseModel):
...
class Bar(BaseModel):
...
# Calling functions is not allowed in type definitions so this won't be accepted:
# FooOrBar = autotagged_union(Foo | Bar)
# Instead, define the union and then apply autotagging only when not type checking.
FooOrBar: TypeAlias = Foo | Bar
if not TYPE_CHECKING:
FooOrBar = autotagged_union(FooOrBar)
You can also decorate model classes directly:
from pydantic import BaseModel
from pydantic_autotag import autotag
@autotag
class Foo(BaseModel):
...
Customization
By default, each class has a type field added to it and the field's value is the class name.
This can be customized by passing field_name and/or tag_value keyword arguments to autotagged_union or autotag:
@autotag(field_name="name", tag_value="Happy")
class PositiveEmotion(BaseModel):
...
is equivalent to:
class PositiveEmotion(BaseModel):
name: Literal["Happy"] = Field(init=False, default="Happy", repr=False, frozen=True)
...
For unions, you must pass a function that returns the tag value for a given class:
FooOrBar = autotagged_union(FooOrBar, field_name="which", tag_value=lambda c: c.__name__.lower())
Type Safety
If you need to directly access the generated field and don't want your type checker to be unhappy, you can declare the field either as a str or as a Literal["ExpectedTagValue"]
and autotag will replace it at runtime just like it would if the field didn't exist. One
easy way to do this is with a base class:
class MyBase(BaseModel):
type: str
class A(MyBase):
...
class B(MyBase):
...
AorB: TypeAlias = A | B
if not TYPE_CHECKING:
AorB = autotagged_union(AorB)
Metadata
Release files for pydantic-autotag 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_autotag-1.0.1.tar.gz | 4.1 kB | Without provenance | Details for pydantic_autotag-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_autotag-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for pydantic_autotag-1.0.1-py3-none-any.whl |
Total release size: 9.2 kB
Release files / pydantic_autotag-1.0.1.tar.gz
| Download URL | pydantic_autotag-1.0.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
492b0ad87344a0c6d7a93ef3f8a2df14032fece62493f21c299e6178d35d2371
|
|
BLAKE2b-256 checksum How to use checksums |
dd030c73b33b5a43d19ebaf3d11fb0d63ad9dce4a632f057651a3cb240966693
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / pydantic_autotag-1.0.1-py3-none-any.whl
| Download URL | pydantic_autotag-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
14da6f9ca8da9d2f63b50ac4a25e2e22b7057c6a6c5a5b22b734c86411dc2ffa
|
|
BLAKE2b-256 checksum How to use checksums |
1861e7e2c802bcee041c90e9c007d99b22daa9e24c7c6c42fe963b0fc3251147
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for pydantic-autotag 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.