influxdata/influxdb · error · ValueError

uint64 field ' ' cannot be negative

Error message

uint64 field '{key}' cannot be negative

What it means

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.

Solutions

  1. Use int64_field(key, value) instead, which accepts negative integers.
  2. Clamp or take abs() of the value if a non-negative semantic is intended: max(0, value).
  3. Validate the value's sign at the data source before calling uint64_field().

Example fix

// before
builder.uint64_field("delta", diff)  # diff may be negative
// after
if diff < 0:
    builder.int64_field("delta", diff)
else:
    builder.uint64_field("delta", diff)
Defensive patterns

Strategy: type-guard

Validate before calling

if value < 0:
    builder.int64_field(key, value)
else:
    builder.uint64_field(key, value)

Type guard

def is_uint64(v: int) -> bool:
    return isinstance(v, int) and 0 <= v <= 2**64 - 1

Try / catch

try:
    builder.uint64_field(key, value)
except ValueError:
    builder.int64_field(key, value)

Prevention

When it happens

Trigger: Calling builder.uint64_field(key, value) with value < 0, e.g. builder.uint64_field("delta", -5).

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of influxdata/influxdb@06200ef96b (2026-09-19). Data as JSON: /api/errors/790c985d2ac2ef3f. Report an issue: GitHub.

Appendix: source

Thrown at influxdb3_py_api/src/line_builder/line_builder.py:81

        """Escape characters in field keys according to line protocol."""
        return (
            value.replace("\\", "\\\\")
            .replace(",", "\\,")
            .replace("=", "\\=")
            .replace(" ", "\\ ")
        )

    def tag(self, key: str, value: str) -> "LineBuilder":
        """Add a tag to the line protocol."""
        self._validate_key(key, "tag")
        self.tags[key] = str(value)
        return self

    def uint64_field(self, key: str, value: int) -> "LineBuilder":
        """Add an unsigned integer field to the line protocol."""
        self._validate_key(key, "field")
        if value < 0:
            raise ValueError(f"uint64 field '{key}' cannot be negative")
        self.fields[key] = f"{value}u"
        return self

    def int64_field(self, key: str, value: int) -> "LineBuilder":
        """Add an integer field to the line protocol."""
        self._validate_key(key, "field")
        self.fields[key] = f"{value}i"
        return self

    def float64_field(self, key: str, value: float) -> "LineBuilder":
        """Add a float field to the line protocol."""
        self._validate_key(key, "field")
        # Check if value has no decimal component
        self.fields[key] = f"{int(value)}.0" if value % 1 == 0 else str(value)
        return self

    def string_field(self, key: str, value: str) -> "LineBuilder":
        """Add a string field to the line protocol."""

View on GitHub (pinned to 06200ef96b)