{"record":{"id":"2346d3493cc74fc6","repo":"tensorflow/models","slug":"invalid-model-instance-s-it-should-be-a-s","errorCode":null,"errorMessage":"Invalid model instance: %s, it should be a %s","messagePattern":"Invalid model instance: (.+?), it should be a (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/bert/serving.py","lineNumber":94,"sourceCode":"\n  @tf.function\n  def serve_examples(self, inputs):\n    features = tf.io.parse_example(inputs, self.name_to_features)\n    for key in list(features.keys()):\n      t = features[key]\n      if t.dtype == tf.int64:\n        t = tf.cast(t, tf.int32)\n      features[key] = t\n    return self.serve(\n        features[\"input_ids\"],\n        input_mask=features[\"input_mask\"] if \"input_mask\" in features else None,\n        segment_ids=features[\"segment_ids\"]\n        if \"segment_ids\" in features else None)\n\n  @classmethod\n  def export(cls, model, export_dir):\n    if not isinstance(model, cls):\n      raise ValueError(\"Invalid model instance: %s, it should be a %s\" %\n                       (model, cls))\n\n    signatures = {\n        \"serving_default\":\n            model.serve.get_concrete_function(\n                input_ids=tf.TensorSpec(\n                    shape=[None, None], dtype=tf.int32, name=\"inputs\")),\n    }\n    if model.name_to_features:\n      signatures[\n          \"serving_examples\"] = model.serve_examples.get_concrete_function(\n              tf.TensorSpec(shape=[None], dtype=tf.string, name=\"examples\"))\n    tf.saved_model.save(model, export_dir=export_dir, signatures=signatures)\n\n\ndef main(_):\n  sequence_length = FLAGS.sequence_length\n  if sequence_length is not None and sequence_length > 0:","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/bert/serving.py#L76-L112","documentation":"Type guard in the classmethod export of the serving wrapper: the object passed as model (first %s) is not an instance of the exporter class (second %s). The classmethod only serializes its own type — its serve/serve_examples tf.functions and feature spec — so foreign model objects are rejected.","triggerScenarios":"Thrown at official/legacy/bert/serving.py:94 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass an instance of the serving class itself, not the raw keras model or another wrapper","Construct the wrapper first (e.g. BertServing(model=...)) and pass that to export"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}