Validate UTF-8 Byte Limits Before Storing Text

Last updated: December 3, 2026.

Check the UTF-8 byte length if a storage field rejects text that appears short enough. Milvus defines VARCHAR max_length in bytes. A character count may be lower than the encoded size.

<?php

function fitsUtf8Field(string $text, int $maxBytes): bool
{
    if ($maxBytes < 1) {
        throw new InvalidArgumentException('Expected a positive byte limit');
    }

    return mb_check_encoding($text, 'UTF-8')
        && strlen($text) <= $maxBytes;
}

var_export(fitsUtf8Field('Café', 4)); // false: 5 bytes
var_export(fitsUtf8Field('Café', 5)); // true

Measure the value that will actually be stored

The letter é in the example uses two bytes in UTF-8, although a character counter counts one. strlen() measures bytes, while mb_check_encoding() rejects malformed UTF-8. The example requires the PHP mbstring extension.

Measure after any normalization, conversion or concatenation. A counting step that runs before adding a prefix or joining chunks can understate the final size. Validate each field separately in a batch, and report the field name, measured byte count and configured limit when an entry fails.

Reject or shorten according to the data contract

Reject an oversized value when its full content matters. If the field is only a preview, mb_strcut($text, 0, $maxBytes, "UTF-8") keeps the result within the byte budget without splitting an encoded code point. It may still separate a combining mark or part of an emoji sequence. If preserving complete displayed characters is important, use grapheme boundaries and keep measuring the result in bytes.

Byte limits vary by system and field type. Check the target schema instead of applying Milvus rules to every SQL VARCHAR. Test an ASCII value, an accented value, an emoji and values that land exactly on either side of the limit.

Documentation

Related guides

Sergey Kornilov

Sergey Kornilov