OpenBB-finance/OpenBB · error · OpenBBError

Query string is empty or invalid -> '{query}'

Error message

Query string is empty or invalid -> '{query}'

What it means

Raised by search_dataflows on the IMF metadata object when _parse_query returns an empty structure for the supplied query string. The parser understands AND ('+' or implicit) and OR ('|') operators plus quoted phrases; a string consisting only of operators, whitespace, or unbalanced quotes parses to nothing and is rejected here before any matching runs.

Source

Thrown at openbb_platform/providers/imf/openbb_imf/utils/metadata.py:130

    def search_dataflows(self, query: str) -> list[dict]:
        """Search dataflows based on a query string.

        Parameters
        ----------
        query : str
            The search query string, which can include AND (+) and OR (|) operators,
            as well as quoted phrases for exact matches.
        Returns
        -------
        list[dict]
            A list of matching dataflows, grouped by their structureRef ID.
        """
        grouped_results: dict = {}
        parsed_query = self._parse_query(query)

        if not parsed_query:
            raise OpenBBError(
                ValueError(f"Query string is empty or invalid -> '{query}'")
            )

        for dataflow_obj in self.dataflows.values():
            dataflow_id = dataflow_obj.get("id", "").lower()
            dataflow_name = dataflow_obj.get("name", "").lower()
            dataflow_description = dataflow_obj.get("description", "").lower()
            dataflow_matches = False

            for or_group in parsed_query:
                or_group_matches_all_and_terms = True

                for and_term in or_group:
                    if not (
                        and_term in dataflow_id
                        or and_term in dataflow_name
                        or and_term in dataflow_description
                    ):

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Provide at least one real search term: query='trade', query='balance+payments', query='goods|services'.
  2. Sanitize built queries: strip operator-only results and require a non-empty term list before calling.
  3. Use quoted phrases for multi-word matches: query='"direction of trade"'.

Example fix

# before
terms = [t for t in user_filters if selected.get(t)]  # all empty
meta.search_dataflows(query='+'.join(terms))

# after
terms = [t.strip() for t in user_filters if selected.get(t) and t.strip()]
if not terms:
    raise ValueError('At least one search term is required.')
meta.search_dataflows(query='+'.join(terms))
Defensive patterns

Strategy: validation

Validate before calling

import re
def build_query(terms: list[str]) -> str | None:
    cleaned = [t.strip() for t in terms if t and t.strip()]
    if not cleaned:
        return None
    return '+'.join(cleaned)

q = build_query(terms)
if q is None:
    raise ValueError('At least one search term is required.')
results = meta.search_dataflows(query=q)

Type guard

def is_parseable_search_query(q: str | None) -> bool:
    if not q:
        return False
    # at least one alphanumeric token outside quotes/operators
    return bool(re.search(r'[A-Za-z0-9]', q.replace('"', '')))

Try / catch

try:
    results = meta.search_dataflows(query=q)
except OpenBBError as e:
    if 'empty or invalid' in str(e):
        results = list(meta.dataflows.values())  # or prompt user for a term
    else:
        raise

Prevention

When it happens

Trigger: query='+', query='|', query='"', query=' ', or a query built by joining an empty/whitespace term list with operators (e.g. '+'.join(terms) with terms all empty).

Common situations: Programmatically assembled queries from optional filters where all filters are blank; users quoting phrases but leaving the closing quote off; copy-paste introducing stray operator characters.

Related errors


AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14). Data as JSON: /api/errors/ded07bd6b0cc6ebb. Report an issue: GitHub.