skillfed

torchtyping

Runtime type annotations for the shape, dtype etc. of PyTorch Tensors.

torchtyping v0.1.5 186.2K downloads/30d#9,992 on PyPI1,484
Permissive license Apache-2.0 AGING released

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 torchtyping

uv

uv add torchtyping

poetry

poetry add torchtyping

Installing 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

Development Status :: 3 - AlphaFramework :: PytestIntended Audience :: DevelopersIntended Audience :: Financial and Insurance IndustryIntended Audience :: Information TechnologyIntended Audience :: Science/ResearchLicense :: OSI Approved :: Apache Software LicenseNatural Language :: EnglishProgramming Language :: Python :: 3Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonTopic :: Scientific/Engineering :: Artificial IntelligenceTopic :: Scientific/Engineering :: Information AnalysisTopic :: Scientific/Engineering :: Mathematics

Tags

pytorch tensor type annotationsshape dtype checking tensorsruntime tensor validationnamed tensor type hintspytorch type safetytensor dimension checkingpytorch static type checking
pytorchtype-hintstensor-validation

More Artificial Intelligence packages