Skip to content

Text Utils

Utility functions for text processing in Gen AI applications.

format_size(size_bytes)

Format a byte count for a size-limit message.

Megabytes are rounded up to one decimal place so a value just over a limit does not appear to equal that limit.

Parameters:

Name Type Description Default
size_bytes int

Number of bytes.

required

Returns:

Name Type Description
str str

A megabyte value for sizes of at least 1 MB, or a byte count for smaller sizes.

join_with_or(names)

Joins names into an English list that ends with "or".

Parameters:

Name Type Description Default
names Iterable[str]

The names to join.

required

Returns:

Name Type Description
str str

a, a or b, or a, b, or c.

normalize_text(text)

Normalizes line endings to a single newline and strips outer whitespace.

CRLF and CR line endings are converted to a single newline. Internal whitespace, blank lines, and Unicode are preserved.

Parameters:

Name Type Description Default
text str

The text to normalize.

required

Returns:

Name Type Description
str str

The normalized text.