{"record":{"id":"66b575f56acb866e","repo":"apache/beam","slug":"failed-to-load-onnx-file-onnx-path","errorCode":null,"errorMessage":"Failed to load ONNX file: {onnx_path}","messagePattern":"Failed to load ONNX file: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/tensorrt_inference.py","lineNumber":72,"sourceCode":"  file = FileSystems.open(engine_path, 'rb')\n  runtime = trt.Runtime(TRT_LOGGER)\n  engine = runtime.deserialize_cuda_engine(file.read())\n  assert engine\n  return engine\n\n\ndef _load_onnx(onnx_path):\n  import tensorrt as trt\n  builder = trt.Builder(TRT_LOGGER)\n  network = builder.create_network(\n      flags=1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))\n  parser = trt.OnnxParser(network, TRT_LOGGER)\n  with FileSystems.open(onnx_path) as f:\n    if not parser.parse(f.read()):\n      LOGGER.error(\"Failed to load ONNX file: %s\", onnx_path)\n      for error in range(parser.num_errors):\n        LOGGER.error(parser.get_error(error))\n      raise ValueError(f\"Failed to load ONNX file: {onnx_path}\")\n  return network, builder\n\n\ndef _build_engine(network, builder):\n  import tensorrt as trt\n  config = builder.create_builder_config()\n  runtime = trt.Runtime(TRT_LOGGER)\n  plan = builder.build_serialized_network(network, config)\n  engine = runtime.deserialize_cuda_engine(plan)\n  builder.reset()\n  return engine\n\n\ndef _assign_or_fail(args):\n  \"\"\"CUDA error checking.\"\"\"\n  from cuda import cuda\n  err, ret = args[0], args[1:]\n  if isinstance(err, cuda.CUresult):","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/tensorrt_inference.py#L54-L90","documentation":"Raised in tensorrt_inference._load_onnx when the TensorRT OnnxParser fails to parse the ONNX file; the individual parser errors are logged first, then a ValueError naming the ONNX path is raised so engine building aborts early.","triggerScenarios":"Loading an ONNX file whose format/IR version is unsupported by the installed TensorRT version, a corrupted/truncated file, or a model with unsupported operators — parser.parse returns False.","commonSituations":"ONNX exported by a newer opset than the TensorRT version supports; model exported with ops (e.g. custom or latest transformers ops) TRT cannot parse; downloading/serializing the file incorrectly (partial upload to GCS); mismatch between onnx and tensorrt package versions.","solutions":["Check the worker logs for the per-error output of parser.get_error to identify the failing op/opset","Re-export the model with an opset version supported by your TensorRT version (e.g. opset 13-17 for TRT 8.x)","Upgrade the TensorRT (and onnx/onnxruntime) versions in the worker image, or simplify/replace unsupported ops in the exported graph","Validate the file locally with onnx.checker.check_model before submitting the pipeline"],"exampleFix":"// before\n# exported with opset 18, worker has TensorRT 8.2 (max opset 17)\n// after\n# re-export: torch.onnx.export(model, x, path, opset_version=17)","handlingStrategy":"validation","validationCode":"import onnx\nm = onnx.load_model(onnx_path)\nonnx.checker.check_model(m)  # raises on invalid/unsupported models\nassert m.ir_version <= 8, 'ONNX IR version may exceed TensorRT support'","typeGuard":null,"tryCatchPattern":"try:\n    handler = TensorRTEngineHandlerNumpy(...)\nexcept ValueError as e:\n    if e.args and str(e).startswith('Failed to load ONNX file'):\n        raise RuntimeError('Re-export ONNX with an opset supported by the worker TensorRT version; see logs for parser errors') from e\n    raise","preventionTips":["Match the export opset to the TensorRT version in the worker image","Run onnx.checker and an onnxruntime load locally before pipeline launch","Pin onnx/tensorrt versions together in the container image"],"tags":["onnx","tensorrt","apache-beam","ml-inference","model-loading"],"backgroundTag":"file-read-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}