keras-team/keras · error · ValueError

The PyTorch export requires the filepath to end with '.pt2'.

Error message

The PyTorch export requires the filepath to end with '.pt2'. Got: {filepath}

What it means

export_torch writes a torch.export archive and enforces the '.pt2' extension as a contract on the filepath. Any other suffix (or no suffix) raises ValueError before any torch work happens. The check is a plain str(filepath).endswith('.pt2'), so even familiar PyTorch names like 'model.pt' or 'model.pth' are rejected.

Source

Thrown at keras/src/export/torch.py:60

            for tracing uses a batch size of 2 instead of 1. This avoids a
            PyTorch limitation where dimensions of size 1 are specialized
            to constants during export.

    Example:

    ```python
    model.export("path/to/model.pt2", format="torch")

    import torch
    loaded_program = torch.export.load("path/to/model.pt2")
    output = loaded_program.module()(torch.randn(1, 10))
    ```
    """
    import torch

    filepath = str(filepath)
    if not filepath.endswith(".pt2"):
        raise ValueError(
            "The PyTorch export requires the filepath to end with "
            f"'.pt2'. Got: {filepath}"
        )

    export_kwargs = _get_export_kwargs(kwargs)
    dynamic_shapes = export_kwargs.get("dynamic_shapes")

    if input_signature is None:
        input_signature = get_input_signature(model)

    # PyTorch limitation: torch.export specializes dimensions of size 1 to
    # constants during tracing. If a dynamic dim (e.g., batch) has a concrete
    # sample value of 1, export fails with "specialized it to be a constant".
    # Using a sample value of 2 (the smallest integer > 1) avoids this.
    # See: https://github.com/pytorch/pytorch/issues/176349
    replace_none_number = 2 if dynamic_shapes is not None else 1
    sample_inputs = tree.map_structure(
        lambda x: convert_spec_to_tensor(

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Rename the target to end with '.pt2': export_torch('model.pt2').
  2. When calling model.export(filepath), ensure the filepath string ends in .pt2 for the torch export path.
  3. If you need a .pt or .pth artifact, export to .pt2 and rename afterwards, or use torch.save yourself.

Example fix

# before
model.export('checkpoints/model.pt')  # -> ValueError

# after
model.export('checkpoints/model.pt2')
Defensive patterns

Strategy: validation

Validate before calling

filepath = str(filepath)
if not filepath.endswith('.pt2'):
    filepath += '.pt2'
export_torch(model, filepath)

Prevention

When it happens

Trigger: model.export('model.pt') or model.export('model.pth') assuming PyTorch conventions; passing a Path or filename template that does not end in .pt2; calling export_torch(filepath='out') directly.

Common situations: Muscle memory from torch.save with .pt or .pth; pipelines that derive export filenames from a basename variable without appending the .pt2 suffix.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/3c91150b22b820c2. Report an issue: GitHub.