Serving Scikit-LLM Pipelines as REST APIs with FastAPI
In this article, you will learn how to serve a Scikit-LLM zero-shot text classification pipeline, powered by a local Ollama model, as a REST API...

Share Post Share In this article, you will learn how to serve a Scikit-LLM zero-shot text classification pipeline, powered by a local Ollama model, as a REST API with FastAPI.
It looks like Scikit-LLM is here to stay. This innovative Python framework pushes the boundaries of machine learning and LLMs by treating them as two sides of the same coin, enabling scikit-learn-based pipelines fueled by powerful pre-trained LLMs adapted to perform specific tasks – for instance, building a smart zero-shot classification engine. All of this sounds enthralling until you try to put it into production and instantly get chained to costly cloud APIs, latency spikes, and so on.
This article covers the first crucial steps toward deploying a deep semantic reasoning pipeline fueled by an LLM and combined with the reliability of standardized machine learning syntax – enter the.fit() and.predict() methods – as well as predictable outputs like those found in traditional machine learning pipelines.
We will put together an open-source stack composed of Scikit-LLM, FastAPI, and Ollama, teaching you how to wire up a local, self-hosted LLM that acts as a deterministic text labeler: all wrapped in an asynchronous, blazing-fast REST API, thereby bypassing cloud costs and commercial lock-in.
For this practical demonstration, I have used Visual Studio Code with a virtual environment using Python 3.11. I have also installed some basic libraries like pandas and scikit-learn in the virtual environment. In addition, you’ll need to install the following specialized libraries to make the code work:
You’ll also need to install Ollama and pull (locally download) llama3. This article explains the process to install Ollama in more detail, but chances are you won’t need it if you just visit the Ollama website: it’s pretty simple to install, I promise!
Once everything is correctly installed and you have resolved any possible issues caused by version compatibility with your Python interpreter, let’s move on to the core of the process. We’ll create a new project folder in our IDE and create two Python files inside it, which will contain the code: name them train.py and app.py.
Let’s start with train.py. In this Python file, we will add the logic to train and serialize the Scikit-LLM pipeline. To keep things simple but scalable, we will just invoke the Ollama model and attach it to a one-stage pipeline consisting of a zero-shot classifier – in simple terms, a pre-trained LLM turned into a classifier by giving it the set of possible categories, or labels, from an example dataset of texts, without actual model training.
After that, we serialize the pipeline: once you run the code below, a file named scikit_llm_model.joblib containing the serialized pipeline will be created in your project folder. Here’s the code to do this:
The next step is to add the code for the app.py file. Run this code after generating the serialized pipeline. This file is needed to:
Once you run this code in your IDE, if everything goes smoothly, you should see the following messages in the output console:
Now your pipeline is accessible (well, locally accessible, I mean), and you can open the FastAPI interface by browsing to http://127.0.0.1:8000/docs. To test the app, click the “ Try it out ” button inside the expandable “ /predict ” block, and edit the “Request body” text box to paste the following input examples or any of your own:
Then click “Execute”, and your pipeline’s LLM will start doing its magic. In a few seconds, the predictions will pop up:
Congratulations if you made it here! Just be aware of a few notes about what we have just done together: