{"record":{"id":"681bd3085adc331f","repo":"commaai/openpilot","slug":"unable-to-get-max-segment-number-ensure-you-have","errorCode":null,"errorMessage":"unable to get max_segment_number. ensure you have access to this route or the route is public.","messagePattern":"unable to get max_segment_number\\. ensure you have access to this route or the route is public\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"openpilot/tools/lib/route.py","lineNumber":316,"sourceCode":"    az_prefix = '/'.join(key.split('/')[:3])\n    return SegmentName.from_azure_prefix(az_prefix)\n\n  @staticmethod\n  def from_azure_prefix(prefix):\n    # xxxxxxxx/1111-11-11-11--11-11-11/0\n    dongle_id, route_name, segment_num = prefix.split(\"/\")\n    return SegmentName(dongle_id + \"|\" + route_name + \"--\" + segment_num)\n\n\n@cache\ndef get_max_seg_number_cached(sr: 'SegmentRange') -> int:\n  try:\n    api = CommaApi(get_token())\n    max_seg_number = api.get(\"/v1/route/\" + sr.route_name.replace(\"/\", \"|\"))[\"maxqlog\"]\n    assert isinstance(max_seg_number, int)\n    return max_seg_number\n  except Exception as e:\n    raise Exception(\"unable to get max_segment_number. ensure you have access to this route or the route is public.\") from e\n\n\nclass SegmentRange:\n  def __init__(self, segment_range: str):\n    m = re.fullmatch(RE.SEGMENT_RANGE, segment_range)\n    assert m is not None, f\"Segment range is not valid {segment_range}\"\n    self.m = m\n\n  @property\n  def route_name(self) -> str:\n    return self.m.group(\"route_name\")\n\n  @property\n  def dongle_id(self) -> str:\n    return self.m.group(\"dongle_id\")\n\n  @property\n  def log_id(self) -> str:","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/commaai/openpilot/blob/516ec1e68203439a73f340f1d0b3b91eabc626ee/openpilot/tools/lib/route.py#L298-L334","documentation":"get_max_seg_number_cached() calls the comma API '/v1/route/<name>' to read 'maxqlog'; any exception (auth failure, 404, network, or a non-int response) is re-raised as a generic Exception with an access hint. SegmentRange operations that expand unbounded ranges (e.g. 'route/3+') need this number.","triggerScenarios":"Using an open-ended SegmentRange like 'route/2--' or route/3+, which must ask the API how many segments exist; calling it for a route your token cannot access, a non-existent route, or while offline.","commonSituations":"Not logged in (get_token() fails) or expired comma account token; typo'd route name; running tools in an environment without network; private route owned by another user.","solutions":["Authenticate: run `python tools/lib/auth.py` (or re-login) so get_token() returns a valid token","Confirm the route exists and is accessible to your account via cabana/connect","Avoid the API call by giving an explicit end segment ('route/2--8' instead of 'route/2--')"],"exampleFix":"# before\nsr = SegmentRange('b0c9d232|2022-01-01--00-00-00/3+')\n\n# after\nsr = SegmentRange('b0c9d232|2022-01-01--00-00-00/3--7')  # explicit end: no max_seg_number lookup","handlingStrategy":"validation","validationCode":"from openpilot.tools.lib.api import CommaApi\nfrom openpilot.tools.lib.auth import get_token\n\ndef route_accessible(route_name: str) -> bool:\n    try:\n        CommaApi(get_token()).get('/v1/route/' + route_name.replace('/', '|'))\n        return True\n    except Exception:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    sr = SegmentRange('route/3+')\n    _ = get_max_seg_number_cached(sr)\nexcept Exception as e:\n    if 'max_segment_number' in str(e):\n        sr = SegmentRange('route/3--7')  # explicit end, no API needed","preventionTips":["Prefer explicit segment ranges ('route/2--8') over open-ended ones in scripts","Log in (auth) before running tools that hit the API","Verify route name spelling and access rights first"],"tags":["network","auth","api","route"],"backgroundTag":null,"analyzedSha":"516ec1e68203439a73f340f1d0b3b91eabc626ee","analyzedAt":"2026-08-15T00:17:37.461Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}