String tensors move through a model like any other tensor, and the string operators of the default domain (plus the Tokenizer of the com.microsoft domain that scikit-learn text pipelines export) are implemented. All of them are exact: they move, compare and count bytes and use no floating-point arithmetic beyond one float add per n-gram hit in TfIdfVectorizer.
How a string tensor travels
The tensor layer has no string type. A string tensor is one opaque rank-1 U8 packed value (PackedStrings; the layout is internal) that carries its own shape, so a node that touches one is shaped when it runs, not when the plan is made. TensorInfo.Kind is StringTensor; TensorInfo.HasStringShape and StringDims give the declared dimensions of a string input (the numeric Dims stay empty). Strings are UTF-8; bytes that are not valid UTF-8 read back as U+FFFD wherever the operator needs characters (case change, regular expressions, Tokenizer), and are moved unchanged by the data-movement operators.
A kernel says that it reads strings with OpKernel.AcceptsText. When a plan is made, every default-domain node with a string input that is not such a kernel is replaced by a text adapter (below) if it is one of the operators in the table, and otherwise refused with SessionErrorKind.UnsupportedOperator ("cannot take strings"), naming the operator.
Data movement and comparison on strings
These operators treat their data elements as opaque. On strings each runs its own numeric kernel on int32 tensors of element numbers, and the result is mapped back to strings, so broadcasting, negative axes, steps, index checks and error messages are the numeric operator's own, written once.
| Operator | Result |
|---|---|
| Reshape, Flatten, Squeeze, Unsqueeze, Concat, Gather, GatherElements, GatherND, Slice, Split, Transpose, Expand, Tile, Where, ScatterND, ScatterElements, Compress, ReverseSequence, DepthToSpace, SpaceToDepth, Identity | strings |
| Equal (byte-for-byte), Shape, Size | booleans, int64 |
| Cast to or from string | see below |
Not accepted on strings (refused when the session is created): CastLike, Pad, Resize, Unique, and every arithmetic or activation operator.
Cast between strings and numbers
Observed on ONNX Runtime 1.31.1 and matched:
- number to string uses
%.8gfor float32/float16/bfloat16 (via float) and the shortest exact form for integers; NaN isNaN, infinities areINFand-INF, negative zero is-0, booleans are1and0; - string to float is
strtod-like: leading white space, the longest numeric prefix ("1,5"is 1), hexadecimal floats,inf,infinityandnanin any case; a value that overflows double, or no digits at all, is an error; - string to a signed integer is
strtoll-like ("3.7"is 3,"1e3"is 1) and wraps when narrowed to int8, int16 or int32; to an unsigned integer or boolean it isstrtoull(a negative value wraps); overflow or no digits is an error.
Operators
| Operator | Opsets | Notes |
|---|---|---|
| StringConcat | 20+ | NumPy broadcasting; scalars allowed. |
| StringNormalizer | 10+ | case_change_action NONE/LOWER/UPPER (invariant culture), stopwords, is_case_sensitive. Input [C] or [1, C]; a result with every element dropped is one empty string. ONNX Runtime refuses a 0-d input and so do we. |
| StringSplit | 20+ | Empty delimiter splits on runs of the space character only (tabs and newlines stay inside a piece); leading and trailing spaces are dropped, also from the remainder left by maxsplit. An empty string has 0 pieces. Rows are padded with empty strings to the longest. |
| RegexFullMatch | 20+ | RE2 syntax, see below. |
| TfIdfVectorizer | 9+ | int32, int64 and string inputs of rank 1 or 2; modes TF, IDF, TFIDF; max_skip_count; the pool may hold n-grams longer than max_gram_length, which are never matched; the output has max(ngram_indexes) + 1 columns. IDF counts each n-gram at most once per row. A zero-width row ([N, 0]) is an error here; ONNX Runtime aborts its process on it. |
Tokenizer (com.microsoft) | 1 | separators or tokenexp (exactly one), mark, mincharnum (at least 1, counted in code points), pad_value. Input rank 1 or 2; an empty input keeps its shape; rows with no tokens pad to the widest row (0 columns when every row is empty and mark is 0). Matches are leftmost-longest. Separators are regular-expression alternatives; a single empty separator splits into characters. |
Regular expressions
RegexFullMatch and Tokenizer take RE2 syntax. They run on .NET's RegexOptions.NonBacktracking engine (linear time in the input, no catastrophic backtracking; AOT, trimming and WebAssembly clean) after a translation that maps RE2 spellings to .NET's. Where the two differ, we follow RE2:
$matches only at the end of the text (not before a final newline) unless(?m)is on.\d,\w,\sand\bare ASCII classes, as in RE2.- POSIX classes
[[:alpha:]]and the Unicode general categories\p{L},\pN,\p{Lu}... are supported; scripts (\p{Greek}) are not. - Strings are matched as UTF-16 after UTF-8 decoding, so a code point above U+FFFF is one match unit of two characters; offsets never reach the output.
Refused when the session is created (a typed SessionException naming the operator and the pattern): look-ahead and look-behind, back-references, atomic groups, \Z, \C, unsupported flags, a repeat count above 1000, a pattern longer than 16384 characters, and anything .NET cannot compile.
Tokenizer and the leftmost-longest rule: the .NET engine returns the first alternative that matches (a|ab finds a), ONNX Runtime the longest (ab). After each match the tokenizer looks for a longer match at the same start, up to 1024 characters further (patterns with no alternation, group or optional part have one candidate and skip this). A token decided by alternation order and longer than 1024 characters is cut there; \b and $ at the end of a longer candidate see the end of the candidate, not of the text.
What ONNX Runtime does differently, and what we follow
- Gather along axis 0 of a string tensor of rank 3 or more returns empty strings for all but the first element of each slice in ONNX Runtime 1.31.1. We return the whole slice, as the specification says (the oracle case is left out of the data and a test pins our result).
- Concat of a rank-2 string tensor with a rank-1 constant, ScatterElements with
indicesandupdatesof different rank, Cast to bfloat16 are refused by ONNX Runtime; they have no oracle case. - Tokenizer
mincharnumbelow 1 is refused by ONNX Runtime ("must have a positive value") and by us. - StringSplit and Tokenizer behaviours above were found by running crafted models.
Checks
tools/ml_oracle_strings.py runs 295 cases through ONNX Runtime 1.31.1 (testdata/ops-ml/strings.json.gz): every operator above with edge inputs (empty strings, empty tensors, scalars, UTF-8, rank 3), the data-movement operators on strings, Cast in both directions for every numeric type, and CountVectorizer/TfidfVectorizer exports from skl2onnx. StringOracleTests runs each case on every kernel path in both modes at one and four threads: strings, booleans and integers must be equal and results bit-identical across paths. TextHostileTests covers refused attributes and a pattern that would take exponential time in a backtracking engine. The backend cases for these operators (test_string_*, test_strnorm_*, test_regex_*, test_tfidfvectorizer_*, test_equal_string*) pass in both modes; the table is conformance.md.