Inference Schema

1.8 · active · verified Thu Apr 16

The `inference-schema` package provides a uniform schema definition for common machine learning applications, specifically designed to aid in web-based ML prediction services. It offers decorators (`@input_schema`, `@output_schema`) that automatically validate and serialize input/output data based on user-defined schemas, integrating well with web frameworks. The current version is 1.8, and it sees periodic updates, often tied to dependency version bumps or feature additions for ML deployments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define input and output schemas for a Python function using `inference-schema` decorators. It uses `PandasParameterType` for structured DataFrame input and a simple dictionary for output. The decorators validate the incoming `input_data` against `sample_input_df` and ensure the function's return value conforms to `sample_output_dict`'s structure.

from inference_schema.schema_decorators import input_schema, output_schema
from inference_schema.parameter_types import PandasParameterType
import pandas as pd
import json

# Define sample input and output data structures
# These samples are used to infer the schema for validation and serialization
sample_input_df = pd.DataFrame({'feature1': [10.0, 20.0], 'feature2': [30.0, 40.0]})
sample_output_dict = {'prediction': [40.0, 60.0]}

@input_schema(PandasParameterType(sample_input_df))
@output_schema(sample_output_dict)
def predict(input_data: pd.DataFrame) -> dict:
    """
    A dummy prediction function that takes a DataFrame and returns a dictionary.
    The decorators handle validation of `input_data` and serialization of the return value.
    """
    # Example prediction logic: sum of features
    predictions = (input_data['feature1'] + input_data['feature2']).tolist()
    return {'prediction': predictions}

# --- Example Usage --- 
# This is how you'd typically call it, with input that matches the schema
input_for_prediction = pd.DataFrame({'feature1': [5.0, 15.0], 'feature2': [25.0, 35.0]})
result = predict(input_for_prediction)
print(f"Predicted result: {result}")

# If used in a web service, the input might come as JSON and be deserialized
# and validated into a DataFrame before reaching `predict` function.
# Example: raw_json_input = '{"feature1": [5.0, 15.0], "feature2": [25.0, 35.0]}' 
# (framework would parse, inference-schema would validate/convert)

view raw JSON →