To use libpostal in C#, install the NHSISL.LibPostalClient binding, which needs a native libpostal library and a model download at build time; this guide shows how to move to OneContact.
OneContact is a free, open-source .NET contact-data library from Recursive AI, licensed MIT or Apache-2.0. NuGet · GitHub.
dotnet add package RecursiveAI.OneContact --version 1.1.0In a .NET 10 console project, replace Program.cs and run dotnet run.
using System.Buffers; using RecursiveAI.OneContact; using RecursiveAI.OneContact.Addresses.Expansion; const string input = "1 Main St Apt 4, Springfield, VA 22162"; var status = PostalAddress.TryParse(input.AsSpan(), Region.US, out var address, out var detail); Console.WriteLine($"Parse: {status}"); if (status == OperationStatus.Done) { Console.WriteLine($"Street: {string.Join(" / ", address.StreetLines)}"); Console.WriteLine($"Locality: {address.Locality}"); Console.WriteLine($"Admin area: {address.AdminArea}"); Console.WriteLine($"Postal code: {address.PostalCode}"); Console.WriteLine($"Country: {address.Country.Code}"); } else { Console.WriteLine($"Reason: {detail.PrimaryReason}"); } foreach (string variant in AddressExpander.Expand(input, ExpansionLanguages.English, maxVariants: 4)) Console.WriteLine(variant); var noCountry = PostalAddress.TryParse("1 Main St".AsSpan(), out _, out var noCountryDetail); Console.WriteLine($"Without country: {noCountry} ({noCountryDetail.PrimaryReason})");
Parse: Done Street: 1 MAIN ST APT 4 Locality: Springfield Admin area: VA Postal code: 22162 Country: US 1 main street apartment 4 springfield vale 22162 1 main street apartment 4 springfield va 22162 1 main street apt 4 springfield vale 22162 1 main st apt 4 springfield va 22162 Without country: InvalidData (address.no-last-line)
“Vale” appears because the English expansion dictionary also reads VA as an abbreviation for “vale.”
OneContact returns typed fields rather than libpostal labels. Its rules read first and refuse input they don’t recognize; a model package reads what they leave open (below). The example supplies a known country to the parser and chooses English separately for expansion. See the OneContact 1.1.0 lists of regions with free-text parsing and expansion languages.
Add a model package for what the rules leave open
libpostal reads every address with its trained model. OneContact reads with rules first, and from 1.1 a trained model package reads what they leave open. In the README’s words: “Reference one model package next to RecursiveAI.OneContact and nothing else changes in your code: the package registers itself, the parsers ask it only for what the rules leave open, and a model never overrules a rule hit.”
Without commas, the rules can’t always tell where the street ends and the city starts, so they refuse the text with address.model-required:
using System.Buffers; using RecursiveAI.OneContact; const string input = "123 Main St Fort Worth TX 76102"; var status = PostalAddress.TryParse(input.AsSpan(), Region.US, out var address, out var detail); Console.WriteLine($"Parse: {status}"); if (status == OperationStatus.Done) { Console.WriteLine($"Street: {string.Join(" / ", address.StreetLines)}"); Console.WriteLine($"Locality: {address.Locality}"); Console.WriteLine($"Admin area: {address.AdminArea}"); Console.WriteLine($"Postal code: {address.PostalCode}"); foreach (var field in detail.Fields) Console.WriteLine($" {field.Name}: {field.Source}"); } else { Console.WriteLine($"Reason: {detail.PrimaryReason}"); }
Parse: InvalidData Reason: address.model-required
Add the model package and run the same program:
dotnet add package RecursiveAI.OneContact.Models --version 1.1.0Parse: Done Street: 123 MAIN ST Locality: Fort Worth Admin area: TX Postal code: 76102 street.house-number: Model street.name: Model street.type: Model locality: Model admin-area: Model postal-code: Model
ParseDetail reports every field as the model’s here. The first program on this page prints the same with or without the package: the rules read that address, and “1 Main St” is still refused with address.no-last-line. RecursiveAI.OneContact.Models.Tier1 covers the United States and Canada; RecursiveAI.OneContact.Models covers those and Mexico, Australia, the United Kingdom and the Latin-script countries of Europe. The weights are inside the package, so there is no data directory to download. The README lists the packages at 9.43 MB and 21.2 MB, and libpostal at “about 2 GB of data.” In the migration reference’s words: “Not equivalent: libpostal’s training data and models are not used or read.”
What the README measured against libpostal
From the README: “What the model packages (RecursiveAI.OneContact.Models.Tier1 and .Models) add to the rules, measured on frozen pages of real websites (Common Crawl, each item checked and labelled by a reviewer; the sets are private, only aggregates are published). The rules read first and the model only decides what they leave open. The tables show the .Models package”.
“Address parsing, field F1 on the coarse labels, against the better of libpostal 1.1.4 and Senzing’s model 1.2.0 (the reference runs with and without the country’s name added to its input). Where the interval of the difference includes zero the row is within noise; the sets are small, so read the item counts.”
| Country | Items | Ours, country given | Best of libpostal 1.1.4 and Senzing 1.2.0 | Difference [95% CI] | Ours, no country | Difference [95% CI] |
|---|---|---|---|---|---|---|
| AD | 46 | 78.9 | 83.0 | -4.1 [-10.5, +1.6] | 79.9 | -2.4 [-7.8, +2.8] |
| AT | 142 | 91.3 | 91.4 | -0.0 [-2.6, +2.4] | 91.0 | -0.4 [-3.2, +2.4] |
| AU | 123 | 96.0 | 94.9 | +1.1 [-1.4, +3.3] | 94.5 | -0.4 [-3.7, +2.4] |
| BE | 112 | 93.5 | 92.9 | +0.7 [-2.2, +3.6] | 92.2 | -0.7 [-3.5, +2.3] |
| CA | 244 | 94.9 | 93.8 | +1.2 [-0.5, +2.8] | 94.9 | +1.4 [-0.3, +3.1] |
| CH | 122 | 94.5 | 91.8 | +2.6 [+0.1, +5.1] | 93.7 | +2.7 [-0.4, +5.7] |
| CZ | 131 | 96.7 | 93.2 | +3.5 [+1.6, +5.7] | 96.4 | +3.1 [+1.3, +5.4] |
| DE | 123 | 92.0 | 88.0 | +4.0 [+1.1, +7.2] | 91.3 | +3.2 [+0.2, +6.5] |
| DK | 138 | 96.7 | 91.5 | +5.2 [+2.3, +8.2] | 96.6 | +6.3 [+3.5, +9.1] |
| EE | 170 | 92.3 | 86.9 | +5.3 [+2.7, +8.3] | 91.7 | +5.1 [+2.4, +8.2] |
| ES | 132 | 88.1 | 82.8 | +5.3 [+1.7, +9.1] | 82.2 | -0.6 [-3.9, +3.0] |
| FI | 103 | 96.3 | 88.8 | +7.4 [+3.8, +11.3] | 94.6 | +7.5 [+3.9, +11.8] |
| FR | 101 | 86.2 | 80.9 | +5.3 [+0.7, +9.7] | 85.6 | +4.7 [-0.1, +9.3] |
| GB | 138 | 85.9 | 87.0 | -1.1 [-4.3, +2.3] | 85.9 | -1.1 [-4.3, +2.3] |
| HR | 133 | 95.4 | 93.8 | +1.6 [-0.6, +3.6] | 95.3 | +1.5 [-0.6, +3.5] |
| HU | 103 | 92.4 | 91.4 | +1.0 [-2.2, +4.3] | 91.6 | +0.2 [-3.4, +3.8] |
| IE | 117 | 78.7 | 65.4 | +13.3 [+8.5, +18.3] | 76.2 | +10.8 [+5.3, +16.2] |
| IM | 25 | 87.5 | 91.9 | -4.4 [-10.9, +0.9] | 85.3 | -6.6 [-14.0, +0.0] |
| IS | 40 | 91.3 | 92.4 | -1.1 [-6.8, +5.1] | 91.3 | -0.8 [-6.8, +5.8] |
| IT | 60 | 96.0 | 89.3 | +6.7 [+3.3, +10.4] | 92.9 | +3.5 [-1.1, +7.8] |
| LI | 117 | 95.1 | 92.4 | +2.6 [-0.2, +5.7] | 95.1 | +2.6 [-0.2, +5.7] |
| LT | 19 | 91.6 | 89.8 | +1.8 [-2.6, +7.1] | 86.8 | -1.8 [-11.5, +7.9] |
| LU | 92 | 91.2 | 85.2 | +6.0 [+1.7, +10.4] | 92.0 | +6.8 [+2.5, +10.9] |
| LV | 66 | 93.6 | 89.7 | +3.9 [+0.3, +8.0] | 93.6 | +3.9 [+0.3, +8.0] |
| MC | 38 | 87.3 | 78.1 | +9.2 [+1.7, +17.3] | 87.3 | +9.2 [+1.7, +17.3] |
| MT | 49 | 75.1 | 42.7 | +32.3 [+22.2, +43.4] | 78.5 | +37.5 [+27.9, +48.6] |
| MX | 144 | 78.1 | 66.3 | +11.8 [+7.1, +16.4] | 77.2 | +10.9 [+6.3, +15.7] |
| NL | 117 | 97.6 | 58.2 | +39.4 [+34.9, +43.6] | 97.6 | +41.5 [+37.1, +46.0] |
| NO | 85 | 93.4 | 94.8 | -1.4 [-4.3, +1.7] | 92.2 | -1.2 [-4.7, +2.6] |
| PL | 128 | 97.9 | 95.5 | +2.4 [+0.5, +4.4] | 97.9 | +2.4 [+0.5, +4.4] |
| PT | 55 | 79.2 | 73.8 | +5.4 [-1.1, +11.5] | 79.2 | +5.4 [-1.1, +11.5] |
| RO | 141 | 73.3 | 68.2 | +5.1 [+0.5, +9.7] | 73.0 | +5.5 [+0.6, +10.2] |
| SE | 49 | 97.1 | 96.1 | +1.0 [-2.2, +5.1] | 95.6 | -0.1 [-4.0, +4.5] |
| SI | 114 | 96.6 | 95.7 | +0.9 [-0.7, +2.6] | 97.0 | +2.2 [+0.3, +4.5] |
| SK | 134 | 95.6 | 94.2 | +1.4 [+0.1, +3.0] | 95.4 | +1.8 [-0.2, +4.2] |
| SM | 61 | 85.7 | 75.6 | +10.1 [+2.9, +16.5] | 86.1 | +10.6 [+3.8, +16.8] |
| US | 250 | 98.2 | 98.4 | -0.2 [-1.2, +0.8] | 98.2 | -0.2 [-1.2, +0.8] |
“Ours” is OneContact with RecursiveAI.OneContact.Models. The Tier 1 package’s rows and the method are in the maintainers’ measurements.
The same address through the libpostal C# binding
In a separate .NET 10 console project, install NHSISL.LibPostalClient 1.0.0.10. The binding’s setup guide describes the model download on first build and the Data folder. This example ran on Linux x64. PaserOptions is the binding’s documented spelling.
dotnet add package NHSISL.LibPostalClient --version 1.0.0.10using NHSISL.LibPostalClient.Clients; using NHSISL.LibPostalClient.Models.Brokers.LibPostal; var dataDirectory = Path.Combine(AppContext.BaseDirectory, "Data"); var config = new LibPostalConfiguration { DataDirectory = dataDirectory, ParserDataDirectory = dataDirectory, LanguageClassifierDataDirectory = dataDirectory, PaserOptions = new ParserOptions { Country = "us", Language = "en", } }; var client = new LibPostalClient(config); List<KeyValuePair<string, string>> parsed = await client.ParseAddressAsync("1 Main St Apt 4, Springfield, VA 22162"); foreach (var component in parsed) { Console.WriteLine($"{component.Key}: {component.Value}"); } foreach (var expansion in await client.ExpandAddressAsync("1 Main St Apt 4, Springfield, VA 22162")) Console.WriteLine(expansion);
house_number: 1 road: main st unit: apt 4 city: springfield state: va postcode: 22162 1 main saint apt 4 springfield vale 22162 1 main saint apt 4 springfield va 22162 1 main saint apt 4 springfield virginia 22162 1 main saint apartment 4 springfield vale 22162 1 main saint apartment 4 springfield va 22162 1 main saint apartment 4 springfield virginia 22162 1 main street apt 4 springfield vale 22162 1 main street apt 4 springfield va 22162 1 main street apt 4 springfield virginia 22162 1 main street apartment 4 springfield vale 22162 1 main street apartment 4 springfield va 22162 1 main street apartment 4 springfield virginia 22162
Map the calls and fields, not the output strings
The libpostal labels and binding API describe the left side. The right side follows the OneContact 1.1.0 migration reference. A mapping is a place to read a value, not a promise that both parsers extract the same text.
| libpostal or C# binding | OneContact 1.1.0 |
|---|---|
parse_address / ParseAddressAsync | PostalAddress.TryParse(text, region, out value, out detail) with a span and a known Region. The result is OperationStatus, not a list. Use detail.PrimaryReason on refusal and detail.Fields for field offsets. |
house_number, road, unit | StreetLines contains whole lines. For parsed parts, inspect each line in StreetDetail: HouseNumber; PreDirectional, StreetName, StreetType, PostDirectional; and UnitDesignator / UnitNumber, respectively. |
city, state, postcode, country | Locality, AdminArea, PostalCode, and Country (a Region). The U.S. example gets US from the supplied hint even though no country label appears in the binding’s result. |
level, staircase, entrance | No separate properties for these labels. The migration reference describes retaining such text in the street line. |
libpostal_setup() and the data directory | Nothing to set up: in the migration reference’s words, “rules and dictionaries are compiled in (the core package is 1.4 MB); the model packages are 10 to 23 MB”. |
expand_address / ExpandAddressAsync | AddressExpander.Expand(text, languages, maxVariants) returns a list of strings. Choose dictionary languages with ExpansionLanguages flags. The default cap is 16; there are no per-transformation switches. |
What to retest before switching
- Country hints and rejected input. Pass a region when it is already known. Without one, the rules need enough evidence in the address; in the run above, the incomplete line “1 Main St” was refused with
address.no-last-line. Route refusals to your existing review or correction path rather than treating them as empty addresses. - Fields a model decided. With a model package referenced, input that was refused with
address.model-requiredcomes back parsed.detail.Fieldssays which fields the model decided; keep that next to the address if your review path should treat them differently. - Fields you store or match on. Compare typed components for your supported countries using a sample of real input shapes, including units, incomplete lines, and any libpostal labels your app requires. Keep the raw input while you check how parsers split and standardize it.
- Expansion-based matching. The binding emitted variants containing
main saintandmain street; the shown OneContact call returned four variants with a different order and content. The inputs match, but the expansion settings do not. Don’t reuse old variant indexes or assume the first item is a verified postal address. If you need a comparison key, evaluate OneContact’s versioned address key separately.
For more examples of successful and refused inputs, see parse a postal address in C#.