Skip to main content

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.

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 pypostal linked 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 real pypostal install.

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.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for polars-libpostal 0.2.0
File Interpreter ABI Platform Actions
polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.28+ x86-64 Details for polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_x86_64.whl
polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.28+ ARM64 Details for polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_aarch64.whl

Total release size: 12.5 MB

Release files / polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL polars_libpostal-0.2.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
e5db38142ece9c211a8c10ade6b75b11498d7a41f51b4cd13b56533755b5d070
BLAKE2b-256 checksum
How to use checksums
1e60204bdc0f074aa7f431c1cc4858f09c72dd521dc53795aea0a5dc80107d10
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 log

Release files / polars_libpostal-0.2.0-cp310-abi3-manylinux_2_28_aarch64.whl

Download URL polars_libpostal-0.2.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
6bd38062638bc012ac5b06e13813f59194e791c51957fab94ab993bf74750212
BLAKE2b-256 checksum
How to use checksums
09a488322914534462041e7b83c1743a6a496c88c0f123e4376d1e4b0b71b525
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 log

Security for polars-libpostal 0.2.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.

Select from 2 files

Attestation for polars_libpostal-0.2.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 log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page