def validate_query(q: Optional[Union[dict, str, CustomVector]], search_method: Union[str, SearchMethod]) -> Optional[
    Union[dict, str, CustomVector]]:
    """
    Returns q if an error is not raised"""
    usage_ref = f"See query reference here: {marqo_docs.query_reference()}"

    # TODO - it looks like API pydantic model is catching invalid input (e.g. bad dict) before it reaches this point
    from marqo.tensor_search.models.api_models import CustomVectorQuery

    if isinstance(q, str) or q is None:
        return q
    elif isinstance(q, CustomVectorQuery):
        if search_method.upper() != SearchMethod.HYBRID and search_method.upper() != SearchMethod.TENSOR:
            raise InvalidArgError(
                'Custom vector search is only supported for search_method="HYBRID" and search_method="TENSOR". '
                f"{usage_ref}")

        return q
    elif isinstance(q, dict):
        if search_method.upper() == SearchMethod.LEXICAL:
            raise InvalidArgError(
                "Multi-term query is not supported for search_method=\"LEXICAL\""
            )
        elif search_method.upper() == SearchMethod.HYBRID:
            raise InvalidArgError(
                "To use multi-term query with search_method=\"HYBRID\", "
                f"use 'hybrid_parameters.queryTensor' instead of 'q'. See {marqo_docs.hybrid_parameters()}"
            )
        if not len(q):
            raise InvalidArgError(
                "Multi-term query requires at least one query. Received empty dictionary. "
                f"{usage_ref}"
            )
        for k, v in q.items():
            base_invalid_kv_message = "Multi queries dictionaries must be <string>:<float> pairs. "
            if not isinstance(k, str):
                raise InvalidArgError(
                    f"{base_invalid_kv_message}Found key of type `{type(k)}` instead of string. Key=`{k}`. "
                    f"{usage_ref}"
                )
            if not isinstance(v, (int, float)):
                raise InvalidArgError(
                    f"{base_invalid_kv_message}Found value of type `{type(v)}` instead of float. Value=`{v}`. "
                    f" {usage_ref}"
                )
    else:
        raise InvalidArgError(
            f"'q' must be a 'string', a 'dict', or 'None' (if 'context' is provided)! Received q of type `{type(q)}`. "
            f"q=`{q}` "
            f"{usage_ref}"
        )
    return q

    Returns q if an error is not raised
src/marqo/tensor_search/validation.py