{"record":{"id":"146c65b193c05f59","repo":"OpenBB-finance/OpenBB","slug":"no-valid-port-code-provided","errorCode":null,"errorMessage":"No valid port_code provided.","messagePattern":"No valid port_code provided\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"openbb_platform/providers/imf/openbb_imf/models/port_volume.py","lineNumber":155,"sourceCode":"                new_values.append(new_item)\n            # Accept first part of port name (before dash)\n            elif item_lower in [\n                v_.split(\" - \")[0].lower().replace(\" \", \"_\")\n                for v_ in port_id_map.values()\n            ]:\n                first_parts = [\n                    v_.split(\" - \")[0].lower().replace(\" \", \"_\")\n                    for v_ in port_id_map.values()\n                ]\n                idx = first_parts.index(item_lower)\n                new_values.append(list(port_id_map.keys())[idx])\n            else:\n                raise ValueError(\n                    f\"Invalid port_code: {item}. Must be a valid port ID or name.Available options: {port_id_choices}.\"\n                )\n\n        if not new_values:\n            raise ValueError(\"No valid port_code provided.\")\n\n        return \",\".join(new_values)\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def validate_model(cls, values):\n        \"\"\"Validate the model before instantiation.\"\"\"\n        if values.get(\"start_date\") is not None and values[\"start_date\"] < dateType(\n            2019, 1, 1\n        ):\n            raise OpenBBError(\n                ValueError(\n                    f\"Minimum start_date is 2019-01-01. Got {values['start_date']} instead.\"\n                )\n            )\n        if not values.get(\"port_code\") and not values.get(\"country\"):\n            values[\"port_code\"] = \"port1114\"\n","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/OpenBB-finance/OpenBB/blob/3e071fcc2cd9f891cac6040ae60296dba76dab46/openbb_platform/providers/imf/openbb_imf/models/port_volume.py#L137-L173","documentation":"Thrown by the port_code field validator on ImfPortVolumeQueryParams when, after normalizing every supplied entry against the IMF PortWatch port ID/name map, zero valid values remain. Individually bad entries raise a separate 'Invalid port_code' error, so this fires when the input itself contains no usable entries (empty string, empty list, or entries that normalize to nothing). It means the query carries no ports to fetch and the request would be meaningless.","triggerScenarios":"Calling the IMF port volume endpoint with port_code='' , port_code=[] , or a whitespace-only value, while also not supplying a country (the before-validator only substitutes the default 'port1114' when port_code is falsy AND country is absent, so an explicitly empty collection that survives to the field validator triggers this).","commonSituations":"A UI multi-select for ports left empty; code that dynamically joins a port list (','.join([]) yields ''); a previous step resolving country->ports returning an empty result that is passed straight through.","solutions":["Pass at least one valid port ID (e.g. 'port1114') or a valid port name, or omit port_code and pass country instead so the default port is used.","Guard before the call: skip or prompt when the constructed port_code string/list is empty.","List valid IDs/names from the port_id_map used by the provider (keys are IDs, values are 'snake_name - country' labels) to build UI choices."],"exampleFix":"# before\nobb.economy.transport_and_shipping.port_volume(provider='imf', port_code=','.join(selected_ports))  # selected_ports == []\n\n# after\nif not selected_ports:\n    raise ValueError('Select at least one port or pass a country.')\nobb.economy.transport_and_shipping.port_volume(provider='imf', port_code=','.join(selected_ports))","handlingStrategy":"validation","validationCode":"def valid_port_input(port_code):\n    if port_code is None:\n        return False\n    vals = port_code.split(',') if isinstance(port_code, str) else list(port_code)\n    return len([v for v in vals if v and v.strip()]) > 0\n\nif not valid_port_input(port_code):\n    raise ValueError('Provide at least one port_code or a country.')","typeGuard":"def is_non_empty_port_selection(value: str | list[str] | None) -> bool:\n    if value is None:\n        return False\n    items = value.split(',') if isinstance(value, str) else value\n    return bool(items) and any(isinstance(i, str) and i.strip() for i in items)","tryCatchPattern":"try:\n    res = await obb.economy.port_volume(provider='imf', port_code=port_code)\nexcept (ValueError, OpenBBError) as e:\n    if 'No valid port_code' in str(e):\n        # rebuild selection / prompt user\n        ...","preventionTips":["Never pass a joined string built from an unvalidated list.","Default the UI/caller to the flagship port (port1114) or a country when no ports are selected.","Expose the provider's port ID/name map as the source of valid choices."],"tags":["validation","parameters","imf","ports","query-params"],"backgroundTag":null,"analyzedSha":"3e071fcc2cd9f891cac6040ae60296dba76dab46","analyzedAt":"2026-08-14T23:40:48.960Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}