apache/beam · error · TypeError
No SerializeToString method is detected on loaded model…
Error message
No SerializeToString method is detected on loaded model. Type of model: {type(model_proto)} What it means
ONNXModelHandler.load_model expects the loaded model as bytes or an object exposing SerializeToString() (a protobuf message, e.g. onnx.ModelProto). If the object is neither bytes nor protobuf-like, a TypeError is raised because the handler cannot serialize it for onnxruntime's InferenceSession.
Solutions
- Load with onnx.load(path) and pass the ModelProto (it has SerializeToString).
- Or read the file yourself and pass bytes: open(path,'rb').read().
- Ensure you pass onnxruntime-compatible ONNX protobuf, not a TorchScript/TF model.
Example fix
// before
handler = ONNXModelHandler(model_url='model.onnx')
session_input = onnx.load('model.onnx').graph # graph has no SerializeToString
// after
model_proto = onnx.load('model.onnx') # ModelProto: has SerializeToString Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(model, bytes) and not (hasattr(model, 'SerializeToString') and callable(getattr(model, 'SerializeToString'))):
raise TypeError('Pass bytes or an onnx.ModelProto to ONNXModelHandler') Type guard
def is_protobuf_like(obj) -> bool:
return hasattr(obj, 'SerializeToString') and callable(obj.SerializeToString) Try / catch
try:
handler.load_model()
except TypeError as e:
if 'SerializeToString' in str(e):
model_bytes = open(model_path, 'rb').read()
# retry with bytes input
else:
raise Prevention
- Load models with onnx.load() (returns ModelProto) rather than passing paths or graphs
- Never pass a string path where bytes/proto is expected
- Verify the artifact is ONNX, not TorchScript or TF SavedModel
When it happens
Trigger: onnx.load() returning a ModelProto is fine, but passing e.g. a session, a numpy object, or a str path into the handler's model slot leads to load_model hitting the else branch.
Common situations: Passing a file path string instead of loaded bytes; using tf/savedmodel exports without protobuf conversion; custom model wrappers lacking SerializeToString.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- A cluster_identifier should be Optional[Union[str…
- Attempted to encode null for non-nullable field
- Cannot convert to a JSON value.
- Cannot get a type descriptor for
- Cannot interpret as Duration.
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/bdadc19c598f58e7.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/python/apache_beam/ml/inference/onnx_inference.py:134
**kwargs)
self._model_uri = model_uri
self._session_options = session_options
self._providers = providers
self._provider_options = provider_options
self._model_inference_fn = inference_fn
def load_model(self) -> ort.InferenceSession:
"""Loads and initializes an onnx inference session for processing."""
# when path is remote, we should first load into memory then deserialize
f = FileSystems.open(self._model_uri, "rb")
model_proto = onnx.load(f)
model_proto_bytes = model_proto
if not isinstance(model_proto, bytes):
if (hasattr(model_proto, "SerializeToString") and
callable(model_proto.SerializeToString)):
model_proto_bytes = model_proto.SerializeToString()
else:
raise TypeError(
"No SerializeToString method is detected on loaded model. " +
f"Type of model: {type(model_proto)}")
ort_session = ort.InferenceSession(
model_proto_bytes,
sess_options=self._session_options,
providers=self._providers,
provider_options=self._provider_options)
return ort_session
def run_inference(
self,
batch: Sequence[numpy.ndarray],
inference_session: ort.InferenceSession,
inference_args: Optional[dict[str, Any]] = None
) -> Iterable[PredictionResult]:
"""Runs inferences on a batch of numpy arrays.
Args:View on GitHub (pinned to 12126d8942)