{"slug": "using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm", "title": "Using LLM Summarization in Your Scikit-Learn Pipelines with Scikit-LLM", "summary": "A tutorial demonstrates combining Scikit-LLM summarization estimators with traditional scikit-learn methods in a single pipeline that summarizes and classifies raw text, configured to run on Groq's free-tier OpenAI-compatible API endpoint at https://api.groq.com/openai/v1 via a GROQ_API_KEY secret in Google Colab. The walkthrough installs the scikit-llm package and uses three sample documents labeled Energy, Finance and Healthcare to show LLM summarization as a preprocessing step before downstream classification.", "body_md": "# Using LLM Summarization in Your Scikit-Learn Pipelines with Scikit-LLM\n\nLearn how to build an architecture that blends Scikit-LLM estimators for text summarization with traditional scikit-learn methods into a single, unified pipeline for summarizing and classifying raw, messy text.\n\n## Introduction\n\nUsing traditional, lightweight machine learning classifiers on text frequently requires condensing lengthy, often noisy raw text into clean signals to succeed. Therefore, it wouldn't be crazy to think of using a **large language model** (LLM) for **text summarization** as a preprocessing stage before feeding cleaner texts to the downstream classification model. What if we could do this seamlessly as part of a traditional, scikit-learn-based pipeline where LLM-driven summarization can coexist with traditional data preprocessing, training, and inference? And what if we could do all of this using exclusively free tools? Look no further.\n\nIn this article, we will learn how to build an architecture that blends **[Scikit-LLM](https://github.com/BeastByteAI/scikit-llm)** estimators for text summarization with traditional **[scikit-learn](https://scikit-learn.org/)** methods into a single, unified pipeline for summarizing and classifying raw, messy text, bringing together the best of both worlds: LLMs and classical machine learning models.\n\n## Integrating LLM-Driven Summarization in a Machine Learning Pipeline\n\nThis example can be run entirely in a **[Google Colab](https://colab.research.google.com/)** notebook. It uses **[Groq](https://groq.com/)**'s LLM API to fully rely on free models. A prerequisite is to access the [Groq website](https://console.groq.com/keys), register, and generate your own API key. Once it's generated, carefully copy your API key (it is shown only once) and paste it as a new \"Secret\" variable in Google Colab, found at the key icon on the left-hand side of Colab. Name the secret variable `GROQ_API_KEY`.\n\nAfter that, we start our notebook by installing Scikit-LLM:\n\n```\n!pip install scikit-llm -q\n```\n\nNow, into the Python code. The first step is to **configure the environment** surrounding Scikit-LLM, routing our requests through Groq's API instead of the default OpenAI API. This will help us leverage Groq's free, friendly, and efficient pre-trained models, bypassing per-token costs.\n\n``` python\nfrom google.colab import userdata\nfrom skllm.config import SKLLMConfig\n\n# Retrieving API key and routing to Groq's free-tier OpenAI-compatible endpoint\ngroq_key = userdata.get('GROQ_API_KEY')\nSKLLMConfig.set_gpt_url(\"https://api.groq.com/openai/v1\")\nSKLLMConfig.set_openai_key(groq_key)\n```\n\nNext, we **define the \"training\" data**. It consists of a small set of initial documents along with their corresponding labels. The documents are intentionally lengthy, somewhat noisy, and verbose, just like in many real-world scenarios. Without a prior summarization to extract relevant signals from the text, directly obtaining numerical, machine learning-friendly representations from these texts would inevitably propagate the noise:\n\n```\n# Raw, lengthy documents and their corresponding class labels\nX_long_texts = [\n    \"The new solar panel technology efficiency has increased by 15% due to the novel perovskite layer, significantly lowering the barrier to entry for residential use and driving green energy adoption by making installations cheaper and faster than ever before.\",\n    \"Financial markets saw a sharp downturn today following the central bank's unexpected interest rate hike, leading to massive sell-offs in the tech sector and triggering volatility indices to reach their highest points in over three years.\",\n    \"The clinical trial for the new arthritis medication showed promising results, reducing joint swelling by 40% in the control group without severe side effects, awaiting FDA approval which could bring relief to millions suffering from autoimmune disorders.\"\n]\n\ny_labels = [\"Energy\", \"Finance\", \"Healthcare\"]\n```\n\nSimilar to few-shot classification, we only need a small, representative set of training examples to let the LLM we will load later know how to reason. No actual training of the LLM takes place.\n\nThe next step is arguably the most critical: we are **defining our three-step pipeline for machine learning-driven classification preceded by LLM-driven summarization**, thereby integrating Scikit-LLM capabilities to accommodate specific tasks on pre-trained LLMs into a broader machine learning pipeline. The code below uses a `GPTSummarizer` object to encapsulate a compatible Groq model for summarization, namely `openai/gpt-oss-20b`. It then embeds the summarization step into the scikit-learn pipeline, followed by term frequency-inverse document frequency (TF-IDF) to obtain text representations of summarized texts rather than the original, lengthy ones. Finally, it defines and trains a logistic regression classifier using the label set defined before.\n\n``` python\nfrom sklearn.pipeline import Pipeline\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nfrom skllm.models.gpt.text2text.summarization import GPTSummarizer\n\n# THREE-STEP MACHINE LEARNING PIPELINE FOR TEXT CLASSIFICATION, PRECEDED BY SUMMARIZATION\n# 1. Summarizer: Condenses long documents using the compatible model on Groq\n# 2. Vectorizer: Converts the concise summaries into numerical TF-IDF vectors\n# 3. Classifier: Learns to classify the TF-IDF vectors using standard Logistic Regression\ntext_pipeline = Pipeline([\n    ('summarizer', GPTSummarizer(\n        model=\"custom_url::openai/gpt-oss-20b\",\n        max_words=20\n    )),\n    ('vectorizer', TfidfVectorizer()),\n    ('classifier', LogisticRegression())\n])\n\n# Fitting the entire pipeline end-to-end\ntext_pipeline.fit(X_long_texts, y_labels)\n```\n\nYou might be wondering why just three training instances in `X_long_texts` could be enough to train the last-stage logistic regression classifier. It's not because the classifier has truly learned the broad concepts of 'Energy', 'Finance', and 'Healthcare'. Instead, it succeeds due to two pipeline mechanics working in tandem: the LLM first acts as a precise filter that condenses noisy text into \"canonical\" keywords. After that, the downstream vectorizer naturally limits its whole vocabulary to those few remaining words, turning the classification task into a pretty easy, largely trivial keyword-matching game.\n\nNext, we take **a look at the summaries produced** for the three training examples to verify they make sense:\n\n```\n# Inspecting the summaries of \"training\" examples, accessing the 'summarizer' object\ntrain_summaries = text_pipeline.named_steps['summarizer'].transform(X_long_texts)\n\nprint(\"--- TRAINING DATA SUMMARIES ---\")\nfor label, original, summary in zip(y_labels, X_long_texts, train_summaries):\n    print(f\"[{label.upper()}]\\nOriginal: {original}\\nSummary:  {summary}\\n\")\n```\n\nOutput:\n\n```\n--- TRAINING DATA SUMMARIES ---\n[ENERGY]\nOriginal: The new solar panel technology efficiency has increased by 15% due to the novel perovskite layer, significantly lowering the barrier to entry for residential use and driving green energy adoption...\nSummary:  New perovskite solar panels boost efficiency by 15%, reducing costs and accelerating residential green energy adoption.\n\n[FINANCE]\nOriginal: Financial markets saw a sharp downturn today following the central bank's unexpected interest rate hike, leading to massive sell-offs in the tech sector and triggering volatility indices...\nSummary:  Markets plunged after unexpected rate hike, causing tech sell‑offs and high volatility.\n\n[HEALTHCARE]\nOriginal: The clinical trial for the new arthritis medication showed promising results, reducing joint swelling by 40% in the control group without severe side effects, awaiting FDA approval...\nSummary:  New arthritis drug trial reduces swelling by 40% with no severe side effects, pending FDA approval.\n```\n\nSince these summaries seem quite sensible, we can now finalize by creating some **test examples** and passing them entirely to the pipeline **for classification via prior summarization**:\n\n```\nX_new = [\n    \"A breakthrough mRNA vaccine targeting seasonal flu strains showed 85% efficacy across diverse patient demographics in stage 3 clinical trials, paving the way for a more robust global immune defense next winter.\",\n    \"Researchers have developed a grid-scale sodium-ion battery that promises to store surplus wind and solar power at a fraction of lithium's cost, potentially resolving the intermittency issue of renewables.\",\n    \"Stocks rallied in late trading as technology shares recovered from yesterday's losses, driven by strong earnings reports from major semiconductor manufacturers and optimistic GDP growth forecasts.\"\n]\n\n# The pipeline automatically summarizes, vectorizes, and classifies the unseen text\npredictions = text_pipeline.predict(X_new)\n\n# Access the intermediate summaries for the unseen data to display them\nnew_summaries = text_pipeline.named_steps['summarizer'].transform(X_new)\n\nprint(\"\\n--- UNSEEN DATA PREDICTIONS ---\")\nfor text, summary, prediction in zip(X_new, new_summaries, predictions):\n    print(f\"Original Document: '{text}'\")\n    print(f\"Intermediate Summary: '{summary}'\")\n    print(f\"Final Predicted Category: {prediction}\\n\" + \"-\" * 70)\n```\n\nAnd these are the final results we obtain — not bad at all!\n\n```\n--- UNSEEN DATA PREDICTIONS ---\nOriginal Document: 'A breakthrough mRNA vaccine targeting seasonal flu strains showed 85% efficacy across diverse patient demographics in stage 3 clinical trials, paving the way for a more robust global immune defense next winter.'\nIntermediate Summary: 'New mRNA flu vaccine achieved 85% efficacy in diverse patients, promising stronger global immunity next winter.'\nFinal Predicted Category: Healthcare\n----------------------------------------------------------------------\nOriginal Document: 'Researchers have developed a grid-scale sodium-ion battery that promises to store surplus wind and solar power at a fraction of lithium's cost, potentially resolving the intermittency issue of renewables.'\nIntermediate Summary: 'New grid‑scale sodium‑ion battery can store excess wind/solar power cheaply, easing renewable intermittency.'\nFinal Predicted Category: Energy\n----------------------------------------------------------------------\nOriginal Document: 'Stocks rallied in late trading as technology shares recovered from yesterday's losses, driven by strong earnings reports from major semiconductor manufacturers and optimistic GDP growth forecasts.'\nIntermediate Summary: 'Tech stocks rebound late, fueled by strong semiconductor earnings and hopeful GDP growth, boosting overall market rally.'\nFinal Predicted Category: Finance\n----------------------------------------------------------------------\n```\n\n## Wrapping Up\n\nWith the aid of Scikit-LLM, we have learned how to embed a free, pre-trained LLM for summarization directly into a native machine learning pipeline based on scikit-learn. In particular, we combined modern prompt-based text distillation with classical, lightweight TF-IDF vectorizers and linear models like logistic regression for classification, making the overall task lightweight and surprisingly effective.\n\n \n\n \n\n[**\\[Iván Palomares Carrascosa\\](https://www.linkedin.com/in/ivanpc/)**](https://www.linkedin.com/in/ivanpc/) is a leader, writer, speaker, and adviser in AI, machine learning, deep learning & LLMs. He trains and guides others in harnessing AI in the real world.", "url": "https://wpnews.pro/news/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm", "canonical_source": "https://www.kdnuggets.com/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm", "published_at": "2026-11-03 15:00:00+00:00", "updated_at": "2026-11-03 15:28:06.556260+00:00", "lang": "en", "topics": ["ai-tools", "large-language-models", "developer-tools", "machine-learning"], "entities": ["Scikit-LLM", "scikit-learn", "Groq", "Google Colab", "OpenAI"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm", "markdown": "https://wpnews.pro/news/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm.md", "text": "https://wpnews.pro/news/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm.txt", "jsonld": "https://wpnews.pro/news/using-llm-summarization-in-your-scikit-learn-pipelines-with-scikit-llm.jsonld"}}