run-llama/llama_index · error · ImportError

Please install scikit-learn to use this feature.

Error message

Please install scikit-learn to use this feature.

What it means

get_top_similar_embeddings_by_query (used when VectorStoreQueryMode is SVM / LINEAR_REGRESSION / LOGISTIC_REGRESSION) needs scikit-learn to fit the classifier. The ImportError fires when sklearn is not installed in the environment.

Source

Thrown at llama-index-core/llama_index/core/indices/query/embedding_utils.py:63

    query_embedding: List[float],
    embeddings: List[List[float]],
    similarity_top_k: Optional[int] = None,
    embedding_ids: Optional[List] = None,
    query_mode: VectorStoreQueryMode = VectorStoreQueryMode.SVM,
) -> Tuple[List[float], List]:
    """
    Get top embeddings by fitting a learner against query.

    Inspired by Karpathy's SVM demo:
    https://github.com/karpathy/randomfun/blob/master/knn_vs_svm.ipynb

    Can fit SVM, linear regression, and more.

    """
    try:
        from sklearn import linear_model, svm
    except ImportError:
        raise ImportError("Please install scikit-learn to use this feature.")

    if embedding_ids is None:
        embedding_ids = list(range(len(embeddings)))
    query_embedding_np = np.array(query_embedding)
    embeddings_np = np.array(embeddings)
    # create dataset
    dataset_len = len(embeddings) + 1
    dataset = np.concatenate([query_embedding_np[None, ...], embeddings_np])
    y = np.zeros(dataset_len)
    y[0] = 1

    if query_mode == VectorStoreQueryMode.SVM:
        # train our SVM
        # TODO: make params configurable
        clf = svm.LinearSVC(
            class_weight="balanced", verbose=False, max_iter=10000, tol=1e-6, C=0.1
        )
    elif query_mode == VectorStoreQueryMode.LINEAR_REGRESSION:

View on GitHub (pinned to afd0fef371)

Solutions

  1. pip install scikit-learn (or add scikit-learn to requirements.txt/pyproject)
  2. Or switch query_mode to VectorStoreQueryMode.DEFAULT, which uses cosine similarity and needs no sklearn
  3. Pin a compatible numpy version if installing sklearn breaks the existing numpy pin

Example fix

# before
retriever = index.as_retriever(vector_store_query_mode=VectorStoreQueryMode.SVM)

# after
pip install scikit-learn
# (code unchanged)
Defensive patterns

Strategy: try-catch

Validate before calling

try:
    import sklearn  # noqa: F401
    HAS_SKLEARN = True
except ImportError:
    HAS_SKLEARN = False

mode = VectorStoreQueryMode.SVM if HAS_SKLEARN else VectorStoreQueryMode.DEFAULT

Type guard

def can_use_svm() -> bool:
    try:
        import sklearn  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    results = retriever.retrieve(query_str)
except ImportError as e:
    if 'scikit-learn' in str(e):
        retriever = index.as_retriever()  # DEFAULT mode, no sklearn
        results = retriever.retrieve(query_str)
    else:
        raise

Prevention

When it happens

Trigger: Constructing a vector retriever or query engine with query_mode=VectorStoreQueryMode.SVM (or the regression modes) — llama-index-core does not ship sklearn as a dependency, so the import at call time fails.

Common situations: Following RAG-fusion / SVM-retrieval examples on a minimal pip install llama-index; Docker images or CI that prune 'extra' dependencies; adding SVM mode to an existing deployment without updating requirements.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/98180e49b4ad7772. Report an issue: GitHub.