{"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":"exception","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/06200ef96ba82c5f6727e5038a83af8e722c6875/influxdb3_py_api/src/line_builder/line_builder.py#L27-L63","documentation":"LineBuilder._validate_key rejects tag/field keys containing a comma, since commas separate tags in line protocol and a raw comma in a key would break point parsing. InvalidKeyError names the offending key and key_type when a comma is present.","triggerScenarios":"Calling lb.tag(\"a,b\", \"v\") or any field setter with a key containing ',' — typically keys parsed from comma-separated input where the split was not performed, or raw sensor/channel identifiers embedding commas.","commonSituations":"Passing 'tag1,tag2' strings as a single key; metric names from systems that use comma-separated lists; unnormalized user input.","solutions":["Strip or replace commas in keys before passing them to LineBuilder (key = key.replace(',', '')).","Properly split comma-separated key lists and add each key individually.","Sanitize dynamic/external keys with a regex removing line-protocol delimiters (, = and spaces)."],"exampleFix":"# before\nlb.tag(raw_key, value)\n# after\nlb.tag(raw_key.replace(',', '_'), value)","handlingStrategy":"validation","validationCode":"import re\ndef sanitize_key(key: str) -> str:\n    return re.sub(r\"[, =]\", \"_\", key.strip())\n\n# then: lb.tag(sanitize_key(k), v)","typeGuard":"def has_no_commas(k) -> bool:\n    return isinstance(k, str) and \",\" not in k","tryCatchPattern":"try:\n    lb.tag(key, value)\nexcept InvalidKeyError:\n    lb.tag(re.sub(r\"[, =]\", \"_\", key), value)","preventionTips":["Split comma-separated key lists before adding tags","Strip line-protocol delimiters (, = and spaces) from external keys","Use a single regex-based sanitizer for all keys","Test dynamic keys against line protocol parsing"],"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"}