validate_schema

verta.registry.validate_schema(f)

Decorator to validate prediction input and output against previously provided schema.

New in version 0.24.0.

Validation is done with the jsonschema library [1]. If no schema has been provided via RegisteredModelVersion.log_schema(), an exception is raised.

Note that an input schema is required but output is not. If the output schema was not set, then the output will not be validated.

Can safely be used with verify_io().

Examples

from pydantic import BaseModel
from verta.registry import validate_schema, VertaModelBase
from verta import Client
from verta.environment import Python

class Input(BaseModel):
    a: int
    b: int

class Output(BaseModel):
    c: int
    d: int

class MyModel(VertaModelBase):
    def __init__(self, artifacts=None):
        pass

    @validate_schema
    def predict(self, input):
        return {'c': 3, 'd': 4}

def main():
    client = Client()

    # register
    model_ver = client.get_or_create_registered_model("My Model").create_standard_model(
        MyModel,
        environment=Python([]),
    )
    model_ver.log_schema(input=Input.schema(), output=Output.schema())

    # deploy
    endpoint = client.get_or_create_endpoint("my-model")
    endpoint.update(model_ver, wait=True)
    deployed_model = endpoint.get_deployed_model()

    # succeeds; input matches previously provided schema
    input = Input(a=1, b=2)
    output = deployed_model.predict(input.dict())

    # fails; this input does not match the schema
    bad_input = {"something": "random"}
    deployed_model.predict(bad_input)

References