Skip to content

NameError: name 'nn' is not defined when importing sentence-transformers with latest transformers #43784

@Alan-Jowett

Description

@Alan-Jowett

System Info

transformers version: latest (installed via pip today, 2026-02-05)
sentence-transformers version: latest
torch version: 2.x (from pytorch/pytorch Docker image)
Python version: 3.11+
OS: Linux (Docker container)

Who can help?

@ArthurZucker @Rocketknight1

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder
  • My own task or dataset

Reproduction

Minimal reproduction:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("all-MiniLM-L6-v2")

Or directly:

from transformers import AutoModel

Results in:

NameError: name 'nn' is not defined

The error occurs during the import chain when transformers attempts to load a module that references nn.Module or similar torch.nn constructs without first importing torch.nn as nn.

Expected behavior

The import should succeed without errors. This worked with previous versions of transformers.

Additional context

This appears to be a similar regression to #38269 ("NameError: name 'Replicate' is not defined"), which was caused by a missing import in transformers/integrations/tensor_parallel.py.

The current error (nn not defined) suggests another module has a similar missing import issue, likely referencing nn.Module without the corresponding import torch.nn as nn statement.

Workaround: Pinning transformers<4.53.0 (or to the last known working version) resolves the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions