Define a Service and Its API
Expose runners through a typed service endpoint.
Define a Service and Its API is a free MLOps Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What a Service Is
In BentoML a Service is the unit you deploy. It wraps your model and exposes one or more API endpoints clients can call. 🛎️
Runners Run the Model
A runner is BentoML's optimized wrapper around a saved model. It handles inference in its own worker so the API stays responsive.
runner = bentoml.sklearn.get("iris_clf:latest").to_runner()Create the Service
You build a service by naming it and passing its runners. The Service object is what BentoML serves and scales.
svc = bentoml.Service("iris_classifier", runners=[runner])Add an Endpoint
You expose a function as an API with the @svc.api decorator. That function becomes a callable HTTP route.
@svc.api(input=..., output=...)
def classify(data):
...Declare Input and Output
You set typed IO descriptors so BentoML can validate and serialize. NumpyNdarray tells it to expect and return arrays.
from bentoml.io import NumpyNdarrayCall the Runner Inside
Within the API function you invoke the model through the runner. Using run sends the input to the model worker and returns the result.
result = runner.predict.run(data)
return resultPut It in service.py
BentoML looks for your code in a file by convention. You place the service in service.py at your project root.
Serve It Locally
One command starts a dev server. bentoml serve points at the file and the svc object, then watches for changes.
bentoml serve service:svc --reloadUse JSON for Dicts
Not every API speaks arrays. Swap the descriptor to JSON when your clients send and expect dictionaries instead.
from bentoml.io import JSONBuilt-In Swagger Docs
Once it runs, BentoML serves interactive Swagger docs at the root URL so you can test each endpoint in the browser. 🎉
Test with a curl Call
You can hit the live endpoint from your terminal. A quick curl POST sends sample input and shows the prediction it returns.
curl -X POST localhost:3000/classify -d "[[5.1,3.5,1.4,0.2]]"Quick Check
You want to call your model from inside an API function. What is the right wrapper to use?
Recap
You turned a runner into a Service, exposed an @svc.api endpoint with typed IO, dropped it in service.py, and served it with built-in docs. Live API done! 🙌
Frequently asked questions
Is the “Define a Service and Its API” lesson free?
Yes — the full text of “Define a Service and Its API” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.
What will I learn in “Define a Service and Its API”?
Expose runners through a typed service endpoint. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MLOps Academy?
No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Define a Service and Its API” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MLOps Academy lesson?
Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Save a Model to the Bento Store
- Define a Service and Its API
- Enable Adaptive Micro-Batching
- Build a Bento and Containerize It