{"record":{"id":"cda23e8d42ac5cb2","repo":"lancedb/lancedb","slug":"cannot-add-note-to-exception","errorCode":null,"errorMessage":"Cannot add note to exception","messagePattern":"Cannot add note to exception","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/python/lancedb/util.py","lineNumber":459,"sourceCode":"\n    return new_func\n\n\ndef validate_table_name(name: str):\n    \"\"\"Verify the table name is valid.\"\"\"\n    native_validate_table_name(name)\n\n\ndef add_note(base_exception: BaseException, note: str):\n    if hasattr(base_exception, \"add_note\"):\n        base_exception.add_note(note)\n    elif isinstance(base_exception.args[0], str):\n        base_exception.args = (\n            base_exception.args[0] + \"\\n\" + note,\n            *base_exception.args[1:],\n        )\n    else:\n        raise ValueError(\"Cannot add note to exception\")\n\n\ndef tbl_to_tensor(tbl: pa.Table):\n    \"\"\"\n    Convert a PyArrow Table to a PyTorch Tensor.\n\n    Each column is converted to a tensor (using zero-copy via DLPack)\n    and the columns are then stacked into a single tensor.\n\n    Fails if torch is not installed.\n    Fails if any column is more than one chunk.\n    Fails if a column's data type is not supported by PyTorch.\n\n    Parameters\n    ----------\n    tbl : pa.Table or pa.RecordBatch\n        The table or record batch to convert to a tensor.\n","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/lancedb/lancedb/blob/c7b051aff7039333a3f61b79217246c27676806a/python/python/lancedb/util.py#L441-L477","documentation":"add_note attaches a note to an exception either via the Python 3.11+ add_note method, or by string-concatenating onto args[0] if args[0] is a str. If the base exception's first arg is not a string and add_note is unavailable, it raises ValueError('Cannot add note to exception').","triggerScenarios":"Calling add_note(exc, note) (directly or via _add_unique_note in the embedding error path, e.g. __resolveVariables/get_embedding_func failures) on an exception whose args[0] is not a str (e.g. KeyError with non-string key, custom exception taking an int) on Python < 3.11.","commonSituations":"Annotating exceptions raised by libraries that pass non-string payloads (KeyError('missing_col') actually has str args, but KeyError(404) does not); running on Python 3.10 or older where Exception.add_note does not exist.","solutions":["Upgrade to Python 3.11+ where native add_note is always used","Before calling, ensure the target exception has a string first arg, or re-wrap it: raise type(exc)(str(exc)) from exc","Wrap the original exception in a RuntimeError with the note instead of mutating it"],"exampleFix":"// before\nadd_note(exc, \"embedding setup failed\")  # ValueError if exc.args[0] not str\n// after\nif not (hasattr(exc, \"add_note\") or (exc.args and isinstance(exc.args[0], str))):\n    exc = RuntimeError(str(exc))\nadd_note(exc, \"embedding setup failed\")","handlingStrategy":"try-catch","validationCode":"def can_annotate(exc) -> bool:\n    return hasattr(exc, \"add_note\") or bool(exc.args and isinstance(exc.args[0], str))","typeGuard":"def annotatable(exc: BaseException) -> bool:\n    return hasattr(exc, \"add_note\") or (exc.args and isinstance(exc.args[0], str))","tryCatchPattern":"try:\n    add_note(exc, note)\nexcept ValueError as e:\n    if \"Cannot add note\" in str(e):\n        raise RuntimeError(f\"{note}: {exc!r}\") from exc\n    raise","preventionTips":["Run on Python 3.11+ so native add_note is always available","Wrap non-string-arg exceptions in RuntimeError before annotating","Never rely on mutating exceptions produced by third-party code with exotic args"],"tags":["python","exceptions","python-version"],"backgroundTag":"invalid-argument-value","analyzedSha":"c7b051aff7039333a3f61b79217246c27676806a","analyzedAt":"2026-09-08T23:42:37.579Z","contentChangedAt":"2026-09-08T23:42:37.579Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}