influxdata/influxdb · error · InvalidKeyError
key ' ' cannot contain commas
Error message
{key_type} key '{key}' cannot contain commas What it means
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.
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).
Example fix
# before
lb.tag(raw_key, value)
# after
lb.tag(raw_key.replace(',', '_'), value) Defensive patterns
Strategy: validation
Validate before calling
import re
def sanitize_key(key: str) -> str:
return re.sub(r"[, =]", "_", key.strip())
# then: lb.tag(sanitize_key(k), v) Type guard
def has_no_commas(k) -> bool:
return isinstance(k, str) and "," not in k Try / catch
try:
lb.tag(key, value)
except InvalidKeyError:
lb.tag(re.sub(r"[, =]", "_", key), value) Prevention
- 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
When it happens
Trigger: 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.
Common situations: Passing 'tag1,tag2' strings as a single key; metric names from systems that use comma-separated lists; unnormalized user input.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- key cannot be empty
- key ' ' cannot contain spaces
- Measurement name cannot contain spaces
- At least one field is required
- key ' ' cannot contain equals signs
AI-assisted analysis of influxdata/influxdb@06200ef96b (2026-09-19).
Data as JSON: /api/errors/2e7c9798a1f3754b.
Report an issue: GitHub.
Appendix: source
Thrown at influxdb3_py_api/src/line_builder/line_builder.py:45
class LineBuilder:
def __init__(self, measurement: str):
if " " in measurement:
raise InvalidMeasurementError("Measurement name cannot contain spaces")
self.measurement = measurement
self.tags: OrderedDict[str, str] = OrderedDict()
self.fields: OrderedDict[str, str] = OrderedDict()
self._timestamp_ns: Optional[int] = None
def _validate_key(self, key: str, key_type: str) -> None:
"""Validate that a key does not contain spaces, commas, or equals signs."""
if not key:
raise InvalidKeyError(f"{key_type} key cannot be empty")
if " " in key:
raise InvalidKeyError(f"{key_type} key '{key}' cannot contain spaces")
if "," in key:
raise InvalidKeyError(f"{key_type} key '{key}' cannot contain commas")
if "=" in key:
raise InvalidKeyError(f"{key_type} key '{key}' cannot contain equals signs")
def _escape_measurement(self, value: str) -> str:
"""Escape characters in measurement names according to line protocol."""
return value.replace(",", "\\,").replace(" ", "\\ ")
def _escape_tag_value(self, value: str) -> str:
"""Escape characters in tag values according to line protocol."""
return (
value.replace("\\", "\\\\")
.replace(",", "\\,")
.replace("=", "\\=")
.replace(" ", "\\ ")
)
def _escape_field_key(self, value: str) -> str:
"""Escape characters in field keys according to line protocol."""View on GitHub (pinned to 06200ef96b)