OpenBB-finance/OpenBB · error · KeyError

Dimension '{dimension[0]}' not valid for this dataflow. Vali

Error message

Dimension '{dimension[0]}' not valid for this dataflow. Valid dimensions: {list(self._selections.keys())}

What it means

set_dimension raises KeyError when the tuple's dimension ID is not in this dataflow's _dimensions list. Selections are keyed strictly by known dimensions, and downstream selections are cleared on each set, so an unknown key is rejected up front.

Source

Thrown at openbb_platform/providers/imf/openbb_imf/utils/progressive_helper.py:168

                codelist_id, agency_id, self.dataflow_id
            )
        return {}

    def set_dimension(self, dimension: tuple[str, str]) -> dict:
        """Set a value for a dimension and clear downstream selections.

        Parameters
        ----------
        dimension : tuple
            A tuple of (dimension_id, value) to set.

        Returns
        -------
        dict
            The updated selections after setting the dimension.
        """
        if dimension[0] not in self._dimensions:
            raise KeyError(
                f"Dimension '{dimension[0]}' not valid for this dataflow."
                f" Valid dimensions: {list(self._selections.keys())}"
            )
        self._selections[dimension[0]] = dimension[1]
        # When a selection is made, we clear selections for downstream dimensions
        # as they might now be invalid.
        found_dim = False
        for dim in self._dimensions:
            if found_dim:
                self._selections[dim] = None
            if dim == dimension[0]:
                found_dim = True

        self.current_dimension = self.get_next_dimension_to_select()

        return self._selections.copy()

    def get_dimensions(self) -> dict[str, str | None]:

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Validate the dimension ID against builder._dimensions before calling set_dimension.
  2. Use the IDs returned by get_dimension_options / the dataflow's DSD (e.g. FREQUENCY not FREQ).
  3. In generic front-ends, present only valid dimension IDs and reject others client-side.

Example fix

# before
builder.set_dimension(('FREQ', 'A'))  # KeyError

# after
if 'FREQ' in builder._dimensions:
    builder.set_dimension(('FREQ', 'A'))
else:
    builder.set_dimension(('FREQUENCY', 'A'))
Defensive patterns

Strategy: validation

Validate before calling

def safe_set_dimension(builder, dim: str, value):
    if dim not in builder._dimensions:
        raise ValueError(f'Unknown dimension {dim!r}; valid: {builder._dimensions}')
    return builder.set_dimension((dim, value))

Type guard

def is_valid_dimension(builder, dim: str) -> bool:
    return dim in builder._dimensions

Try / catch

try:
    builder.set_dimension((dim, value))
except KeyError as e:
    if 'not valid for this dataflow' in str(e):
        closest = difflib.get_close_matches(dim, builder._dimensions, n=1)
        if closest:
            builder.set_dimension((closest[0], value))
        else:
            raise
    else:
        raise

Prevention

When it happens

Trigger: Calling builder.set_dimension(('FREQ', 'A')) when the dataflow uses 'FREQUENCY'; mixing dimension IDs from another dataflow; typos in interactive tooling that feed arbitrary keys into set_dimension.

Common situations: Porting parameter dictionaries between dataflows, hardcoded scripts written against an older DSD, or building generic UIs that pass user input straight through.

Related errors


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