OpenBB-finance/OpenBB · error · ValueError

Validation error when converting dict to BaseModel: {e}

Error message

Validation error when converting dict to BaseModel: {e}

What it means

ValueError raised by dict_to_basemodel when constructing the generic Data(**data_dict) model raises a Pydantic ValidationError - e.g. a field fails coercion (str where number expected) or an unknown/misconfigured constraint. The original ValidationError is chained as the cause, so details of the failing field are preserved in e.

Source

Thrown at openbb_platform/core/openbb_core/app/utils.py:115

    base_models = []
    for item in data_list:
        if isinstance(item, Data) or issubclass(type(item), Data):
            base_models.append(item)
        elif isinstance(item, dict):
            base_models.append(Data(**item))
        elif isinstance(item, (DataFrame, Series)):
            base_models.extend(df_to_basemodel(item))
        else:
            raise ValueError(f"Unsupported list item type: {type(item)}")
    return base_models


def dict_to_basemodel(data_dict: dict) -> Data:
    """Convert a dictionary to BaseModel."""
    try:
        return Data(**data_dict)
    except ValidationError as e:
        raise ValueError(
            f"Validation error when converting dict to BaseModel: {e}"
        ) from e


def ndarray_to_basemodel(array: "ndarray") -> list[Data]:
    """Convert a NumPy array to list of BaseModel."""
    # Assuming a 2D array where rows are records
    if array.ndim != 2:
        raise ValueError("Only 2D arrays are supported.")
    return [
        Data(**{f"column_{i}": value for i, value in enumerate(row)}) for row in array
    ]


def convert_to_basemodel(data) -> Data | list[Data]:
    """Dispatch function to convert different types to BaseModel."""
    # pylint: disable=import-outside-toplevel
    from numpy import ndarray

View on GitHub (pinned to 3e071fcc2c)

Solutions

  1. Inspect the chained ValidationError (raise ... from e) for the exact field and reason
  2. Clean the dict before conversion: strip 'N/A'/'-' placeholders, cast numerics, drop or fix invalid keys
  3. If writing a provider, do the coercion in the fetcher/transform step instead of relying on Data
  4. Upgrade the provider package if upstream parsing was fixed

Example fix

# before
row = {'date': '2024-01-02', 'close': 'N/A'}
model = convert_to_basemodel(row)

# after
row = {'date': '2024-01-02', 'close': None}
model = convert_to_basemodel(row)
Defensive patterns

Strategy: validation

Validate before calling

def clean_row(d: dict) -> dict:
    return {
        k: (None if isinstance(v, str) and v.strip() in {'', '-', 'N/A', 'n/a'} else v)
        for k, v in d.items()
    }

Type guard

def is_valid_row(d: dict) -> bool:
    try:
        Data(**d)
        return True
    except Exception:
        return False

Try / catch

try:
    model = dict_to_basemodel(row)
except ValueError as e:
    logger.warning('skipping invalid row %s: %s', row, e.__cause__)
    model = None  # skip bad record in ETL loops

Prevention

When it happens

Trigger: convert_to_basemodel({'close': 'n/a'}) where the dict came from a provider row whose string values cannot coerce to the declared field types; nested dicts that Data rejects; keys with None for required fields in strict contexts.

Common situations: Provider responses containing placeholder strings ('-', 'N/A') in numeric columns; malformed API payloads after schema drift; user code hand-building dicts with wrong key casing (aliases not enabled) so validation fails.

Related errors


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