{"record":{"id":"0e555f6b278cfc7d","repo":"OpenBB-finance/OpenBB","slug":"invalid-data-format-expected-a-list-of-imfportinf","errorCode":null,"errorMessage":"Invalid data format. Expected a list of ImfPortInfoData.","messagePattern":"Invalid data format\\. Expected a list of ImfPortInfoData\\.","errorType":"exception","errorClass":"OpenBBError","httpStatus":null,"severity":"error","filePath":"openbb_platform/providers/imf/openbb_imf/views/port_info.py","lineNumber":27,"sourceCode":"    from numpy import nan\n    from openbb_core.app.model.abstract.error import OpenBBError\n    from pandas import DataFrame\n\n    try:\n        import plotly.express as px\n        from openbb_charting.core.openbb_figure import OpenBBFigure\n    except Exception as e:\n        raise OpenBBError(\n            \"Could not import Charting modules. Install with `pip install openbb-charting`.\"\n            + f\" -> {e}\"\n        ) from e\n\n    if (\n        data is not None\n        and not isinstance(data, list)\n        or not all(isinstance(item, ImfPortInfoData) for item in data)\n    ):\n        raise OpenBBError(\"Invalid data format. Expected a list of ImfPortInfoData.\")\n    if len(data) == 0:\n        raise OpenBBError(\"No data to plot.\")\n\n    df = DataFrame([item.model_dump() for item in data]).query(\"vessel_count_total > 0\")\n\n    min_size, max_size = 4, 10\n\n    if \"country\" in df.columns and df[\"country\"].nunique() == 1:\n        share_import = df[\"share_country_maritime_import\"].fillna(0)\n        share_export = df[\"share_country_maritime_export\"].fillna(0)\n        df[\"import_export_share\"] = share_import + share_export\n        share_values = df[\"import_export_share\"]\n\n        if share_values.nunique() > 1:\n            df[\"marker_size\"] = (\n                (share_values - share_values.min())\n                / (share_values.max() - share_values.min() + 1e-9)\n            ) * (max_size - min_size) + min_size","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/providers/imf/openbb_imf/views/port_info.py#L9-L45","documentation":"Type guard in the port-info map view: data must be a list of ImfPortInfoData instances. Due to operator precedence the check is (data is not None and not isinstance(data, list)) or not all(isinstance(item, ImfPortInfoData)); any non-list input, any item of the wrong type, or mixed content triggers it. None input is not rejected here and fails later at len().","triggerScenarios":"Passing a DataFrame, list of dicts, generator, or a list mixing ImfPortInfoData with raw rows/different provider models into plot_port_info_map; forwarding deserialized (dict) results from an API response instead of model instances.","commonSituations":"Callers converting fetcher output to dicts/JSON for transport, then calling the chart view without re-validating; combining port data with other maritime results; mock objects in tests failing isinstance checks.","solutions":["Pass the endpoint's model-validated results list directly.","Re-validate dicts first: [ImfPortInfoData.model_validate(d) for d in rows].","Check for None before calling (guard does not cover it).","Keep homogeneous lists - one model type per call."],"exampleFix":"# before\nplot_port_info_map(port_rows_as_dicts)\n# after\nfrom openbb_imf.models.port_info import ImfPortInfoData\nplot_port_info_map([ImfPortInfoData.model_validate(d) for d in port_rows_as_dicts])","handlingStrategy":"type-guard","validationCode":"from openbb_imf.models.port_info import ImfPortInfoData\n\ndef is_port_info_list(data) -> bool:\n    return isinstance(data, list) and len(data) > 0 and all(\n        isinstance(i, ImfPortInfoData) for i in data\n    )","typeGuard":"from openbb_imf.models.port_info import ImfPortInfoData\n\ndef is_port_info_list(data: object) -> bool:\n    return isinstance(data, list) and len(data) > 0 and all(\n        isinstance(i, ImfPortInfoData) for i in data\n    )","tryCatchPattern":"from openbb_core.app.model.abstract.error import OpenBBError\ntry:\n    plot_port_info_map(data)\nexcept OpenBBError as e:\n    if 'Invalid data format' in str(e) and isinstance(data, list):\n        plot_port_info_map([ImfPortInfoData.model_validate(d) for d in data])\n    else:\n        raise","preventionTips":["Pass endpoint .results directly to chart views","Re-validate dicts with model_validate before plotting","Never mix provider models in one list"],"tags":["imf","type-validation","charting","openbberror"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}