Implementation:Evidentlyai Evidently Text Length Descriptor
| Knowledge Sources | |
|---|---|
| Domains | Descriptors, Text Analysis, Data Monitoring |
| Last Updated | 2026-02-14 12:00 GMT |
Overview
Descriptor that computes the character length of text values in each row of a specified column, returning a numerical column suitable for monitoring and testing.
Description
The TextLength class is a Descriptor subclass that measures the length of text in a given column. For each row, it applies a helper function _apply that returns:
- 0 for None or NaN values
- len(value) for all other values
The result is a DatasetColumn of type ColumnType.Numerical, making it compatible with numerical metrics and tests.
The implementation converts the dataset to a DataFrame, applies the _apply function row-wise using pandas.Series.apply, and wraps the result in a DatasetColumn.
Usage
Use TextLength when you need to monitor or test the length of text content in your data. Common use cases include checking for empty responses, monitoring prompt/response lengths in LLM applications, or detecting anomalous text lengths in production data.
Code Reference
Source Location
- Repository: Evidentlyai_Evidently
- File: src/evidently/descriptors/_text_length.py
Signature
class TextLength(Descriptor):
column_name: str
def __init__(
self, column_name: str,
alias: Optional[str] = None,
tests: Optional[List[AnyDescriptorTest]] = None,
):
def generate_data(self, dataset: "Dataset", options: Options) -> Union[DatasetColumn, Dict[str, DatasetColumn]]:
def list_input_columns(self) -> Optional[List[str]]:
def _apply(value: Any):
Import
from evidently.descriptors._text_length import TextLength
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| column_name | str | Yes | Name of the text column to measure length for |
| alias | Optional[str] | No (default: "text_length") | Custom display name for the descriptor |
| tests | Optional[List[AnyDescriptorTest]] | No | Tests to apply to the computed text length values |
Outputs
| Name | Type | Description |
|---|---|---|
| return | DatasetColumn | Numerical column containing character counts per row; 0 for None/NaN values |
Usage Examples
from evidently.descriptors._text_length import TextLength
# Basic usage - measure length of a text column
descriptor = TextLength(column_name="response")
# With custom alias and tests
from evidently.core.datasets import gt
descriptor = TextLength(
column_name="response",
alias="response_length",
tests=[gt(0)], # ensure all responses are non-empty
)
# Add to dataset
dataset.add_descriptors([descriptor])
# Use in a report
from evidently import Report
from evidently.metrics import MeanValue
report = Report(metrics=[MeanValue("response_length")])
snapshot = report.run(dataset)
Related Pages
- Environment:Evidentlyai_Evidently_Python_Core_Environment
- Evidentlyai_Evidently_Custom_Descriptors - Custom descriptor classes for user-defined transformations
- Evidentlyai_Evidently_Context_Relevance - Context relevance descriptor for RAG evaluation