{"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":"exception","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/06200ef96ba82c5f6727e5038a83af8e722c6875/influxdb3_py_api/src/line_builder/line_builder.py#L25-L61","documentation":"LineBuilder._validate_key rejects tag/field keys containing a space character, because spaces are line-protocol delimiters between the measurement/tags section and fields, and unescaped spaces in keys would produce malformed line protocol. The helper raises InvalidKeyError naming the key and key_type when a space is found.","triggerScenarios":"Calling lb.tag(\"my key\", \"v\") or any field setter with a key containing a space — commonly keys derived from human-readable labels, CSV headers, or user input with whitespace.","commonSituations":"Using display names ('host name') as tag keys, CSV column headers with spaces, or names produced by concatenation with spaces.","solutions":["Replace spaces in keys with underscores or strip/normalize whitespace before calling the setter.","Sanitize dynamic keys: key = key.replace(' ', '_').strip().","Fix upstream data sources (CSV headers, config) to emit space-free keys."],"exampleFix":"# before\nlb.tag(\"host name\", host)\n# after\nlb.tag(\"host_name\", host)","handlingStrategy":"validation","validationCode":"def sanitize_key(key: str) -> str:\n    return key.strip().replace(\" \", \"_\")\n\n# then: lb.tag(sanitize_key(k), v)","typeGuard":"def has_no_spaces(k) -> bool:\n    return isinstance(k, str) and \" \" not in k","tryCatchPattern":"try:\n    lb.tag(key, value)\nexcept InvalidKeyError:\n    lb.tag(key.replace(\" \", \"_\"), value)","preventionTips":["Normalize keys from human-readable sources (CSV headers, labels)","Use a shared sanitize function for all dynamic tag/field keys","Avoid concatenating names with spaces when building keys","Validate keys in CI with line-protocol round-trip tests"],"tags":["python","sdk","line-protocol","validation"],"backgroundTag":"invalid-argument-value","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"}