influxdata/influxdb · error · InvalidMeasurementError

Measurement name cannot contain spaces

Error message

Measurement name cannot contain spaces

What it means

The Python SDK's LineBuilder is a helper for constructing InfluxDB line protocol strings. Measurement names are taken as-is and spaces are not valid unescaped measurement separators in line protocol, so the constructor eagerly rejects any measurement containing a space by raising InvalidMeasurementError. Escaping is handled internally (via _escape_measurement) for characters like commas, but spaces in the measurement name are disallowed at construction time.

Solutions

  1. Remove the space from the measurement name (use underscores or CamelCase).
  2. Pass the escaped form explicitly if a space is truly required in line protocol ('my\ measurement'), though a space-free name is strongly recommended.
  3. Sanitize/normalize user- or config-supplied measurement names before constructing LineBuilder.

Example fix

# before
lb = LineBuilder("cpu usage")
# after
lb = LineBuilder("cpu_usage")
Defensive patterns

Strategy: validation

Validate before calling

def safe_line_builder(measurement: str) -> "LineBuilder":
    name = measurement.strip().replace(" ", "_")
    if not name:
        raise ValueError("measurement name is empty")
    return LineBuilder(name)

Type guard

def is_valid_measurement(m) -> bool:
    return isinstance(m, str) and len(m) > 0 and " " not in m

Try / catch

try:
    lb = LineBuilder(measurement)
except InvalidMeasurementError:
    lb = LineBuilder(measurement.replace(" ", "_"))

Prevention

When it happens

Trigger: Calling LineBuilder("my measurement") — any measurement string containing a literal space character (' ') passed to __init__.

Common situations: Building measurement names from human-readable labels or user input ('cpu usage'), concatenating a name with a unit separated by a space, or copying a display name into code.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

    pass


class InvalidKeyError(InfluxDBError):
    """Raised when a tag or field key is invalid"""

    pass


class InvalidLineError(InfluxDBError):
    """Raised when a line protocol string is invalid"""

    pass


class LineBuilder:
    def __init__(self, measurement: str):
        if " " in measurement:
            raise InvalidMeasurementError("Measurement name cannot contain spaces")
        self.measurement = measurement
        self.tags: OrderedDict[str, str] = OrderedDict()
        self.fields: OrderedDict[str, str] = OrderedDict()
        self._timestamp_ns: Optional[int] = None

    def _validate_key(self, key: str, key_type: str) -> None:
        """Validate that a key does not contain spaces, commas, or equals signs."""
        if not key:
            raise InvalidKeyError(f"{key_type} key cannot be empty")
        if " " in key:
            raise InvalidKeyError(f"{key_type} key '{key}' cannot contain spaces")
        if "," in key:
            raise InvalidKeyError(f"{key_type} key '{key}' cannot contain commas")
        if "=" in key:
            raise InvalidKeyError(f"{key_type} key '{key}' cannot contain equals signs")

    def _escape_measurement(self, value: str) -> str:
        """Escape characters in measurement names according to line protocol."""

View on GitHub (pinned to 06200ef96b)