Serve a model with an explicit prediction schema
In this article (5 sections)
A model service needs a contract for inputs and outputs. Column names alone do not define acceptable ranges, category values, required identifiers or behavior for unexpected fields. Validate before preprocessing so malformed requests receive stable errors instead of unpredictable predictions.
A strict request model
The deployment lab defines five required fields for an original synthetic subscription-risk model: customer ID, tenure months, monthly spend, plan and support-ticket count. Unknown keys are forbidden.
from deployment_cases import schema_case
result = schema_case()
assert result["invalid_requests"] == 3
assert result["rejected_requests"] == 3
print(result["json_schema_required"])The validator accepts a well-formed plus plan request. It rejects negative spend, the unknown plan gold, and an unexpected field. The error types are greater_than_equal, literal_error and extra_forbidden.
Define semantics as well as types
An integer tenure needs a unit and reference date. Monthly spend needs currency, tax and refund treatment. Support tickets need a time window. Put these definitions in an interface document and feature dictionary; JSON Schema cannot express every business meaning.
Choose behavior for missing and unseen values deliberately. Reject a required feature, apply a trained missing-value policy, or route to review. Do not silently convert an unknown category into the most common category.
Return traceable outputs
The response should contain the request or entity identifier, model version, score and decision. Keep thresholds versioned. Avoid exposing internal stack traces or sensitive features. Attach a request ID to errors and logs so operators can trace a failure without logging unrestricted raw data.
Generate schema examples and contract tests from the same source where possible. Test boundary values, wrong types, extra fields, empty batches and oversized payloads.
The Data Science course connects model features to an explicit serving boundary and operating evidence.
Exercise
Add currency and request-timestamp fields. Define timezone and allowed lateness, generate JSON Schema, and write acceptance/rejection tests without calling the model on invalid inputs.
Continue learning
This article is part of the Model deployment and MLOps sequence. Use the neighbouring tasks when you need the prerequisite or the next application.
- Continue with Test batch and single-record prediction consistency.
- Then apply it in Package preprocessing with the trained model.
Reference: Pydantic model documentation.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science