langchain-ai/langchain · error · ValueError

search_type of {search_type} not allowed. Valid values are:

Error message

search_type of {search_type} not allowed. Valid values are: {cls.allowed_search_types}

What it means

Pydantic field validator on `VectorStoreRetriever` (`as_retriever()` result): `search_type` must be in the class's `allowed_search_types` (default `{'similarity', 'similarity_score_threshold', 'mmr'}`), otherwise construction fails with `ValueError` listing the permitted values. It guards the retriever config at creation time rather than at query time.

Source

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

        """Validate search type.

        Args:
            values: Values to validate.

        Returns:
            Validated values.

        Raises:
            ValueError: If `search_type` is not one of the allowed search types.
            ValueError: If `score_threshold` is not specified with a float value(`0~1`)
        """
        search_type = values.get("search_type", "similarity")
        if search_type not in cls.allowed_search_types:
            msg = (
                f"search_type of {search_type} not allowed. Valid values are: "
                f"{cls.allowed_search_types}"
            )
            raise ValueError(msg)
        if search_type == "similarity_score_threshold":
            score_threshold = values.get("search_kwargs", {}).get("score_threshold")
            if (score_threshold is None) or (not isinstance(score_threshold, float)):
                msg = (
                    "`score_threshold` is not specified with a float value(0~1) "
                    "in `search_kwargs`."
                )
                raise ValueError(msg)
        return values

    def _get_ls_params(self, **kwargs: Any) -> LangSmithRetrieverParams:
        """Get standard params for tracing."""
        kwargs_ = self.search_kwargs | kwargs

        ls_params = super()._get_ls_params(**kwargs_)

        ls_params["ls_vector_store_provider"] = self.vectorstore.__class__.__name__

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pass a valid `search_type`: `'similarity'`, `'similarity_score_threshold'`, or `'mmr'` (or whatever the subclass's `allowed_search_types` contains).
  2. If you extended a subclass with a new mode, add it to `allowed_search_types` and handle it in `_get_relevant_documents`.
  3. Check `VectorStoreRetriever.allowed_search_types` at runtime when search_type comes from config.

Example fix

# before
retriever = store.as_retriever(search_type="similarity_threshold")  # ValueError

# after
retriever = store.as_retriever(
    search_type="similarity_score_threshold",
    search_kwargs={"score_threshold": 0.5},
)
Defensive patterns

Strategy: validation

Validate before calling

from langchain_core.vectorstores import VectorStoreRetriever

ALLOWED = set(VectorStoreRetriever.allowed_search_types)

if search_type not in ALLOWED:
    raise ValueError(f"search_type must be one of {sorted(ALLOWED)}, got {search_type!r}")
retriever = store.as_retriever(search_type=search_type, **kwargs)

Type guard

def is_allowed_retriever_search_type(value: str) -> bool:
    """Check against the retriever class's allowed_search_types."""
    return isinstance(value, str) and value in VectorStoreRetriever.allowed_search_types

Try / catch

from pydantic import ValidationError

try:
    retriever = store.as_retriever(search_type=search_type)
except ValidationError as e:
    if "search_type" in str(e):
        retriever = store.as_retriever()  # default 'similarity'
    else:
        raise

Prevention

When it happens

Trigger: `vectorstore.as_retriever(search_type='foo')`, constructing `VectorStoreRetriever(vectorstore=..., search_type='top_k')`, or a subclass narrowing `allowed_search_types` and then passing a now-disallowed standard value.

Common situations: Typos in retriever config; copying `search_type` values valid for a vector store subclass but not the base retriever; custom retriever subclasses that restricted `allowed_search_types` while callers still send `'mmr'`.

Related errors


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