pandera.api.tensordict.container.TensorDictSchema¶

class pandera.api.tensordict.container.TensorDictSchema(keys=None, batch_size=None, dtype=None, checks=None, coerce=False, name=None, title=None, description=None, metadata=None)[source]¶

Validate TensorDict and tensorclass objects.

Analogous to DataFrameSchema for DataFrames and DatasetSchema for xarray Datasets.

The schema accepts a keys parameter (instead of columns) which is more semantically appropriate for TensorDict’s dictionary-like structure.

Keys may be plain strings or nested tuples, e.g. ("next", "observation"), matching TorchRL’s nested TensorDict layout.

Create TensorDict schema.

Parameters:
  • keys (UnionType[dict[Union[str, tuple[str, …]], Tensor], None]) – Dictionary mapping key names to Tensor components. Keys may be strings or nested tuples, e.g. ("next", "observation"). If a list of strings is provided, creates default Tensor components for each key.

  • batch_size (UnionType[tuple[UnionType[int, None], …], None]) – Expected batch dimensions. Use None for flexible dimensions. For example, (32,) requires exactly 32 samples, while (None,) allows any batch size.

  • dtype (Any) – Override dtype for all tensors (optional).

  • checks – Schema-wide checks applied to the entire TensorDict.

  • coerce (bool) – Enable automatic dtype coercion during validation.

  • name (UnionType[str, None]) – Name of the schema.

  • title (UnionType[str, None]) – Human-readable title for the schema.

  • description (UnionType[str, None]) – Detailed description of the schema.

  • metadata (UnionType[dict, None]) – Additional metadata associated with the schema.

Attributes

BACKEND_REGISTRY

properties

Get the properties of the schema for serialization purposes.

Methods

__init__(keys=None, batch_size=None, dtype=None, checks=None, coerce=False, name=None, title=None, description=None, metadata=None)[source]¶

Create TensorDict schema.

Parameters:
  • keys (UnionType[dict[Union[str, tuple[str, …]], Tensor], None]) – Dictionary mapping key names to Tensor components. Keys may be strings or nested tuples, e.g. ("next", "observation"). If a list of strings is provided, creates default Tensor components for each key.

  • batch_size (UnionType[tuple[UnionType[int, None], …], None]) – Expected batch dimensions. Use None for flexible dimensions. For example, (32,) requires exactly 32 samples, while (None,) allows any batch size.

  • dtype (Any) – Override dtype for all tensors (optional).

  • checks – Schema-wide checks applied to the entire TensorDict.

  • coerce (bool) – Enable automatic dtype coercion during validation.

  • name (UnionType[str, None]) – Name of the schema.

  • title (UnionType[str, None]) – Human-readable title for the schema.

  • description (UnionType[str, None]) – Detailed description of the schema.

  • metadata (UnionType[dict, None]) – Additional metadata associated with the schema.

classmethod get_backend(check_obj=None, check_type=None)[source]¶

Get the backend for TensorDict validation.

Overrides base class to handle tensorclass objects which have unique types at runtime. We use TensorDict’s backend since tensorclasses share the same interface and both inherit from object in their MRO.

Return type:

BaseSchemaBackend

static register_default_backends(check_obj_cls)[source]¶

Register default backends at validation time.

Return type:

None

validate(check_obj, head=None, tail=None, sample=None, random_state=None, lazy=False, inplace=False)[source]¶

Validate a TensorDict against the schema.

Parameters:
  • check_obj (Any) – TensorDict or tensorclass to validate.

  • head (UnionType[int, None]) – Number of leading samples to validate (not used for TensorDict).

  • tail (UnionType[int, None]) – Number of trailing samples to validate (not used for TensorDict).

  • sample (UnionType[int, None]) – Number of random samples to validate (not used for TensorDict).

  • random_state (UnionType[int, None]) – Random seed for sampling (not used for TensorDict).

  • lazy (bool) – If True, collect all errors instead of fail-fast.

  • inplace (bool) – Whether to modify object in place (not supported for TensorDict).

Return type:

Any

Returns:

Validated TensorDict or tensorclass.

Raises:

SchemaError – If validation fails.