The RecursiveAI.OneContact.Models package ships an offline model that reads addresses the rules can’t. Installing it turns on the fallback automatically; pass the destination country, such as GB, because the model doesn’t determine it.
dotnet add package RecursiveAI.OneContact.ModelsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneContact; var address = PostalAddress.Parse( "58 Example Avenue Shrewsbury\nSY4 1AA", Region.Parse("GB")); 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.
58 EXAMPLE AVENUE, SHREWSBURY, SY4 1AAFull program
The download uses the invented UK address above, where the town is on the street line and the postcode is alone on the last line. It prints the country from the hint and the parsed delivery fields.
using System.Buffers; using RecursiveAI.OneContact; const string text = "58 Example Avenue Shrewsbury\nSY4 1AA"; var status = PostalAddress.TryParse(text, Region.Parse("GB"), out var address, out var detail); if (status != OperationStatus.Done) { Console.WriteLine(detail.PrimaryReason.Id); return; } Console.WriteLine($"Country: {address.Country}"); foreach (var line in address.StreetLines) Console.WriteLine($"Street: {line}"); Console.WriteLine($"Town: {address.Locality}"); Console.WriteLine($"Postcode: {address.PostalCode}");
dotnet run prints:
Country: GB Street: 58 EXAMPLE AVENUE Town: Shrewsbury Postcode: SY4 1AA
If the address can’t be parsed, Parse throws ContactParseException; the full program prints detail.PrimaryReason.Id instead.
Variations
To check that the fallback actually read this input, print each field’s Source with the same country hint:
using System.Buffers; using RecursiveAI.OneContact; var status = PostalAddress.TryParse( "58 Example Avenue Shrewsbury\nSY4 1AA", Region.Parse("GB"), out _, out var detail); if (status != OperationStatus.Done) throw new FormatException(detail.PrimaryReason.Id); foreach (var field in detail.Fields) Console.WriteLine($"{field.Name}: {field.Source}");
street.house-number: Model street.name: Model street.type: Model locality: Model postal-code: Model
Related recipes
- Parse messy US and Canadian addresses in C#
- See which address fields came from a model in C#
- Parse a postal address in C#
The short answer
Install RecursiveAI.OneContact.Models and call PostalAddress.Parse(text, country) with the destination country you already know. Use Region.Parse("GB") for the United Kingdom, not UK. The package covers Tier 1 (the US and Canada) and Tier 2 (the other countries with free-text support, including the UK, Mexico, and Australia). The smaller RecursiveAI.OneContact.Models.Tier1 package covers only the US and Canada. Check the free-text coverage list before enabling this path for a country.
What does the country hint mean when parsing an international address?
The hint is the country the address belongs to, not the sender’s country or the language of the address. If your checkout already asks the customer to select a destination country, use that selection. It can stay separate from the free-text address: the example doesn’t append United Kingdom to the string just to make the parser recognize it.
The rules try to read the address for that country first. The model can label the words the rules couldn’t read, but it doesn’t classify the country. A Tier 2 model read gets its country from the hint or from a country already decided by the rules. If your input has no trustworthy country value, don’t substitute the country of your own business or the last successful row. Ask for the destination country before relying on the result.
Is the postal code enough to tell which country an address is in?
No. The model documentation gives Germany, Spain, and Czechia as examples of countries whose postcodes can look like a US ZIP code. A code can help read an address once the country is known, but a shared pattern can’t establish which country the address belongs to. Keep the hint as a separate field when you import records so you can rerun a parse with the same context later.
The example shows the code SY4 1AA in PostalCode, separate from Locality (Shrewsbury). That split is not a validation against a delivery database. If your input already has separate address fields, use country-specific formatting rather than joining those fields and asking a model to split them again.