Recursive AI

libpostal in C#: migrate to OneContact

In C#, replace libpostal parsing with PostalAddress.TryParse and expansion with AddressExpander.Expand. See where OneContact’s results differ.

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.

InstallShell
dotnet add package RecursiveAI.OneContact --version 1.1.0

In a .NET 10 console project, replace Program.cs and run dotnet run.

Program.cs.NET 10
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})");
OutputText
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:

Program.cs.NET 10
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}");
}
OutputNo model package
Parse: InvalidData
Reason: address.model-required

Add the model package and run the same program:

InstallShell
dotnet add package RecursiveAI.OneContact.Models --version 1.1.0
OutputWith RecursiveAI.OneContact.Models
Parse: 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.”

CountryItemsOurs, country givenBest of libpostal 1.1.4 and Senzing 1.2.0Difference [95% CI]Ours, no countryDifference [95% CI]
AD4678.983.0-4.1
[-10.5, +1.6]
79.9-2.4
[-7.8, +2.8]
AT14291.391.4-0.0
[-2.6, +2.4]
91.0-0.4
[-3.2, +2.4]
AU12396.094.9+1.1
[-1.4, +3.3]
94.5-0.4
[-3.7, +2.4]
BE11293.592.9+0.7
[-2.2, +3.6]
92.2-0.7
[-3.5, +2.3]
CA24494.993.8+1.2
[-0.5, +2.8]
94.9+1.4
[-0.3, +3.1]
CH12294.591.8+2.6
[+0.1, +5.1]
93.7+2.7
[-0.4, +5.7]
CZ13196.793.2+3.5
[+1.6, +5.7]
96.4+3.1
[+1.3, +5.4]
DE12392.088.0+4.0
[+1.1, +7.2]
91.3+3.2
[+0.2, +6.5]
DK13896.791.5+5.2
[+2.3, +8.2]
96.6+6.3
[+3.5, +9.1]
EE17092.386.9+5.3
[+2.7, +8.3]
91.7+5.1
[+2.4, +8.2]
ES13288.182.8+5.3
[+1.7, +9.1]
82.2-0.6
[-3.9, +3.0]
FI10396.388.8+7.4
[+3.8, +11.3]
94.6+7.5
[+3.9, +11.8]
FR10186.280.9+5.3
[+0.7, +9.7]
85.6+4.7
[-0.1, +9.3]
GB13885.987.0-1.1
[-4.3, +2.3]
85.9-1.1
[-4.3, +2.3]
HR13395.493.8+1.6
[-0.6, +3.6]
95.3+1.5
[-0.6, +3.5]
HU10392.491.4+1.0
[-2.2, +4.3]
91.6+0.2
[-3.4, +3.8]
IE11778.765.4+13.3
[+8.5, +18.3]
76.2+10.8
[+5.3, +16.2]
IM2587.591.9-4.4
[-10.9, +0.9]
85.3-6.6
[-14.0, +0.0]
IS4091.392.4-1.1
[-6.8, +5.1]
91.3-0.8
[-6.8, +5.8]
IT6096.089.3+6.7
[+3.3, +10.4]
92.9+3.5
[-1.1, +7.8]
LI11795.192.4+2.6
[-0.2, +5.7]
95.1+2.6
[-0.2, +5.7]
LT1991.689.8+1.8
[-2.6, +7.1]
86.8-1.8
[-11.5, +7.9]
LU9291.285.2+6.0
[+1.7, +10.4]
92.0+6.8
[+2.5, +10.9]
LV6693.689.7+3.9
[+0.3, +8.0]
93.6+3.9
[+0.3, +8.0]
MC3887.378.1+9.2
[+1.7, +17.3]
87.3+9.2
[+1.7, +17.3]
MT4975.142.7+32.3
[+22.2, +43.4]
78.5+37.5
[+27.9, +48.6]
MX14478.166.3+11.8
[+7.1, +16.4]
77.2+10.9
[+6.3, +15.7]
NL11797.658.2+39.4
[+34.9, +43.6]
97.6+41.5
[+37.1, +46.0]
NO8593.494.8-1.4
[-4.3, +1.7]
92.2-1.2
[-4.7, +2.6]
PL12897.995.5+2.4
[+0.5, +4.4]
97.9+2.4
[+0.5, +4.4]
PT5579.273.8+5.4
[-1.1, +11.5]
79.2+5.4
[-1.1, +11.5]
RO14173.368.2+5.1
[+0.5, +9.7]
73.0+5.5
[+0.6, +10.2]
SE4997.196.1+1.0
[-2.2, +5.1]
95.6-0.1
[-4.0, +4.5]
SI11496.695.7+0.9
[-0.7, +2.6]
97.0+2.2
[+0.3, +4.5]
SK13495.694.2+1.4
[+0.1, +3.0]
95.4+1.8
[-0.2, +4.2]
SM6185.775.6+10.1
[+2.9, +16.5]
86.1+10.6
[+3.8, +16.8]
US25098.298.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.

Install the bindingShell
dotnet add package NHSISL.LibPostalClient --version 1.0.0.10
Program.cs.NET 10
using 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);
OutputText
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# bindingOneContact 1.1.0
parse_address / ParseAddressAsyncPostalAddress.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, unitStreetLines contains whole lines. For parsed parts, inspect each line in StreetDetail: HouseNumber; PreDirectional, StreetName, StreetType, PostDirectional; and UnitDesignator / UnitNumber, respectively.
city, state, postcode, countryLocality, 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, entranceNo separate properties for these labels. The migration reference describes retaining such text in the street line.
libpostal_setup() and the data directoryNothing 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 / ExpandAddressAsyncAddressExpander.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-required comes back parsed. detail.Fields says 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 saint and main 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#.