run-llama/llama_index · error · ValueError

Optimizer returned zero sentences.

Error message

Optimizer returned zero sentences.

What it means

Error "Optimizer returned zero sentences." thrown in run-llama/llama_index.

Source

Thrown at llama-index-core/llama_index/core/postprocessor/optimizer.py:138

            num_top_k = None
            threshold = None
            if self.percentile_cutoff is not None:
                num_top_k = int(len(split_text) * self.percentile_cutoff)
            if self.threshold_cutoff is not None:
                threshold = self.threshold_cutoff

            top_similarities, top_idxs = get_top_k_embeddings(
                query_embedding=query_bundle.embedding,
                embeddings=text_embeddings,
                similarity_fn=self._embed_model.similarity,
                similarity_top_k=num_top_k,
                embedding_ids=list(range(len(text_embeddings))),
                similarity_cutoff=threshold,
            )

            if len(top_idxs) == 0:
                raise ValueError("Optimizer returned zero sentences.")

            rangeMin, rangeMax = 0, len(split_text)

            if self.context_before is None:
                self.context_before = 1
            if self.context_after is None:
                self.context_after = 1

            top_sentences = [
                " ".join(
                    split_text[
                        max(idx - self.context_before, rangeMin) : min(
                            idx + self.context_after + 1, rangeMax
                        )
                    ]
                )
                for idx in top_idxs
            ]

View on GitHub (pinned to afd0fef371)

Solutions

  1. Lower the optimizer's cutoff/percentile threshold so some sentences survive.
  2. Check that the input nodes contain text and the embedding model works.

When it happens

Trigger: Thrown at llama-index-core/llama_index/core/postprocessor/optimizer.py:138 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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