spacy-llm: Integrating LLMs into Structured NLP Pipelines with spaCy
This repository profile is provided by osrepos.com, an open source repository discovery platform.

Summary
spacy-llm seamlessly integrates Large Language Models (LLMs) into spaCy, offering a modular system for rapid prototyping and transforming unstructured LLM responses into robust outputs for various NLP tasks. It supports a wide range of LLMs, including OpenAI, Cohere, Anthropic, and open-source models, enabling users to combine the power of LLMs with spaCy's production-ready capabilities. This package allows for quick experimentation and the creation of efficient, reliable, and controlled NLP systems.
Repository Information
Topics
Click on any tag to explore related repositories
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Introduction
`spacy-llm` is a powerful Python package that integrates Large Language Models (LLMs) into spaCy, a leading library for advanced Natural Language Processing. This integration provides a modular system designed for fast prototyping and prompting, effectively turning unstructured LLM responses into robust outputs for a variety of NLP tasks, often without requiring training data.
The package features a serializable `llm` component for easy integration into your spaCy pipeline, along with modular functions to define specific tasks and models. It interfaces with major LLM APIs such as OpenAI, Cohere, Anthropic, Google PaLM, and Microsoft Azure AI. Additionally, `spacy-llm` supports a broad spectrum of open-source LLMs hosted on Hugging Face, including Falcon, Dolly, Llama 2, OpenLLaMA, StableLM, and Mistral. It also integrates with LangChain, allowing all LangChain models and features to be utilized within `spacy-llm`.
Out-of-the-box, `spacy-llm` provides tasks for Named Entity Recognition, Text Classification, Lemmatization, Relationship Extraction, Sentiment Analysis, Span Categorization, Summarization, Entity Linking, Translation, and raw prompt execution for maximum flexibility. Users can also implement their own custom functions for prompting, parsing, and model integrations via spaCy's registry. For handling prompts that exceed an LLM's context window, a map-reduce approach is available to split prompts and fuse the results.
Installation
To install `spacy-llm`, ensure you have `spacy` installed in your virtual environment, then run the following command:
python -m pip install spacy-llm
Examples
Here are a couple of quick examples to get started with `spacy-llm`.
In Python code
For quick experiments, you can use the following Python code to perform text classification with a GPT model from OpenAI:
import spacy
nlp = spacy.blank("en")
llm = nlp.add_pipe("llm_textcat")
llm.add_label("INSULT")
llm.add_label("COMPLIMENT")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}
This example uses the `llm_textcat` factory, which leverages the latest version of the built-in text classification task and the default GPT-3.5 model from OpenAI.
Using a config file
For more control over the various parameters of the `llm` pipeline, you can utilize spaCy's config system. Create a `config.cfg` file like the one below:
[nlp]
lang = "en"
pipeline = ["llm"]
[components]
[components.llm]
factory = "llm"
[components.llm.task]
@llm_tasks = "spacy.TextCat.v3"
labels = ["COMPLIMENT", "INSULT"]
[components.llm.model]
@llm_models = "spacy.GPT-4.v2"
Then, run the following Python code to load and use your configured pipeline:
from spacy_llm.util import assemble
nlp = assemble("config.cfg")
doc = nlp("You look gorgeous!")
print(doc.cats)
# {"COMPLIMENT": 1.0, "INSULT": 0.0}
This approach provides greater flexibility for customizing your LLM-powered NLP workflows.
Why Use spacy-llm?
Large Language Models offer powerful natural language understanding, making them excellent for quickly prototyping custom NLP tasks with few or no examples. However, for production systems, supervised learning models often provide better efficiency, reliability, control, and accuracy for well-defined tasks.
`spacy-llm` offers the best of both worlds. You can rapidly initialize pipelines with LLM-powered components for quick experimentation and then seamlessly integrate or replace them with spaCy's traditional supervised learning or rule-based components as your project matures. This allows you to leverage the prototyping speed of LLMs while maintaining the production-readiness, efficiency, and control that spaCy is known for. Even when an LLM is justified for complex tasks, `spacy-llm` enables you to combine it with other spaCy components, such as cheaper text classification models for filtering or rule-based systems for output validation, creating a robust and optimized NLP system.
Links
- GitHub Repository: explosion/spacy-llm
- spaCy Documentation: spacy.io
- PyPI Project Page: spacy-llm
Related repositories
Similar repositories that may be relevant next.

AuditNLG: Auditing Generative AI for Trustworthiness
June 25, 2026
AuditNLG is an open-source library from Salesforce designed to enhance the trustworthiness of generative AI language models. It provides state-of-the-art techniques to detect and improve factualness, safety, and constraint adherence in AI-generated text. This library simplifies the process of auditing AI outputs, offering explanations and alternative suggestions for problematic content.

Odysseus: A Comprehensive Self-Hosted AI Workspace for Productivity
June 25, 2026
Odysseus is a powerful self-hosted AI workspace designed to integrate various AI-powered tools into a single platform. It offers functionalities for chat, agents, deep research, document management, email, and calendar, supporting both local and API models. This comprehensive solution aims to enhance productivity and streamline AI workflows in a private environment.

Headroom: Drastically Reduce LLM Token Usage for AI Agents
June 25, 2026
Headroom is an innovative context compression layer for AI agents, designed to significantly reduce token usage for LLMs. It achieves 60-95% fewer tokens across various inputs like tool outputs, logs, files, and RAG chunks, all while preserving answer accuracy. This powerful tool enhances efficiency and cost-effectiveness for AI interactions.
PixelRAG: Pixel-Native Search for Visual Retrieval-Augmented Generation
June 22, 2026
PixelRAG revolutionizes search by enabling pixel-native retrieval, moving beyond traditional text parsing. It renders documents as screenshots, preserving visual context like tables and charts, which is crucial for accurate answers from reader models. This allows for searching any document based on its visual appearance, not just its textual content.
Source repository
Open the original repository on GitHub.