OpenBB-finance/OpenBB · error · ValueError
Country '{c}' is not available for {commodity}. Valid countr
Error message
Country '{c}' is not available for {commodity}. Valid countries: {valid_keys} What it means
Country-validation ValueError from the ISO-code branch: the input matched a two-letter code in the global COUNTRY_TO_REGION map, but that code is not in the commodity-specific valid country set fetched from the metadata API (valid_country_codes). The message lists the full valid key set via get_valid_country_keys().
Source
Thrown at openbb_platform/providers/government_us/openbb_government_us/utils/psd_data_downloader.py:363
if c.upper() in REGION_DISPLAY:
selected_region_codes.append(c.upper())
country_codes.append(c.upper())
# Check if it's a region name (includes "european_union" -> R05)
elif country_key in REGIONS:
region_code = REGIONS[country_key]
selected_region_codes.append(region_code)
country_codes.append(region_code)
# Check for "eu" shorthand -> treat as region R05
elif country_key == "eu":
selected_region_codes.append("R05")
country_codes.append("R05")
# Check if it's a country code
elif c.upper() in COUNTRY_TO_REGION:
code = c.upper()
# Validate against commodity-specific countries
if valid_country_codes and code not in valid_country_codes:
valid_keys = get_valid_country_keys()
raise ValueError(
f"Country '{c}' is not available for {commodity}. Valid countries: {valid_keys}"
)
selected_country_codes.append(code)
country_codes.append(code)
# Check if it's a country name (snake_case)
elif country_key in COUNTRIES:
code = COUNTRIES[country_key]
# Validate against commodity-specific countries
if valid_country_codes and code not in valid_country_codes:
valid_keys = get_valid_country_keys()
raise ValueError(
f"Country '{c}' is not available for {commodity}. Valid countries: {valid_keys}"
)
selected_country_codes.append(code)
country_codes.append(code)
else:
# Unknown country/region
valid_keys = sorted(View on GitHub (pinned to 3e071fcc2c)
Solutions
- Choose countries from the valid list in the error message (or the commodity's country metadata).
- Use a region shorthand ('world', 'eu', region codes R00-R18) instead of unsupported individual countries.
- Filter your country list against the commodity's available countries before calling.
Example fix
# before fetch(commodity="cotton", country="LU") # Luxembourg not reported # after fetch(commodity="cotton", country="R15") # use the covering region
Defensive patterns
Strategy: validation
Validate before calling
from openbb_government_us.utils.psd_data_downloader import (
COMMODITIES, _get_commodity_countries, COUNTRY_TO_REGION,
)
def country_available(commodity: str, code: str) -> bool:
valid = set(_get_commodity_countries(COMMODITIES[commodity]).values())
return code.upper() in COUNTRY_TO_REGION and (not valid or code.upper() in valid) Type guard
def is_iso_code_for_commodity(commodity: str, c: str) -> bool is not None and isinstance(c, str) and len(c) == 2 and c.upper() in COUNTRY_TO_REGION and country_available(commodity, c)
Prevention
- Use region codes (R00-R18, 'world', 'eu') when individual countries are unsupported.
- Cache the commodity's country metadata and validate selections against it.
When it happens
Trigger: Passing country='ZW' (or any ISO code) for a commodity the PSD database does not report for that country, e.g. a small country for a niche commodity.
Common situations: Region templates that pass a fixed country list to every commodity; users assuming worldwide coverage per commodity.
Related errors
- Unknown commodity: {commodity} -> Valid choices: {valid_choi
- Attribute '{attr}' is not available for {commodity}. Valid a
- Unknown country/region: '{c}' for {commodity}. Valid countri
- Invalid country: '{value}'. Accepts ISO 3166-1 alpha-2 codes
- Country '{v.name}' ({v.alpha_2}) is not supported by FMP. Va
AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14).
Data as JSON: /api/errors/5db56a24a220bdd2.
Report an issue: GitHub.