Recursive AI

Parse messy US and Canadian addresses in C#

OneContact is a free, open-source contact data library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies, no network access.

OneContact parses with built-in rules first, and this package adds a small local model for inputs the rules can’t split. The model never replaces a field the address rules already found.

Terminal
dotnet add package RecursiveAI.OneContact.Models.Tier1

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported. NuGet brings in RecursiveAI.OneContact automatically, so you don’t need a separate install command.

C#
using RecursiveAI.OneContact;
 
var address = PostalAddress.Parse(
    "123 Main St Fort Worth TX 76102",
    Region.US);
Console.WriteLine(address.Canonical);

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.

Text
123 MAIN ST, FORT WORTH TX 76102

Full program

The download parses two invented inputs. In the US input, the missing separator before Fort Worth makes the rules refuse the address. In the Canadian input, the rules read the street and last line; the model labels Alex Example as the recipient.

C#
using System.Buffers;
using RecursiveAI.OneContact;
 
Show("123 Main St Fort Worth TX 76102", Region.US);
Show("Alex Example\n123 Main St\nToronto ON M5V 2T6",
    Region.CA);
 
static void Show(string text, Region country)
{
    var status = PostalAddress.TryParse(
        text, country, out var address, out var detail);
    if (status != OperationStatus.Done)
    {
        Console.WriteLine(detail.PrimaryReason.Id);
        return;
    }
 
    Console.WriteLine(address.Canonical);
    foreach (var field in detail.Fields)
        Console.WriteLine($"  {field.Name}: {field.Source}");
}

dotnet run prints:

Text
123 MAIN ST, FORT WORTH TX 76102
  street.house-number: Model
  street.name: Model
  street.type: Model
  locality: Model
  admin-area: Model
  postal-code: Model
ALEX EXAMPLE, 123 MAIN ST, TORONTO ON M5V 2T6
  recipient: Model
  street.house-number: Rule
  street.name: Rule
  street.type: Rule
  locality: Rule
  admin-area: Rule
  postal-code: Rule

If a parse fails, Parse throws ContactParseException; the full program uses TryParse instead and prints the reason ID.

Variations

Turn off the registered model for one scope to see what the rules do with the same US input. In ContactModels.Use(null, null), the first null disables the address model and the second disables the name model:

C#
using System.Buffers;
using RecursiveAI.OneContact;
using RecursiveAI.OneContact.Models;
 
using var rulesOnly = ContactModels.Use(null, null);
var status = PostalAddress.TryParse(
    "123 Main St Fort Worth TX 76102",
    Region.US, out var address, out var detail);
Console.WriteLine(status == OperationStatus.Done
    ? address.Canonical : detail.PrimaryReason.Id);
Text
address.model-required

The short answer

Add RecursiveAI.OneContact.Models.Tier1 to the .NET 10 project that calls PostalAddress.Parse. The model registers automatically and adds a local fallback for US and Canadian addresses. Keep using the same address API and pass Region.US or Region.CA when you already know the country. The model is not a separate service: its trained parameters ship in the package and are loaded on first use, with no separate weights download.

How do you parse a postal address without commas in C#?

Missing punctuation doesn’t always make an address unreadable. What matters is whether the rules can tell where one field ends and the next begins. In 123 Main St Fort Worth TX 76102, the state and ZIP are recognizable, but the rules refuse to split the run of words into a street and a two-word city. With the fallback installed, the parser can use the model’s word labels to separate Main St from Fort Worth. The formatted result adds a comma; you don’t have to insert it into the source text yourself.

Use PostalAddress.Parse for an input you expect to read, or the status-returning TryParse in the download for a batch of mixed-quality rows. If an input still fails, keep the original row and use the reason ID to decide whether to ask for a corrected address. The model does not supply fields it didn’t label, and a successful parse does not establish that the address receives mail.

How does the model handle Canadian addresses without changing the rules?

In the Toronto example, the rules already recognize the street, locality, province, and Canadian postal code. The line above the street is preserved as written by the rules but is not classified as a recipient. The model labels that line while leaving the other fields alone. That’s why the Canadian example prints both Model and Rule, even though its single-line address can look the same without the package. Read ParseDetail.Fields if that distinction matters to your import.

Choose the Tier 1 package if your input is limited to the US and Canada. For addresses such as British or Australian ones, use the Tier 1 + Tier 2 package instead. If a project references both, the broader package wins; the parser doesn’t run two models in sequence. The model documentation explains when the model runs and how the package registers it.

All onecontact recipes