langchain-ai/langchain · error · ValueError

search_type of {search_type} not allowed. Expected search_ty

Error message

search_type of {search_type} not allowed. Expected search_type to be 'similarity', 'similarity_score_threshold' or 'mmr'.

What it means

`VectorStore.search(query, search_type, ...)` dispatches on the `search_type` string; anything other than `'similarity'`, `'similarity_score_threshold'`, or `'mmr'` raises `ValueError`. The strictness exists because a typo would otherwise silently fall through to no search at all.

Source

Thrown at libs/core/langchain_core/vectorstores/base.py:324

        Raises:
            ValueError: If `search_type` is not one of `'similarity'`,
                `'mmr'`, or `'similarity_score_threshold'`.
        """
        if search_type == "similarity":
            return self.similarity_search(query, **kwargs)
        if search_type == "similarity_score_threshold":
            docs_and_similarities = self.similarity_search_with_relevance_scores(
                query, **kwargs
            )
            return [doc for doc, _ in docs_and_similarities]
        if search_type == "mmr":
            return self.max_marginal_relevance_search(query, **kwargs)
        msg = (
            f"search_type of {search_type} not allowed. Expected "
            "search_type to be 'similarity', 'similarity_score_threshold'"
            " or 'mmr'."
        )
        raise ValueError(msg)

    async def asearch(
        self, query: str, search_type: str, **kwargs: Any
    ) -> list[Document]:
        """Async return docs most similar to query using a specified search type.

        Args:
            query: Input text.
            search_type: Type of search to perform.

                Can be `'similarity'`, `'mmr'`, or `'similarity_score_threshold'`.
            **kwargs: Arguments to pass to the search method.

        Returns:
            List of `Document` objects most similar to the query.

        Raises:
            ValueError: If `search_type` is not one of `'similarity'`,

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Set `search_type` to exactly one of `'similarity'`, `'similarity_score_threshold'`, or `'mmr'`.
  2. Validate/normalize external input before passing it: lowercase it and check membership in the allowed set.
  3. For custom search modes, call the underlying method directly (e.g. `store.max_marginal_relevance_search(...)`) instead of the dispatcher.

Example fix

# before
docs = store.search(query, search_type="similarity_score")  # ValueError

# after
docs = store.search(query, search_type="similarity_score_threshold", score_threshold=0.5)
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = {"similarity", "similarity_score_threshold", "mmr"}

search_type = (search_type or "similarity").lower()
if search_type not in ALLOWED:
    raise ValueError(f"search_type must be one of {sorted(ALLOWED)}, got {search_type!r}")
docs = store.search(query, search_type, **search_kwargs)

Type guard

def is_valid_search_type(value: str) -> bool:
    """Type/narrowing guard for VectorStore.search dispatch values."""
    return isinstance(value, str) and value in {
        "similarity", "similarity_score_threshold", "mmr"
    }

Try / catch

try:
    docs = store.search(query, search_type)
except ValueError as e:
    if "search_type" in str(e):
        docs = store.search(query, "similarity")  # safe fallback
    else:
        raise

Prevention

When it happens

Trigger: Calling `store.search(q, search_type='similiarity')` (typo), `search_type='similarity_score'`, or a custom type the base class does not know; passing user-supplied config directly as `search_type`.

Common situations: Config files with misspelled search types; version drift where an integration once accepted an extra type; UI dropdowns that emit values not matching the three allowed strings; case sensitivity issues (`'MMR'`).

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/8ad23f9f67b9c5a9. Report an issue: GitHub.