LlmNormalizeText Function  
 
INT WINAPI LlmNormalizeText(
  LPCTSTR lpszInput,  
  INT cchInput,  
  LPTSTR lpszOutput,  
  INT nMaxLength  
);

The LlmNormalizeText function normalizes message text by removing extraneous whitespace and applying consistent formatting.

Parameters

lpszInput
A pointer to a null-terminated string which specifies the text which should be normalized. This parameter cannot be NULL.
cchInput
An integer which specifies the length of the input string. If this value is -1 the entire input string up to the terminating null character will be normalized.
lpszOutput
A pointer to a string buffer which will contain the normalized text when the function returns. The output string will always be null-terminated. If the string buffer is not large enough to contain all of the normalized text, the function will fail and the last error code will be set to ST_ERROR_BUFFER_TOO_SMALL.
nMaxLength
An integer which specifies the maximum number of characters which can be copied into the output string buffer. This value must be large enough to include a terminating null character. If this value is too small, or an invalid length is specified, the function will fail.

Return Value

If the function succeeds, the return value is the number of characters copied to the output buffer, not including the terminating null character. If the function fails, the return value is zero and the LlmGetLastError function will return a non-zero value.

Remarks

The LlmNormalizeText function processes the input string, decomposing Unicode characters to their canonical equivalents, replacing confusable homoglyphs with normalized characters, removing combining characters, normalizing end-of-line characters, and trimming leading and trailing whitespace including non-breaking spaces and other special Unicode space characters. Although removing unnecessary whitespace may slightly reduce token usage, the primary benefit is producing cleaner and more consistent input for the model to process.

Line break characters are normalized to use Windows end-of-line conventions. Isolated carriage return or line feed characters, Unicode line and paragraph separators, and form feed characters are converted to CRLF sequences. Multiple consecutive line breaks are reduced to improve consistency.

Normalization can be useful when comparing prompts, checking for empty or whitespace-only input, or applying simple validation rules before a request is submitted. It does not attempt to determine whether the text is safe, appropriate, or semantically valid. Applications should perform any additional validation required for their own use case.

Using this function is optional. Prompt text does not need to be normalized before it is sent to a provider, and using this function is not a required sanitization step. If preserving the exact formatting of the original text is important, such as indentation, tables, or code samples, the application should send the original text instead.

The ANSI version of this function expects the input to be UTF-8 encoded, but will fall back to using the current active code page if invalid UTF-8 characters are encountered in the string. The output from this function will always be valid Unicode text. For applications using the ANSI version of this function, this means the output string will use UTF-8 encoding.

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csllmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also

LlmEstimateTokenCount, LlmSendMessage, LlmSendMessageEx