{"record":{"id":"790c985d2ac2ef3f","repo":"influxdata/influxdb","slug":"uint64-field-key-cannot-be-negative","errorCode":null,"errorMessage":"uint64 field '{key}' cannot be negative","messagePattern":"uint64 field '(.+?)' cannot be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"influxdb3_py_api/src/line_builder/line_builder.py","lineNumber":81,"sourceCode":"        \"\"\"Escape characters in field keys according to line protocol.\"\"\"\n        return (\n            value.replace(\"\\\\\", \"\\\\\\\\\")\n            .replace(\",\", \"\\\\,\")\n            .replace(\"=\", \"\\\\=\")\n            .replace(\" \", \"\\\\ \")\n        )\n\n    def tag(self, key: str, value: str) -> \"LineBuilder\":\n        \"\"\"Add a tag to the line protocol.\"\"\"\n        self._validate_key(key, \"tag\")\n        self.tags[key] = str(value)\n        return self\n\n    def uint64_field(self, key: str, value: int) -> \"LineBuilder\":\n        \"\"\"Add an unsigned integer field to the line protocol.\"\"\"\n        self._validate_key(key, \"field\")\n        if value < 0:\n            raise ValueError(f\"uint64 field '{key}' cannot be negative\")\n        self.fields[key] = f\"{value}u\"\n        return self\n\n    def int64_field(self, key: str, value: int) -> \"LineBuilder\":\n        \"\"\"Add an integer field to the line protocol.\"\"\"\n        self._validate_key(key, \"field\")\n        self.fields[key] = f\"{value}i\"\n        return self\n\n    def float64_field(self, key: str, value: float) -> \"LineBuilder\":\n        \"\"\"Add a float field to the line protocol.\"\"\"\n        self._validate_key(key, \"field\")\n        # Check if value has no decimal component\n        self.fields[key] = f\"{int(value)}.0\" if value % 1 == 0 else str(value)\n        return self\n\n    def string_field(self, key: str, value: str) -> \"LineBuilder\":\n        \"\"\"Add a string field to the line protocol.\"\"\"","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/influxdata/influxdb/blob/06200ef96ba82c5f6727e5038a83af8e722c6875/influxdb3_py_api/src/line_builder/line_builder.py#L63-L99","documentation":"uint64_field() is for unsigned 64-bit integers in line protocol, denoted with a trailing 'u'. A negative Python int cannot be represented as a uint64, so the builder raises ValueError before storing the field. Use int64_field() for values that can be negative.","triggerScenarios":"Calling builder.uint64_field(key, value) with value < 0, e.g. builder.uint64_field(\"delta\", -5).","commonSituations":"Computing differences of unsigned counters where a later snapshot is smaller than an earlier one; passing signed sensor deltas or temperatures; type conversions from signed sources without a range check.","solutions":["Use int64_field(key, value) instead, which accepts negative integers.","Clamp or take abs() of the value if a non-negative semantic is intended: max(0, value).","Validate the value's sign at the data source before calling uint64_field()."],"exampleFix":"// before\nbuilder.uint64_field(\"delta\", diff)  # diff may be negative\n// after\nif diff < 0:\n    builder.int64_field(\"delta\", diff)\nelse:\n    builder.uint64_field(\"delta\", diff)","handlingStrategy":"type-guard","validationCode":"if value < 0:\n    builder.int64_field(key, value)\nelse:\n    builder.uint64_field(key, value)","typeGuard":"def is_uint64(v: int) -> bool:\n    return isinstance(v, int) and 0 <= v <= 2**64 - 1","tryCatchPattern":"try:\n    builder.uint64_field(key, value)\nexcept ValueError:\n    builder.int64_field(key, value)","preventionTips":["Use int64_field by default; reserve uint64_field for provably non-negative values.","Validate sign at the data source, especially for counter differences.","Clamp with max(0, value) when negative values are meaningless."],"tags":["validation","value-out-of-range","python","influxdb"],"backgroundTag":"value-out-of-range","analyzedSha":"06200ef96ba82c5f6727e5038a83af8e722c6875","analyzedAt":"2026-09-19T12:55:30.003Z","contentChangedAt":"2026-09-19T12:55:30.003Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}