Metadata Schema and Validation

[1]:
import yaml

from seldon_core.metadata import validate_model_metadata

Examples Description

Simple and transparent list of valid model metadata definitions

kfserving Schema

[2]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- datatype: BYTES
  name: input
  shape: [ 1, 4 ]
outputs:
- datatype: BYTES
  name: output
  shape: [ 3 ]
"""

validate_model_metadata(yaml.safe_load(data))
[2]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'datatype': 'BYTES', 'name': 'input', 'shape': [1, 4]}],
 'outputs': [{'datatype': 'BYTES', 'name': 'output', 'shape': [3]}]}

Seldon Schema

array

payload examples: - {"data": {"names": ["a", "b"], "ndarray": [[1, 2], [3, 4]]}} - {"data": {"names": ["a", "b"], "tensor": {"values": [1, 2, 3, 4], "shape": [2, 2]}}

[3]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- messagetype: tensor
  schema:
    names: [a, b, c, d]
    shape: [ 4 ]
outputs:
- messagetype: tensor
  schema:
    shape: [ 1 ]
"""

validate_model_metadata(yaml.safe_load(data))
[3]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'messagetype': 'tensor',
   'schema': {'names': ['a', 'b', 'c', 'd'], 'shape': [4]}}],
 'outputs': [{'messagetype': 'tensor', 'schema': {'shape': [1]}}]}

json data

payload example: {"jsonData": {"my-custom-field": "my-custom-data"}}

[4]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- messagetype: jsonData
outputs:
- messagetype: tensor
  schema:
    shape: [ 1 ]
"""

validate_model_metadata(yaml.safe_load(data))
[4]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'messagetype': 'jsonData'}],
 'outputs': [{'messagetype': 'tensor', 'schema': {'shape': [1]}}]}

json data with user’s schema

payload example: {"jsonData": {"names": ["a", "b"], "data": [1, 2]}}

[5]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- messagetype: jsonData
  schema:
      type: object
      properties:
          names:
              type: array
              items:
                  type: string
          data:
            type: array
            items:
                type: number
                format: double
outputs:
- messagetype: tensor
  schema:
    shape: [ 1 ]
"""

validate_model_metadata(yaml.safe_load(data))
[5]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'messagetype': 'jsonData',
   'schema': {'type': 'object',
    'properties': {'names': {'type': 'array', 'items': {'type': 'string'}},
     'data': {'type': 'array',
      'items': {'type': 'number', 'format': 'double'}}}}}],
 'outputs': [{'messagetype': 'tensor', 'schema': {'shape': [1]}}]}

str data

payload example: {"strData": "some test input"}

[6]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- messagetype: strData
outputs:
- messagetype: tensor
  schema:
    shape: [ 1 ]
"""

validate_model_metadata(yaml.safe_load(data))
[6]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'messagetype': 'strData'}],
 'outputs': [{'messagetype': 'tensor', 'schema': {'shape': [1]}}]}

custom

[7]:
data = """
name: my-model-name
versions: [ my-model-version-01 ]
platform: seldon
inputs:
- messagetype: customData
  schema:
    my-names: ["a", "b", "c"]
outputs:
- messagetype: tensor
  schema:
    shape: [ 1 ]
"""

validate_model_metadata(yaml.safe_load(data))
[7]:
{'name': 'my-model-name',
 'versions': ['my-model-version-01'],
 'platform': 'seldon',
 'inputs': [{'messagetype': 'customData',
   'schema': {'my-names': ['a', 'b', 'c']}}],
 'outputs': [{'messagetype': 'tensor', 'schema': {'shape': [1]}}]}