Classify raw text with text.onnx (in the zip). The graph runs the fitted TfidfVectorizer for you.
The zip includes export.py; see how to recreate the model.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel; using var model = Model.Load("text.onnx"); using var inputs = new ModelInputs(); inputs.Add("text", ["invoice charge"]); using var result = model.Run(inputs); Console.WriteLine( result.Strings("output_label")[0]);
The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.
If you’re pasting the snippet into a new project, copy text.onnx from the zip into that project folder before running it.
Prints:
billingFull program
using RecursiveAI.OneModel; using var model = Model.Load("text.onnx"); using var inputs = new ModelInputs(); inputs.Add("text", ["invoice charge"]); using var result = model.Run(inputs); var label = result.Strings("output_label")[0]; var chance = result.StringMaps( "output_probability")[0][label]; Console.WriteLine(FormattableString.Invariant( $"{label}: {chance:F4}"));
dotnet run prints:
billing: 0.6268The two labels are account and billing. The sample was fitted on six made-up messages such as reset password and pay invoice. The full program sends invoice charge and prints its label and the probability for that label.
If you read output_label with Int64s, OneModel throws SessionException; this model’s labels are strings.
Variations
To score two messages at once, replace the inputs.Add line. The strings are two separate rows, not two words in a single row.
inputs.Add("text", ["invoice charge", "reset login"]);
The two labels, in order, are billing and account.
Related recipes
ONNX with multiple inputs and outputs · Run a scikit-learn random forest with ONNX · Run an ONNX ColumnTransformer
The short answer
Export the fitted TfidfVectorizer and classifier together. Pass a string tensor named text to OneModel and read the predicted label from output_label. Tokenization and weighting happen inside the ONNX graph, so C# receives and passes along the original message rather than constructing a feature vector.
How do I convert a scikit-learn TF-IDF pipeline to ONNX?
Fit the vectorizer and the classifier as one scikit-learn pipeline. Then declare a string input to skl2onnx. This example uses one string per record and leaves the batch size open:
To recreate the model, install the export dependencies in a Python environment:
python -m pip install scikit-learn==1.9.1 python -m pip install skl2onnx==1.20.0 python -m pip install numpy==2.5.5 onnx==1.23.2
Save this as export.py and run python export.py in the project folder. It replaces the sample .onnx file. You don’t need Python to run the C# example with the supplied model.
from sklearn.feature_extraction import text from sklearn.linear_model import LogisticRegression from sklearn.pipeline import make_pipeline from skl2onnx import to_onnx from skl2onnx.common import data_types messages = [ "reset password", "account login", "password expired", "pay invoice", "invoice refund", "billing charge", ] labels = ["account"] * 3 + ["billing"] * 3 classifier = make_pipeline( text.TfidfVectorizer(lowercase=False), LogisticRegression(), ).fit(messages, labels) inputs = [("text", data_types.StringTensorType( [None, 1] ))] graph = to_onnx( classifier, initial_types=inputs, target_opset=17 ) with open("text.onnx", "wb") as file: file.write(graph.SerializeToString())
Exporting only the logistic regression step would leave you with a numeric input and no fitted text preprocessing. The skl2onnx TF-IDF example also shows why the tokenizer matters: changing which characters form tokens can change predictions even with identical classifier weights.
How does ONNX tokenize raw text for TF-IDF?
The sample TfidfVectorizer(lowercase=False) was fitted on lowercase messages, so it doesn’t add a lowercase conversion. In the exported graph, a Tokenizer splits the text into tokens; the TF-IDF step uses the fitted vocabulary and weights. In TF-IDF, terms that appear in many training documents generally carry less weight than terms that are more distinctive. Don’t split the message into a list of words or lowercase it in C# unless that was a separate step before training. Confirm that predictions after export agree with Python scikit-learn on representative text, especially punctuation and mixed case. See the OneModel string operators reference for the supported tokenizer rules.
How do I get text labels and their probabilities from ONNX?
This model was trained on string labels, so Strings("output_label") gives one string per message. The default skl2onnx export returns probabilities as a map per record. Use StringMaps("output_probability") and look up the predicted label in the map for the same record. The batch in the variation sends the messages as two rows of [2, 1]; it doesn’t join them into one document. If you train on integer labels instead, use Int64s and Int64Maps for a corresponding default export.