Registry / http-networking / async-substrate-interface

async-substrate-interface

JSON →
library2.2.0pypypi✓ verified 85d ago

Asyncio library for interacting with Substrate. It aims to be mostly API-compatible with `py-substrate-interface`. The current version is 1.6.4, with frequent releases, often several per month. It utilizes `bt-decode` for faster SCALE decoding.

pip install async-substrate-interface
INSTALL
IMPORT
SIG · ASYNC-SUBSTRATE-IN
A
async-substrate-interface
http-networkingpythonv2.2.0
Install
2.7s avg
Import
567ms
Disk
45MB
Pass rate
9/ 10
Env Coverage9 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 2.63s
py 3.11
✓ —
✓ 2.48s
py 3.12
✓ —
✓ 2.25s
py 3.13
✓ —
✓ 2.28s
py 3.9
✕ build_error
✓ 3.93s
45MB installed
● package 45MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AsyncSubstrateInterface
from async_substrate_interface import AsyncSubstrateInterface

Initializes an `AsyncSubstrateInterface` to connect to a Substrate node (e.g., Polkadot) and performs an asynchronous query for an account's system data. Ensure an `asyncio` event loop is running to execute the main function.

import asyncio from async_substrate_interface import AsyncSubstrateInterface async def main(): substrate = AsyncSubstrateInterface( url="wss://rpc.polkadot.io" ) async with substrate: result = await substrate.query( module='System', storage_function='Account', params=['5CZs3T15Ky4jch1sUpSFwkUbYEnsCfe1WCY51fH3SPV6NFnf'] # Example address ) print(result) if __name__ == '__main__': asyncio.run(main())
Debug
Known issues
breakingVersion 1.6.4 introduced changes to fix the 'legacy (old) runtimeApi'. Users relying on older runtime API interaction patterns might need to update their code to align with these changes.
fix
Review the `v1.6.4` changelog and your Substrate node's API documentation for specifics on updated runtime API interactions. Adjust your code to use the modern API.
affects: >=1.6.4
deprecatedSupport for Python 3.9 was officially removed in version 1.6.2. Attempts to use the library with Python 3.9 or older will likely result in installation or runtime errors.
fix
Upgrade your Python environment to version 3.10, 3.11, 3.12, 3.13, or 3.14. The library currently requires Python `<3.15,>=3.10`.
affects: >=1.6.2
gotchaWhile designed to be 'mostly API-compatible with py-substrate-interface', the library is explicitly stated as 'about 90% API compatible' and 'its own library'. This implies that subtle differences in API or behavior may exist.
fix
When migrating from `py-substrate-interface` or developing, carefully review the `async-substrate-interface` documentation and test critical paths to identify any API deviations or behavioral changes.
affects: All
gotchaThe library employs various caching mechanisms (in-memory and disk-based via `DiskCachedAsyncSubstrateInterface`). Cache sizes are configurable via environment variables (`SUBSTRATE_CACHE_METHOD_SIZE`, `SUBSTRATE_RUNTIME_CACHE_SIZE`), and disk caching is keyed by the network URI.
fix
Be aware of these caching behaviors. Ensure environment variables are set correctly if you wish to override defaults. If using `DiskCachedAsyncSubstrateInterface` with multiple networks, remember that caches are separate per URI, which is usually desired but important to understand.
affects: All
gotchaThe documentation suggests that while `AsyncSubstrateInterface` can be used directly, it is generally recommended to use it as part of `AsyncSubtensor` when operating within the Bittensor ecosystem.
fix
If your application is part of the Bittensor ecosystem, consider leveraging `AsyncSubtensor` for a more integrated and potentially optimized experience. If using `AsyncSubstrateInterface` directly, be mindful of any Bittensor-specific conventions or helper methods you might be missing.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'async_substrate_interface'
The `async-substrate-interface` package is not installed in the Python environment being used, or there's a typo in the import statement.
fix
Ensure the package is correctly installed using pip: `pip install async-substrate-interface`. Also, verify the import statement is `from async_substrate_interface import AsyncSubstrateInterface`.
RuntimeWarning: coroutine 'AsyncSubstrateInterface.query' was never awaited
An asynchronous method (coroutine) of `AsyncSubstrateInterface` was called without using the `await` keyword, or an `AsyncSubstrateInterface` instance was not used within an `async with` statement.
fix
Always `await` calls to asynchronous methods and initialize `AsyncSubstrateInterface` within an `async with` block inside an `async def` function.
```python
import asyncio
from async_substrate_interface import AsyncSubstrateInterface

async def main():
    async with AsyncSubstrateInterface(url="wss://rpc.polkadot.io") as substrate:
        result = await substrate.query(
            module='System',
            storage_function='Account',
            params=['5CZs3T15Ky4jch1sUpSFwkUbYEnsCfe1WCY51fH3SPV6NFnf']
        )
        print(result)

if __name__ == '__main__':
    asyncio.run(main())
```
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was closed unexpectedly
The client failed to establish or maintain a WebSocket connection with the Substrate node, often due to an incorrect node URL, the node not running, network issues, or the node not being configured to accept external WebSocket connections (e.g., missing `--ws-external` flag).
fix
Verify the Substrate node is running and accessible at the specified URL. If connecting to a remote node, ensure the node is started with `--ws-external` to allow external connections and that no firewalls are blocking the port. For example: `your-node --dev --ws-external`.
RemainingScaleBytesNotEmptyException
This error, often originating from the underlying `scalecodec` library, indicates a mismatch between the expected data types and the actual SCALE-encoded data received from the Substrate node, frequently due to an outdated type registry or custom runtime types not being correctly defined or loaded.
fix
Ensure your local type registry is up-to-date. If using a custom chain or a chain with recent runtime upgrades, manually provide the correct `type_registry_preset` or a custom `type_registry` JSON file when initializing `AsyncSubstrateInterface`. You might also try setting `use_remote_preset=True` if supported by the connected node.
Upgrade
Version history
2.2.0latest on PyPI · released Jun 11, 2026
Audit
Dependencies
pythonrequiredRequired Python version range.
aiosqliterequiredUsed for caching mechanisms.
bt-decoderequiredUsed for faster SCALE decoding.
scalecodecrequiredCore library for SCALE codec functionality.
websocketsrequiredFor WebSocket communication with Substrate nodes.
xxhashrequiredLikely used for hashing in caching or data structures.
Agent activity
26 hits · last 30 days
node
22
OpenAI (training)
1
Resources
async-substrate-interface — pip install async-substrate-interface · libregistry