{"record":{"id":"f2fdb97be1c957e6","repo":"influxdata/influxdb","slug":"key-type-key-cannot-be-empty","errorCode":null,"errorMessage":"{key_type} key cannot be empty","messagePattern":"(.+?) key cannot be empty","errorType":"validation","errorClass":"InvalidKeyError","httpStatus":null,"severity":"error","filePath":"influxdb3_py_api/src/line_builder/line_builder.py","lineNumber":41,"sourceCode":"class InvalidLineError(InfluxDBError):\n    \"\"\"Raised when a line protocol string is invalid\"\"\"\n\n    pass\n\n\nclass LineBuilder:\n    def __init__(self, measurement: str):\n        if \" \" in measurement:\n            raise InvalidMeasurementError(\"Measurement name cannot contain spaces\")\n        self.measurement = measurement\n        self.tags: OrderedDict[str, str] = OrderedDict()\n        self.fields: OrderedDict[str, str] = OrderedDict()\n        self._timestamp_ns: Optional[int] = None\n\n    def _validate_key(self, key: str, key_type: str) -> None:\n        \"\"\"Validate that a key does not contain spaces, commas, or equals signs.\"\"\"\n        if not key:\n            raise InvalidKeyError(f\"{key_type} key cannot be empty\")\n        if \" \" in key:\n            raise InvalidKeyError(f\"{key_type} key '{key}' cannot contain spaces\")\n        if \",\" in key:\n            raise InvalidKeyError(f\"{key_type} key '{key}' cannot contain commas\")\n        if \"=\" in key:\n            raise InvalidKeyError(f\"{key_type} key '{key}' cannot contain equals signs\")\n\n    def _escape_measurement(self, value: str) -> str:\n        \"\"\"Escape characters in measurement names according to line protocol.\"\"\"\n        return value.replace(\",\", \"\\\\,\").replace(\" \", \"\\\\ \")\n\n    def _escape_tag_value(self, value: str) -> str:\n        \"\"\"Escape characters in tag values according to line protocol.\"\"\"\n        return (\n            value.replace(\"\\\\\", \"\\\\\\\\\")\n            .replace(\",\", \"\\\\,\")\n            .replace(\"=\", \"\\\\=\")\n            .replace(\" \", \"\\\\ \")","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/influxdata/influxdb/blob/d28e26e048401c53cbb98cf2d6ab0cf1e98048ca/influxdb3_py_api/src/line_builder/line_builder.py#L23-L59","documentation":"LineBuilder._validate_key raises InvalidKeyError with '{key_type} key cannot be empty' when tag() or any field method (uint64_field, int64_field, float64_field, string_field, bool_field) is called with an empty-string key. key_type is either 'tag' or 'field', telling you which call site failed. Empty identifiers cannot be represented in line protocol, so the builder rejects them before mutation.","triggerScenarios":"builder.tag('', 'host1') or builder.string_field('', 'value'). Typically the key expression evaluates to '' — a missing dict entry, a split() on malformed input yielding '', or a blank CSV column header used as a key.","commonSituations":"Dynamic key generation from data files (blank headers), parsing 'k=v' strings where the left side is missing, optional config keys that were never set but are passed through unconditionally.","solutions":["Check the key is a non-empty string before calling tag()/field methods","Fix the upstream producer of the key (missing dict key, blank header, failed split)","Skip or default the tag/field explicitly when the key would be empty instead of passing '' through"],"exampleFix":"# before\nbuilder.tag(col_name, col_value)  # col_name == '' -> InvalidKeyError\n\n# after\nif col_name:\n    builder.tag(col_name, str(col_value))","handlingStrategy":"validation","validationCode":"def add_tag(builder, key, value):\n    if not isinstance(key, str) or not key:\n        raise ValueError(f\"tag key missing or empty: {key!r}\")\n    builder.tag(key, str(value))","typeGuard":null,"tryCatchPattern":"from influxdb3_py_api.line_builder import InvalidKeyError\n\ntry:\n    builder.tag(key, value)\nexcept InvalidKeyError as e:\n    logger.warning(\"dropping bad tag: %s\", e)","preventionTips":["Validate keys are non-empty strings before every tag()/field call","Fix producers of blank keys (blank CSV headers, failed splits) at the source","Centralize key validation in one helper used by all plugin code"],"tags":["influxdb","line-protocol","python","tags","fields","validation"],"backgroundTag":"line-protocol-validation","analyzedSha":"d28e26e048401c53cbb98cf2d6ab0cf1e98048ca","analyzedAt":"2026-08-16T19:53:34.623Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}