{"record":{"id":"3c91150b22b820c2","repo":"keras-team/keras","slug":"the-pytorch-export-requires-the-filepath-to-end-wi","errorCode":null,"errorMessage":"The PyTorch export requires the filepath to end with '.pt2'. Got: {filepath}","messagePattern":"The PyTorch export requires the filepath to end with '\\.pt2'\\. Got: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/export/torch.py","lineNumber":60,"sourceCode":"            for tracing uses a batch size of 2 instead of 1. This avoids a\n            PyTorch limitation where dimensions of size 1 are specialized\n            to constants during export.\n\n    Example:\n\n    ```python\n    model.export(\"path/to/model.pt2\", format=\"torch\")\n\n    import torch\n    loaded_program = torch.export.load(\"path/to/model.pt2\")\n    output = loaded_program.module()(torch.randn(1, 10))\n    ```\n    \"\"\"\n    import torch\n\n    filepath = str(filepath)\n    if not filepath.endswith(\".pt2\"):\n        raise ValueError(\n            \"The PyTorch export requires the filepath to end with \"\n            f\"'.pt2'. Got: {filepath}\"\n        )\n\n    export_kwargs = _get_export_kwargs(kwargs)\n    dynamic_shapes = export_kwargs.get(\"dynamic_shapes\")\n\n    if input_signature is None:\n        input_signature = get_input_signature(model)\n\n    # PyTorch limitation: torch.export specializes dimensions of size 1 to\n    # constants during tracing. If a dynamic dim (e.g., batch) has a concrete\n    # sample value of 1, export fails with \"specialized it to be a constant\".\n    # Using a sample value of 2 (the smallest integer > 1) avoids this.\n    # See: https://github.com/pytorch/pytorch/issues/176349\n    replace_none_number = 2 if dynamic_shapes is not None else 1\n    sample_inputs = tree.map_structure(\n        lambda x: convert_spec_to_tensor(","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/export/torch.py#L42-L78","documentation":"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.","triggerScenarios":"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.","commonSituations":"Muscle memory from torch.save with .pt or .pth; pipelines that derive export filenames from a basename variable without appending the .pt2 suffix.","solutions":["Rename the target to end with '.pt2': export_torch('model.pt2').","When calling model.export(filepath), ensure the filepath string ends in .pt2 for the torch export path.","If you need a .pt or .pth artifact, export to .pt2 and rename afterwards, or use torch.save yourself."],"exampleFix":"# before\nmodel.export('checkpoints/model.pt')  # -> ValueError\n\n# after\nmodel.export('checkpoints/model.pt2')","handlingStrategy":"validation","validationCode":"filepath = str(filepath)\nif not filepath.endswith('.pt2'):\n    filepath += '.pt2'\nexport_torch(model, filepath)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Normalize every export path through a helper that enforces the expected extension.","Remember the Keras torch export uses .pt2, not torch's .pt or .pth.","Fail fast on bad suffixes during config validation, not at export time."],"tags":["keras","pytorch","export","file-extension","validation"],"backgroundTag":"invalid-file-extension","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}