{"record":{"id":"8d8e122418c83885","repo":"influxdata/influxdb","slug":"key-type-key-key-cannot-contain-spaces","errorCode":null,"errorMessage":"{key_type} key '{key}' cannot contain spaces","messagePattern":"(.+?) key '(.+?)' cannot contain spaces","errorType":"validation","errorClass":"InvalidKeyError","httpStatus":null,"severity":"error","filePath":"influxdb3_py_api/src/line_builder/line_builder.py","lineNumber":43,"sourceCode":"\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(\" \", \"\\\\ \")\n        )\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/influxdata/influxdb/blob/d28e26e048401c53cbb98cf2d6ab0cf1e98048ca/influxdb3_py_api/src/line_builder/line_builder.py#L25-L61","documentation":"LineBuilder._validate_key raises InvalidKeyError when a tag or field key contains a space. Spaces are structural separators in line protocol (measurement,tags<space>fields<space>timestamp), so a raw space inside an identifier would shift the parse; the builder rejects it rather than silently escaping keys. Tag and field keys must be space-free.","triggerScenarios":"builder.tag('server name', 'host1') or builder.float64_field('mem used', 0.64) — any tag()/field call whose key contains ' '. The offending key is echoed in the message.","commonSituations":"Keys derived from human-readable labels ('cpu usage'), CSV/JSON property names with spaces, or template strings where a variable expanded with surrounding whitespace ('{name} ')","solutions":["Rename the key: replace spaces with underscores (snake_case) at ingest","Sanitize all dynamic keys with a single normalize function before feeding the builder","If renaming is impossible, log and skip the offending tag/field rather than letting the whole build fail"],"exampleFix":"# before\nbuilder.tag(\"server name\", \"host1\")  # InvalidKeyError\n\n# after\nbuilder.tag(\"server_name\", \"host1\")","handlingStrategy":"validation","validationCode":"def valid_lp_key(k: str) -> bool:\n    return bool(k) and not any(c in k for c in \" ,=\")\n\nif valid_lp_key(tag_key):\n    builder.tag(tag_key, tag_value)","typeGuard":null,"tryCatchPattern":"from influxdb3_py_api.line_builder import InvalidKeyError\n\ntry:\n    builder.tag(tag_key, tag_value)\nexcept InvalidKeyError as e:\n    logger.warning(\"renaming/skipping key: %s\", e)","preventionTips":["Standardize on snake_case identifiers for tags and fields across the codebase","Run dynamic keys through a single sanitizer that strips ' ', ',', '='","Test with realistic column/property names containing spaces"],"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"}