torchtyping
Runtime type annotations for the shape, dtype etc. of PyTorch Tensors.
What it is and what it does
torchtyping lets you annotate PyTorch tensors with their expected shape, dtype, layout, and dimension names directly in function signatures. Instead of writing comments like `# x has shape (batch, hidden_state)`, you write `x: TensorType["batch", "hidden_state"]`, making tensor contracts explicit and machine-readable. When typeguard is installed and enabled, these annotations are checked at runtime to catch shape mismatches and dtype errors early.
The package integrates with typeguard for optional runtime validation and pytest for test-time checking, so you can enforce tensor contracts without runtime overhead in production. It supports flexible shape specifications (exact sizes, named dimensions, variable batch dimensions with `...`), multiple dtype checks, and extensible custom validation rules. However, the author now recommends jaxtyping for new projects, citing better static type checker compatibility.
Use it for:
- Document tensor shapes in neural network functions to prevent shape mismatch bugs during model development.
- Validate batch dimensions and channel sizes at runtime during training to catch data pipeline errors early.
- Enforce consistent dimension naming across a codebase to improve code clarity and reduce shape-related debugging.
- Test tensor contracts in pytest with automatic typeguard patching to catch shape violations in unit tests.
- Combine with typeguard's import hook to automatically check all tensor operations in a module without manual decorators.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Adds type annotations for PyTorch tensor shape, dtype, layout, and dimension names with optional runtime checking via typeguard.
Yes, if you are actively developing PyTorch models and want runtime shape validation. The low install friction and permissive license make adoption straightforward. However, be aware that the package is aging and the author recommends jaxtyping for new projects due to better static type checker support. If you need static type checking or are starting a new project, consider jaxtyping instead; if you have an existing codebase using torchtyping or prefer runtime-only validation, it remains functional.
Install
torchtyping on PyPI
pip
pip install torchtypinguv
uv add torchtypingpoetry
poetry add torchtypingInstalling torchtyping
Before you install
Low friction installation with only torch and typeguard as runtime dependencies. Maintenance status is aging—last release was in 2024—but the repository remains active and unarchived with 1484 stars.
License in practice
Apache-2.0 permissive license allows commercial and private use with minimal restrictions; suitable for most projects.
Quickstart
pip install torchtyping typeguard
from torch import rand
from torchtyping import TensorType, patch_typeguard
from typeguard import typechecked
patch_typeguard()
@typechecked
def func(x: TensorType["batch"], y: TensorType["batch"]) -> TensorType["batch"]:
return x + y
func(rand(3), rand(3)) # works
Requires typeguard <3.0.0 if using runtime checking; Python >=3.7 and PyTorch >=1.7.0.
Verify before relying
- Whether static type checkers (mypy, pyright) fully recognize TensorType annotations without additional configuration
- Current adoption rate and community support level relative to the newer jaxtyping recommendation
- Whether the package remains actively maintained beyond the most recent release date
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.7.0) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — torch, typeguard |
| Maintenance | aging — 743 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 186,194/month — #9,992 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: torchtyping-0.1.5-py3-none-any.whl
Tags
More Artificial Intelligence packages
LiteLLM provides a unified Python interface to…
permissive · top 100 on PyPI
huggingface-hubClient library and CLI tool for downloading,…
permissive · top 100 on PyPI
langchainLangChain provides a framework for building…
permissive · top 1,000 on PyPI
hf-xethf-xet provides chunk-based deduplication and…
permissive · top 1,000 on PyPI
tokenizersTokenizers converts raw text into token…
permissive · top 1,000 on PyPI
transformersTransformers provides a unified framework for…
permissive · top 1,000 on PyPI
jaxtypingProvides type annotations and runtime…
permissive · top 5,000 on PyPI
lovely-tensorsMonkey-patches PyTorch tensors to display…
permissive · top 15,000 on PyPI
typeguardPerforms run-time type checking for Python…
permissive · top 1,000 on PyPI
spmd-typesProvides type annotations and runtime checking…
permissive · top 15,000 on PyPI
torch-complexProvides a Python class wrapping real and…
permissive · top 15,000 on PyPI
safetensorsSerializes and deserializes tensors to and from…
permissive · top 1,000 on PyPI
nptypingProvides type hints and runtime type checking…
permissive · top 15,000 on PyPI
torchPyTorch provides GPU-accelerated tensor…
permissive · top 1,000 on PyPI
rotary-embedding-torchImplements rotary positional embeddings for…
permissive · top 15,000 on PyPI
pytorchThis package is a redirect or placeholder; the…
unclear · top 15,000 on PyPI