Web3 Multi Provider
web3.py HTTP providers that accept a list of RPC endpoints and fail over to the next one when an endpoint errors, so the application stays online while a node is down.
Requires Python 3.12+ and web3.py 7.
Install
The package is on PyPI as web3-multi-provider:
pip install web3-multi-provider
# or
poetry add web3-multi-provider
With Prometheus metrics:
pip install "web3-multi-provider[metrics]"
# or
poetry add "web3-multi-provider[metrics]"
Releases after 2.3.0 are published from GitHub Actions with PEP 740 attestations, see Verify a release.
Usage
from web3 import Web3
from web3_multi_provider import MultiProvider
from web3_multi_provider import FallbackProvider
w3 = Web3(MultiProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
]))
# or
w3 = Web3(FallbackProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
]))
last_block = w3.eth.get_block('latest')
MultiProvider
This provider spreads requests across the endpoints (round-robin): each request starts one endpoint further than the previous one and fails over to the next endpoints in turn. It fails if no endpoints are available.
Use it with a cooldown. Without one, the provider does not know that an endpoint is down: every request that starts at it tries it first, and the endpoint after it gets its share of the requests too. With a cooldown, requests rotate over the endpoints that are up, so they share the load evenly.
FallbackProvider
This provider sends requests to the endpoints in the configured order until one answers or the list is exhausted.
Endpoint cooldown
A failing endpoint is slow to fail over from: web3 retries it before the next endpoint is tried. All
providers accept cooldown_seconds: an endpoint that failed is then tried after the other endpoints
until its cooldown ends. It is still tried if the other endpoints fail. When the cooldown ends, one
request probes the endpoint; concurrent requests keep it last until that request finishes. With
FallbackProvider the probe keeps the endpoint's place in the configured order, so a backup is not
probed while the endpoints before it answer. With MultiProvider the probe goes first, so a
recovered endpoint is back in the rotation at once. If the probe fails, a new cooldown starts. An endpoint that answers, including one tried
last, is back in its usual place at once. The default 0 disables the cooldown.
w3 = Web3(FallbackProvider([
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
], cooldown_seconds=60))
Errors that blame the request itself do not start a cooldown, so one bad request does not move a
healthy endpoint back for everyone. By default (is_endpoint_failure) these are HTTP 400, 413, 414
and 422: the endpoint answered, so they also end a cooldown, like a success. Every other error starts
a cooldown, including HTTP 401 and 403 from an expired API key and responses that are not JSON.
Pass cooldown_on to decide yourself, for example to start a cooldown on any error:
w3 = Web3(FallbackProvider(endpoints, cooldown_seconds=60, cooldown_on=lambda error: True))
A JSON-RPC error in an HTTP 200 response (for example, a rate limit reported as error -32005) is a
valid answer, so it never reaches cooldown_on and does not start a cooldown.
Retries
Each endpoint is a web3 HTTPProvider, which by default makes up to 5 attempts at a failed read
request with exponential backoff, about 2 seconds in total, before failover moves on. With several endpoints,
failover already is the retry, so these retries mostly delay it. Consider turning them off:
w3 = Web3(FallbackProvider(endpoints, exception_retry_configuration=None, cooldown_seconds=60))
To keep some retries instead, pass an ExceptionRetryConfiguration from
web3.providers.rpc.utils. Its retries counts attempts, so retries=2 means one retry, and its
errors is required. The trade-off: when every endpoint has a short glitch
at once, fewer attempts are made before NoActiveProviderError is raised.
AsyncMultiProvider and AsyncFallbackProvider
These providers are async versions of MultiProvider and FallbackProvider respectively. They may
be used with instances of AsyncWeb3.
from web3 import AsyncWeb3
from web3_multi_provider import AsyncMultiProvider
w3 = AsyncWeb3(AsyncMultiProvider([ # RPC endpoints list
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
]))
Errors
NoActiveProviderError(anExceptionGroup) is raised when every endpoint failed; it holds the error of each endpoint.ChainIdMismatchErroris raised when the endpoints report different chain IDs. Each endpoint fetches its chain ID witheth_chainIdon its first request, not when the provider is created.ProtocolNotSupportedis raised on creation for a non-HTTP endpoint.
Metrics
The library has built-in Prometheus metrics. They are disabled by default — install the extra and call init_metrics() before creating any provider.
pip install web3-multi-provider[metrics]
from web3 import Web3
from web3_multi_provider import MultiProvider, init_metrics
init_metrics() # must be called before creating a provider
w3 = Web3(MultiProvider([
'http://127.0.0.1:8000/',
'https://mainnet.infura.io/v3/...',
]))
init_metrics() registers the following metrics with the default Prometheus registry:
| Metric | Type | Description |
|---|---|---|
http_rpc_requests |
Counter | Total HTTP requests by network, provider, response code, result |
http_rpc_batch_size |
Histogram | Distribution of batch sizes |
http_rpc_response_seconds |
Histogram | RPC response time |
http_rpc_request_payload_bytes |
Histogram | Request payload size |
http_rpc_response_payload_bytes |
Histogram | Response payload size |
rpc_request |
Counter | RPC calls by method and result |
http_rpc_provider_cooldown_events |
Counter | Endpoint cooldown transitions by provider and endpoint index: entered, probe_failed, recovered |
Custom configuration
Use MetricsConfig to set a namespace or override the chain ID → network name mapping:
from web3_multi_provider.metrics import MetricsConfig
init_metrics(MetricsConfig(
namespace="myapp",
chain_id_to_name={
1: "ethereum",
137: "polygon",
# add any custom chains here
},
))
Custom registry
Pass a custom Prometheus registry if you don't want to use the global one:
from prometheus_client import CollectorRegistry
from web3_multi_provider import init_metrics
registry = CollectorRegistry()
init_metrics(registry=registry)
Verify a release
The wheel and the sdist on PyPI carry attestations signed by Sigstore with the identity of the
publish.yml workflow of this repository. PyPI shows them on the
files page of a version. To verify a file:
pip install pypi-attestations
python -m pypi_attestations verify pypi \
--repository https://github.com/lidofinance/web3py-multi-http-provider \
pypi:web3_multi_provider-<version>-py3-none-any.whl
A local file (from pip download --no-deps web3-multi-provider==<version>) can be passed by its path instead.
For developers
This project requires a specific Poetry version, pinned in .poetry-version. To install it:
pipx install "poetry==$(cat .poetry-version)"
Requirements
- Docker
- Dev Container CLI:
npm install -g @devcontainers/cli - VS Code: Dev Containers extension
- JetBrains: Dev Containers plugin
Setup
devcontainer up --workspace-folder .
devcontainer exec --workspace-folder . bash
This installs all dependencies and pre-commit hooks automatically via postCreateCommand.
Alternatively, without Docker:
poetry install- to install depspre-commit install- to install pre-commit hooks
Tests
poetry run pytest -m "not integration" tests
The integration tests call real RPC endpoints, set as a comma-separated list in
WEB3_MULTI_PROVIDER_INTEGRATION_RPC_URLS:
WEB3_MULTI_PROVIDER_INTEGRATION_RPC_URLS=https://...,https://... \
poetry run pytest -m integration tests/test_integration_http_provider.py
Release
- Merge the changes to
main. Every push tomainpublishes a.devbuild to TestPyPI. - Publish a GitHub release tagged
v<version>, asv2.3.1. ThePublishworkflow sets the package version from the tag, builds, signs the distributions and uploads them with their attestations to PyPI through trusted publishing (environmentrelease).
Metadata
Release files for web3-multi-provider 2.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| web3_multi_provider-2.3.1.tar.gz | 25.4 kB | With provenance | Details for web3_multi_provider-2.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| web3_multi_provider-2.3.1-py3-none-any.whl | Python 3 | none | any | Details for web3_multi_provider-2.3.1-py3-none-any.whl |
Total release size: 54.4 kB
Release files / web3_multi_provider-2.3.1.tar.gz
| Download URL | web3_multi_provider-2.3.1.tar.gz |
|---|---|
| Size | 25.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
606ad3605b4e69215f51b0cdcbaa89c73642bdda0d493194e7d8701eafd37730
|
|
BLAKE2b-256 checksum How to use checksums |
a83fca4613993cb8b24923ba27927d82ba3b1b3e95ca17811dbca867a28be234
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.12.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 26, 2026.
Transparency logRelease files / web3_multi_provider-2.3.1-py3-none-any.whl
| Download URL | web3_multi_provider-2.3.1-py3-none-any.whl |
|---|---|
| Size | 29.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
33d2bec8aa79e011ca09ccfe6f7ce008fe2c0a38c9f8ac72cc67995f31d47134
|
|
BLAKE2b-256 checksum How to use checksums |
abeceaf39bf9ecbfdf49865e7153ac4f71784c78ae99f961e7113487adc13a10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.12.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 26, 2026.
Transparency logSecurity for web3-multi-provider 2.3.1
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 web3_multi_provider-2.3.1.tar.gz
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 26, 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.