polars-libpostal
Parse street addresses with libpostal as a Polars expression that runs on every core.
import polars as pl
from polars_libpostal import parse_address
df = pl.DataFrame({"address": ["Apt 4B, 456 Elm Ave, Springfield, IL 62704", None]})
df.with_columns(
parse_address(
"address", labels=["house_number", "road", "unit", "city", "state", "postcode"]
).alias("p")
).unnest("p")
address house_number road unit city state postcode
Apt 4B, 456 Elm Ave, Springfield, IL 62704 456 elm ave apt 4b springfield il 62704
null null null null null null null
parse_address returns a struct with one field per libpostal label: house, category, near, house_number, road,
unit, level, staircase, entrance, po_box, postcode, suburb, city_district, city, island,
state_district, state, country_region, country and world_region. Pass labels=[...] to get only some of them, in
the order you give. A label that is absent, or null or empty input, gives null. A label that appears twice is joined with a
space. Output is lowercase, as libpostal produces it. It works on LazyFrames and the output schema is known without
parsing anything.
parse_address_list keeps libpostal's own response instead: a List(Struct({"label": String, "value": String})) with one
element per component, in the order the components appear in the address, and repeated labels kept apart. Use it when the
order matters, for example to fold several labels into one column:
from polars_libpostal import parse_address_list
kept = ["house", "house_number", "road", "unit", "level", "po_box"]
label = pl.element().struct.field("label")
value = pl.element().struct.field("value")
df.with_columns(
parse_address_list("address")
.list.eval(value.filter(~label.is_in(kept)))
.list.join(" ")
.alias("other")
)
address other
Apt 4B, 456 Elm Ave, Springfield, IL 62704 springfield il 62704
null null
expand_address returns libpostal's normalized forms of each address as a List(String): abbreviations spelled out,
numbers as digits, accents and punctuation normalized. Two addresses that share an expansion are likely the same place, so
it is useful for matching and deduplication:
from polars_libpostal import expand_address
df.with_columns(expanded=expand_address("address", languages=["en"]))
address expanded
30 W 26th St ["30 w 26th saint", "30 w 26th street", "30 west 26th saint", "30 west 26th street", ...]
Without languages, libpostal's language classifier picks them for each address.
Why this exists
libpostal is a C library, but it cannot be called from several threads: its address parser reuses one parse context, one
CRF scoring context and one Viterbi buffer for every caller. Parse from two threads and they overwrite each other's
buffers (Error in prediction, then double free or corruption in a stress test). The usual Python binding also holds the
GIL, so the common answer is one process per core, each loading its own 3 GB model.
This package ships a patched libpostal (patches/0001-thread-safe-address-parser.patch) that gives each thread its own
scratch space, and a Rust plugin that parses rows on Polars' own Rayon pool. One process, one copy of the model, all cores.
Install
uv add polars-libpostal
uv run python -m polars_libpostal download-data # about 3 GB, once
Without uv, pip install polars-libpostal works too, followed by python -m polars_libpostal download-data.
Wheels are built for Linux x86_64 and aarch64 (manylinux_2_28, Python 3.11+) and bundle the patched libpostal. There is no sdist: it could not build without the patched library. See Building from source.
The model data is looked up in this order: $LIBPOSTAL_DATA_DIR, an existing libpostal install at
/usr/local/share/libpostal, then ~/.local/share/polars-libpostal. uv run python -m polars_libpostal data-dir prints the
one that will be used, and parse_address(..., data_dir=...) overrides it. The models load on the first call in a process.
Speed
Measured on 14 cores (WSL2) with 193,500 distinct addresses:
| Parser | Throughput |
|---|---|
| libpostal through its Python binding | about 10,000 addresses/s |
polars-libpostal on 14 threads |
about 54,000 addresses/s |
The patched parser scales to about 4x at 4 threads and 6x at 8 threads in the C stress test, then flattens, so expect roughly that. The plugin parses every row, repeated ones included. If your data has many repeats, parse the distinct values and join the result back.
Correctness
scripts/stress.sh builds a C program in which every thread parses the same addresses and compares each result with a
single-threaded reference. On the patched library, 280,000 concurrent parses on up to 14 threads gave 0 mismatches; stock
libpostal crashes with 4 threads. The Python suite repeats that check through the plugin (tests/test_parse.py).
scripts/tsan.sh runs the same test under ThreadSanitizer. On the patched library it reports no data races (8 threads,
80,000 parses). On stock libpostal, as a control, it reports races in the shared parse context and then crashes, so the tool
does see the bug the patch fixes. A workflow (.github/workflows/tsan.yml) repeats both runs on pull requests that touch the patch or the scripts, and on demand. That is strong
evidence for the addresses and code paths tested, not a proof: ThreadSanitizer only reports races that actually happen.
Notes
- libpostal ignores the country when it parses (its parser sets language and country to NULL, because the model was trained without them), so there is no country argument.
- Only the parser is bundled. libpostal's address expansion and language classifier are not used.
- The plugin and any other libpostal in the same process (for example
pypostallinked to the stock library) should not share a library name. The wheel bundles its copy under a hashed name, which keeps them apart; this is not yet tested against a realpypostalinstall.
Building from source
You need a Rust toolchain, uv, and the autotools to build libpostal.
uv sync # creates the venv and installs the dev tools
uv run just libpostal # patched libpostal into ./.libpostal (about 15 minutes)
uv run just dev # build the extension into the venv
uv run just data # download the model data
uv run just test
or by hand: scripts/build_libpostal.sh /opt/libpostal, then
LIBPOSTAL_PREFIX=/opt/libpostal LD_LIBRARY_PATH=/opt/libpostal/lib uv run maturin develop --uv. Linking against stock libpostal builds
fine and then fails or corrupts memory under load, so always use the patched build.
Licensing
The code in this repository is licensed under either of Apache-2.0 or MIT, at your option. Wheels bundle libpostal (MIT) and the patch is distributed under its terms; the model data is downloaded from Senzing and has its own terms. See NOTICE.
Metadata
Release files for polars-libpostal 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.28+ x86-64 | Details for polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl |
| polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_aarch64.whl | CPython 3.10 | abi3 | Linux glibc 2.28+ ARM64 | Details for polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_aarch64.whl |
Total release size: 12.5 MB
Release files / polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl
| Download URL | polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 6.4 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
df7c7220bc2e1623992523c7483c5763cf0bec90ee87f33e54671b2cef383416
|
|
BLAKE2b-256 checksum How to use checksums |
c39a37f81d3de938085450a389851573efd134b10184e94d2f260889ed5f674e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_aarch64.whl
| Download URL | polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 6.1 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
a07ffd6e8fc2fa42976545492331f55422f7b3406de9d41d1e44c3e8aa674531
|
|
BLAKE2b-256 checksum How to use checksums |
9156b97e28b94dc9d95e4ce0390792989c2926544cd50b5cbc760e5618103242
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for polars-libpostal 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for polars_libpostal-0.4.0-cp310-abi3-manylinux_2_28_x86_64.whl
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.