Skip to content

Shape inconsistency in Tagger.predict #14048

Open

Description

@ryanheise

How to reproduce the behaviour

Technically this is not behavioural, but the code looks wrong. In spacy/pipeline/tagger.pyx:

    def predict(self, docs):
        """Apply the pipeline's model to a batch of docs, without modifying them.

        docs (Iterable[Doc]): The documents to predict.
        RETURNS: The models prediction for each document.

        DOCS: https://spacy.io/api/tagger#predict
        """
        if not any(len(doc) for doc in docs):
            # Handle cases where there are no tokens in any docs.
            n_labels = len(self.labels)
            guesses = [self.model.ops.alloc((0, n_labels)) for doc in docs]
            assert len(guesses) == len(docs)
            return guesses
        scores = self.model.predict(docs)
        assert len(scores) == len(docs), (len(scores), len(docs))
        guesses = self._scores2guesses(scores)
        assert len(guesses) == len(docs)
        return guesses

    def _scores2guesses(self, scores):
        guesses = []
        for doc_scores in scores:
            doc_guesses = doc_scores.argmax(axis=1)
            if not isinstance(doc_guesses, numpy.ndarray):
                doc_guesses = doc_guesses.get()
            guesses.append(doc_guesses)
        return guesses

In the normal case, _scores2guesses produces the result which looks like a list of 1D arrays. But if all of the docs are empty, it uses [alloc((0, n_labels)) for doc in docs] which produces a list of 2D arrays. This looks like a shape inconsistency in the API contract. In the end, I don't think it affects the behaviour, but it looks wrong.

Your Environment

N/A

Activity

  1. Amruthareddygurugari20 commented on Oct 17, 2026

    @Amruthareddygurugari20

    Hi, I'd like to work on this. In the empty-docs branch of Tagger.predict, I'd return 1D integer arrays (alloc((0,), dtype="i")) so they match what _scores2guesses returns in the normal case. I'd also add a test that checks the shape for both empty and non-empty docs. Does that sound right? Can I take it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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