{"record":{"id":"6188120d5ac1a4fd","repo":"OpenBB-finance/OpenBB","slug":"unexpected-data-format-expected-dict-got-type-618812","errorCode":null,"errorMessage":"Unexpected data format. Expected dict, got: {type(data)}","messagePattern":"Unexpected data format\\. Expected dict, got: (.+?)","errorType":"exception","errorClass":"OpenBBError","httpStatus":null,"severity":"error","filePath":"openbb_platform/providers/benzinga/openbb_benzinga/models/price_target.py","lineNumber":301,"sourceCode":"        \"\"\"Return the raw data from the Benzinga endpoint.\"\"\"\n        # pylint: disable=import-outside-toplevel\n        from openbb_benzinga.utils.helpers import response_callback\n        from openbb_core.provider.utils.helpers import amake_request, get_querystring\n\n        token = credentials.get(\"benzinga_api_key\") if credentials else \"\"\n        base_url = \"https://api.benzinga.com/api/v2.1/calendar/ratings\"\n        query.limit = query.limit or 200\n        querystring = get_querystring(query.model_dump(by_alias=True), [])\n\n        url = f\"{base_url}?{querystring}&token={token}\"\n        data = await amake_request(url, response_callback=response_callback, **kwargs)\n\n        if isinstance(data, dict) and \"ratings\" not in data:\n            raise OpenBBError(\n                f\"Unexpected data format. Expected 'ratings' key, got: {list(data.keys())}\"\n            )\n        if not isinstance(data, dict):\n            raise OpenBBError(\n                f\"Unexpected data format. Expected dict, got: {type(data)}\"\n            )\n        if isinstance(data, dict) and not data.get(\"ratings\"):\n            raise EmptyDataError(\"No ratings data returned.\")\n\n        return data[\"ratings\"]\n\n    @staticmethod\n    def transform_data(\n        query: BenzingaPriceTargetQueryParams,\n        data: list[dict],\n        **kwargs: Any,\n    ) -> list[BenzingaPriceTargetData]:\n        \"\"\"Return the transformed data.\"\"\"\n        results: list[BenzingaPriceTargetData] = []\n        # Remove duplicated field with a URL\n        for item in data:\n            item.pop(\"url_calendar\", None)","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/providers/benzinga/openbb_benzinga/models/price_target.py#L283-L319","documentation":"The type gate at price_target.py:301: if the decoded Benzinga /calendar/ratings payload is not a dict at all (list, str, None), the fetcher raises OpenBBError(\"Unexpected data format. Expected dict, got: {type(data)}\"). The response_callback normally yields dicts, so a non-dict means the endpoint served a bare array or the body was parsed into something unexpected.","triggerScenarios":"Benzinga returning a top-level JSON array on error paths, response_callback receiving HTML/text that decodes to str, or test/mocked responses returning lists.","commonSituations":"API revisions, transparent proxies or gateways rewriting responses, and unit tests that stub amake_request with list payloads.","solutions":["Log the raw body/type to identify what your network path actually received.","Update openbb-benzinga — its response_callback owns normalization for this endpoint.","If mocking in tests, return the documented dict shape ({\"ratings\": [...]}).","Treat as a contract break to report rather than retry identically."],"exampleFix":"# before\nraise OpenBBError(f\"Unexpected data format. Expected dict, got: {type(data)}\")\n\n# after\nif isinstance(data, list):\n    data = {\"ratings\": data}  # tolerate bare-array responses","handlingStrategy":"type-guard","validationCode":"if not isinstance(data, dict):\n    if isinstance(data, list):\n        data = {\"ratings\": data}\n    else:\n        raise OpenBBError(f\"unsupported Benzinga payload: {type(data)}\")","typeGuard":"def is_benzinga_dict(data) -> bool:\n    return isinstance(data, dict)","tryCatchPattern":"from openbb_core.app.model.abstract.error import OpenBBError\ntry:\n    rows = await fetcher.a_fetch_data(query, credentials)\nexcept OpenBBError as e:\n    if \"Expected dict\" in str(e):\n        logger.error(\"payload type drift from Benzinga: %s\", e)  # report; do not blind-retry\n    raise","preventionTips":["Return the documented dict shape ({\"ratings\": [...]}) from test doubles and response callbacks.","Log payload types at integration boundaries (proxies, gateways).","Update openbb-benzinga when the endpoint schema changes."],"tags":["python","openbb","benzinga","provider","type-validation"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}