AI Agents · Guide
Package and Serve a Machine Learning Model
Package the complete prediction behavior, validate a JSON artifact and run a localhost inference endpoint with explicit input and failure contracts.
Metarix Editorial · Nov 8, 2026 · 4 min read
Documentation-based research
On this page
Training produces an estimator. A usable release also needs preprocessing, a feature contract, a chosen threshold, environment information and a way to identify the exact artifact. Serving adds another interface: how requests arrive, how invalid inputs are handled and how failures become visible.
This lesson in the Machine Learning Engineer learning path packages the synthetic classifier and runs a local HTTP exercise. It does not deploy a hardened public service.
Release the entire prediction behavior
The example’s prediction depends on feature order, training medians, scaling means and scales, classifier weights, intercept and threshold. Saving weights alone would omit behavior. The artifact schema names those fields and the serving code checks that the dimensions and values are valid before use.
The project exports a small JSON representation for this specific numeric logistic pipeline. A unit test compares its output with the fitted scikit-learn pipeline. It is not a general exporter for arbitrary estimators or preprocessing graphs.
For other projects, choose persistence according to compatibility, trust and operational needs. Scikit-learn’s documentation explains options and their limitations. Pickle-based formats, including many joblib artifacts, can execute code when loaded and must come from a trusted source. Version compatibility must be planned; a filename ending in model does not establish a portable interface.
JSON avoids that particular object-deserialization mechanism, but an attacker can still change coefficients or thresholds. The artifact must come from a controlled release process. A hash identifies bytes and detects changes only when you trust the expected hash; storing a mutable hash beside a mutable model is not proof of authenticity.
Define the request and response contract
The local endpoint accepts all four named features. A measurement may be null and use the training median; an entirely missing measurement set is rejected. Unknown fields, booleans, non-finite values and values outside the example’s ranges are rejected before prediction.
The response includes probability, threshold, predicted_class and an explicit educational warning. A consumer should not assume that a probability is a calibrated safety estimate or that a positive class automatically authorizes an action.
Keep the contract versioned. Changing a unit from degrees Celsius to Fahrenheit without changing the interface can be more damaging than an obvious server failure. Test producer and consumer assumptions together.
Exercise: run the local inference endpoint
Use the downloadable project, follow its README setup, and train once:
python ml_capstone.py train --out artifacts
python ml_capstone.py serve --model artifacts/model.json --port 8765
In another terminal, send a request:
curl -sS http://127.0.0.1:8765/predict -H 'Content-Type: application/json' -d '{"temperature_c":75,"vibration_mm_s":5,"load_pct":null,"pressure_kpa":130}'
Expect a JSON response with a probability between zero and one, threshold 0.3 for the shipped fixture, and predicted_class 0 or 1. Then send an empty object and confirm that the server returns HTTP 400. The test suite also checks unsupported content types, unknown paths and oversized payloads.
The server binds to 127.0.0.1 and uses Python’s standard-library HTTPServer. It is deliberately a local teaching interface. The Python documentation warns that http.server is not recommended for production. Do not expose this example to the internet.
Turn the exercise into a deployment design
A real service needs authentication and authorization, TLS termination, bounded request lifecycles, concurrency, resource limits, dependency updates, process supervision and observability. It also needs an owner for model releases, a rollback procedure and a clear policy for unavailable or invalid predictions.
Choose batch or online inference from the action’s timing requirements. If daily scoring is sufficient, a batch job may be easier to operate than a continuously available API. If online responses are necessary, measure end-to-end latency and capacity under representative loads before selecting infrastructure.
Learn one deployment route well before collecting three cloud platforms and Kubernetes as prerequisites. Metarix’s guide to deploying AI agents on AWS discusses related infrastructure tradeoffs, but its agent examples do not validate a deployment for this model.
You have completed this lesson when you can reproduce the artifact, make valid and invalid local requests, and explain what would have to change before a public deployment. The next lesson addresses what happens after a model release starts operating.
Sources and method
The JSON export, CLI prediction and localhost endpoint were tested on Linux with Python 3.12.14 and scikit-learn 1.8.0. No public hosting, load test or production security assessment was performed.