OpenBB-finance/OpenBB · error · ValueError

Country and counterpart parameters cannot be empty.

Error message

Country and counterpart parameters cannot be empty.

What it means

Raised at the entry of imts_query (IMF Direction of Trade / International Merchandise Trade Statistics builder) when either the country or the counterpart argument is falsy. Both sides of a trade query are mandatory — the function builds an SDMX key that needs a reporter and a partner — so an empty side aborts before any validation or request.

Source

Thrown at openbb_platform/providers/imf/openbb_imf/utils/dot_helpers.py:165

        The frequency of the data, by default "A" (annual).
    start_date : str | None
        The start date of the data, by default None.
    end_date : str | None
        The end date of the data, by default None.
    **kwargs : dict
        Additional query parameters to pass to the API.

    Returns
    -------
    dict
        A dictionary with keys: 'data' containing the fetched data,
        and 'metadata' containing the related metadata.
    """
    # pylint: disable=import-outside-toplevel
    from openbb_imf.utils.query_builder import ImfQueryBuilder

    if not country or not counterpart:
        raise ValueError("Country and counterpart parameters cannot be empty.")

    freq = freq[0].upper()

    if freq and freq not in ["A", "Q", "M"]:
        raise ValueError("Frequency must be one of 'A', 'Q', or 'M'.")

    query_builder = ImfQueryBuilder()
    dataflow_id = "IMTS"
    params = query_builder.metadata.get_dataflow_parameters(dataflow_id)
    country_values = {item["value"] for item in params.get("COUNTRY", [])}
    counterpart_values = {
        item["value"]
        for item in params.get("COUNTERPART_COUNTRY", params.get("COUNTRY", []))
    }

    def _validate_selection(selection, valid_values, name):
        """Validate country or counterpart selection."""
        if not valid_values:

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Always pass both sides: country (reporter) and counterpart (partner).
  2. Use '*' as counterpart to get trade with all partners when a single partner was not intended.
  3. Validate both args non-empty before calling in pipelines built from user input.

Example fix

# before
res = imts_query(country='USA', counterpart='', indicator='TXG_FOB_USD')

# after
res = imts_query(country='USA', counterpart='*', indicator='TXG_FOB_USD')
Defensive patterns

Strategy: validation

Validate before calling

if not country or not counterpart:
    counterpart = counterpart or '*'
country = country or 'USA'
assert country and counterpart
res = imts_query(country=country, counterpart=counterpart, indicator='TXG_FOB_USD')

Type guard

def is_valid_trade_pair(country, counterpart) -> bool:
    def ok(v):
        return bool(v) and (v == '*' or all(isinstance(x, str) and x.strip() for x in ([v] if isinstance(v, str) else v)))
    return ok(country) and ok(counterpart)

Try / catch

try:
    res = imts_query(country=c, counterpart=cp, indicator=ind)
except ValueError as e:
    if 'cannot be empty' in str(e):
        cp = '*'
        res = imts_query(country=c, counterpart=cp, indicator=ind)
    else:
        raise

Prevention

When it happens

Trigger: imts_query(country=None, counterpart='USA', ...) or counterpart='' from a UI where the user selected only one side of the trade pair; forgetting that counterpart is a required positional arg.

Common situations: Optional-looking parameters in wrapper code defaulting to None; multi-select forms where the partner-country field was skipped; refactoring from bilateral(country_a, country_b) signatures.

Related errors


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