{"record":{"id":"2e7c9798a1f3754b","repo":"influxdata/influxdb","slug":"key-type-key-key-cannot-contain-commas","errorCode":null,"errorMessage":"{key_type} key '{key}' cannot contain commas","messagePattern":"(.+?) key '(.+?)' cannot contain commas","errorType":"validation","errorClass":"InvalidKeyError","httpStatus":null,"severity":"error","filePath":"influxdb3_py_api/src/line_builder/line_builder.py","lineNumber":45,"sourceCode":"\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\n    def _escape_field_key(self, value: str) -> str:\n        \"\"\"Escape characters in field keys according to line protocol.\"\"\"","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/influxdata/influxdb/blob/d28e26e048401c53cbb98cf2d6ab0cf1e98048ca/influxdb3_py_api/src/line_builder/line_builder.py#L27-L63","documentation":"LineBuilder._validate_key raises InvalidKeyError when a tag or field key contains a comma. Commas separate the measurement from tag pairs and tag pairs from each other in line protocol, so a comma in an identifier would split the element; the builder rejects it. Note this applies to keys only — tag values are auto-escaped, and keys are deliberately not escaped for you.","triggerScenarios":"builder.tag('region,zone', 'us') or builder.string_field('a,b', 'x') — often caused by mistakenly using a joined string (','.join([...])) as a key instead of as a value, or embedding list output into a key name.","commonSituations":"Composing composite keys by joining values with commas; keys built from f-strings over lists ('tags={my_list}'); data copied from CSV where a column name contains a comma.","solutions":["Use a different separator when composing key names (underscore or dash)","If you meant to store a comma-separated list, put it in the tag VALUE (auto-escaped) or a string field, not the key","Add a key sanitizer that strips/replaces ',', ' ', '=' in one pass"],"exampleFix":"# before\nbuilder.tag(\",\".join([\"region\", \"zone\"]), \"us\")  # InvalidKeyError\n\n# after\nbuilder.tag(\"region_zone\", \"us\")  # or store the list as a value:\nbuilder.tag(\"location\", \",\".join([\"region\", \"zone\"]))  # value is escaped","handlingStrategy":"validation","validationCode":"def normalize_key(k: str) -> str:\n    return k.replace(\",\", \"_\").replace(\" \", \"_\").replace(\"=\", \"_\")\n\nbuilder.tag(normalize_key(raw_key), value)","typeGuard":null,"tryCatchPattern":"from influxdb3_py_api.line_builder import InvalidKeyError\n\ntry:\n    builder.tag(raw_key, value)\nexcept InvalidKeyError as e:\n    logger.warning(\"bad key %r: %s\", raw_key, e)","preventionTips":["Never join lists into identifiers with ',' — use '_' or '-'","Store comma-separated data in tag values (auto-escaped), not keys","Lint plugin code for identifiers built from joined data"],"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"}