An API's character limit must name its unit. UTF-8 bytes, Unicode code points, and grapheme clusters answer different questions, so one string can satisfy a user-interface limit while exceeding a storage or transport limit. Define the user-facing rule and the technical boundary separately, then validate each without silently corrupting the text.
“Maximum 20 characters” is incomplete when the client, server, and database do not count the same thing.
Count one visible example in more than one way
Consider an accented letter represented in two forms:
| Representation | Python string length | UTF-8 byte length |
|---|---|---|
\u00e9 |
1 | 2 |
e\u0301 |
2 | 3 |
Both commonly display as an accented e, but their underlying sequences differ. Four local checks using Python 3.14.2 and its Unicode 16.0.0 database confirmed these two measurements, NFC equivalence, and the failure of a one-byte UTF-8 slice to decode. No API server or database integration was tested.
The small example can be inspected directly:
import unicodedata
precomposed = "\u00e9"
decomposed = "e\u0301"
assert len(precomposed) == 1
assert len(decomposed) == 2
assert unicodedata.normalize("NFC", decomposed) == precomposed
The lengths above describe Python strings, not a universal result of every language's length operation. Verify what the chosen runtime counts before using it as an API contract.
Choose a user-facing unit deliberately
Unicode Standard Annex #29 defines default text-segmentation rules, including extended grapheme clusters. They provide a useful basis for many user-perceived character operations, with documented limits and possible tailoring. A rendered glyph, a grapheme cluster, and a code point are not interchangeable objects.
An emoji sequence can contain multiple code points while behaving like one editing unit. A UI that truncates at an arbitrary code-point position may split a sequence that its user sees as one character.
Use a maintained segmentation implementation appropriate to the product's supported Unicode version and language behavior. The local Python fixture did not implement or test full grapheme segmentation. Counting code points and calling the result “graphemes” would conceal the missing operation.
Treat normalization as part of the data contract
Unicode Standard Annex #15 distinguishes canonical and compatibility normalization. NFC can make canonically equivalent forms comparable under an explicit policy, but normalization is not a general cure for visual ambiguity or identifier security.
Decide where normalization occurs and which fields allow it. A display name, an opaque signed payload, and a password do not automatically share a transformation policy. Changing bytes after signing or storing an identifier can break a contract that depends on the original sequence.
For a normalized display-name field, a possible validation order is: decode valid input, apply the field's declared normalization, check the user-facing length, then check its encoded storage budget. This is a proposed field policy, not a rule to apply to every string in a service.
Reject or truncate at a boundary you can explain
If storage allows at most a fixed number of UTF-8 bytes, cutting raw bytes can leave invalid encoding. Cutting code points can preserve encoding while still splitting a grapheme cluster. An ellipsis also consumes part of the chosen budget.
Prefer an explicit validation error when silent truncation would change an identifier or user-authored meaning. If a preview is intentionally shortened, preserve the original and use a documented segmentation and byte-budget policy for the preview.
Our API error guide explains stable client-facing failure fields. A useful length error states the affected field and the relevant rule without echoing sensitive input or telling the client merely that the string is “invalid.”
Verify the same rule across components
Keep fixtures containing precomposed and decomposed text, combining marks, emoji sequences, empty strings, and inputs at each boundary. Compare client validation, server acceptance, database behavior, and any serialized response under the versions actually deployed.
The four local checks demonstrate specific representation differences. They do not certify a complete international-text policy. The practical goal is a limit whose unit, transformation, and failure behavior agree across the system and can be explained to the person entering the text.