Registry / data / databento-dbn

databento-dbn

JSON →
library0.60.0pypypi✓ verified 87d ago

Python bindings for encoding and decoding Databento Binary Encoding (DBN). This library provides efficient Rust-backed functionality for working with DBN data streams and files, offering features like record buffering, mutable record references, and direct access to timestamp fields. As of version 0.54.0, it includes enhancements for dynamic record types and improved memory management. Releases occur frequently, typically on a monthly or bi-monthly basis.

pip install databento-dbn
INSTALL
IMPORT
SIG · DATABENTO-DBN
D
databento-dbn
datapythonv0.60.0
Install
1.8s avg
Import
18ms
Disk
21MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.60.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
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.020s · 23.7MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.8s · import 0.016s · 24MB
21MB installed
● package 21MB
Code
Verified usage

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

DBNDecoder
from databento_dbn import DBNDecoder
DBNEncoder
from databento_dbn import DBNEncoder
Metadata
from databento_dbn import Metadata
MBO
from databento_dbn import MBO
Trades
from databento_dbn import Trades
DBNRecord
from databento_dbn import DBNRecord
from databento_dbn import Record
`Record` was removed as a union type alias in v0.46.0; use `DBNRecord` instead.
UNDEF_TIMESTAMP
from databento_dbn import UNDEF_TIMESTAMP

This quickstart demonstrates how to encode a DBN record from Python objects and then decode the resulting DBN bytes. It covers creating metadata, encoding an MBO record, and decoding from both raw bytes and a file-like object. It also highlights checking for `UNDEF_TIMESTAMP` for fields like `ts_out`.

import io import datetime from databento_dbn import DBNDecoder, DBNEncoder, Metadata, Schema, SType, Compression, MBO, UNDEF_TIMESTAMP NANO_SECONDS_IN_SECOND = 1_000_000_000 def to_nanos(dt: datetime.datetime) -> int: "Convert datetime object to nanoseconds since Unix epoch." return int(dt.timestamp() * NANO_SECONDS_IN_SECOND) # 1. Create DBN metadata metadata = Metadata( version=3, dataset="GLBX.MDP3", schema=Schema.MBO, stype_in=SType.RAW_SYMBOL, stype_out=SType.INSTRUMENT_ID, start=to_nanos(datetime.datetime(2024, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc)), end=to_nanos(datetime.datetime(2024, 1, 1, 0, 0, 1, tzinfo=datetime.timezone.utc)), symbols=["ES.c.0"], partial=[0], not_found=[0], mappings=[], ts_out=False, # Set to False for this example to show UNDEF_TIMESTAMP compression=Compression.NONE, ) # 2. Encode a sample MBO record into DBN bytes encoder = DBNEncoder(metadata=metadata, upgrade_records=True) sample_mbo = MBO( publisher_id=1, instrument_id=12345, ts_event=to_nanos(datetime.datetime(2024, 1, 1, 0, 0, 0, 123456789, tzinfo=datetime.timezone.utc)), action=b'A', side=b'B', price=100_00_000_000_000, # Represents 100.00 in fixed-point nanodollars size=10, depth=0, is_snapshot=1, ts_in_delta=0, sequence=1, booklevel=0, flags=0, display_qty=10, orders_count=1, ts_recv=to_nanos(datetime.datetime(2024, 1, 1, 0, 0, 0, 123456789, tzinfo=datetime.timezone.utc)), trade_size=0, trade_id=0, mbp_flags=0, channel_id=0, ) encoded_bytes = encoder.encode_record(sample_mbo) encoded_bytes += encoder.finish() # Finalize the stream print(f"Encoded {len(encoded_bytes)} bytes of DBN data.") # 3. Decode the DBN data from bytes decoder = DBNDecoder() decoded_records = [] for record in decoder.decode(encoded_bytes): decoded_records.append(record) print(f"\nDecoded {len(decoded_records)} records from raw bytes.") for record in decoded_records: print(record) if isinstance(record, MBO): print(f" MBO Record: Instrument ID={record.instrument_id}, Price={record.price / NANO_SECONDS_IN_SECOND:.2f}, Size={record.size}") if record.ts_out == UNDEF_TIMESTAMP: print(" ts_out is undefined (as expected for this metadata configuration)") # 4. Decode the DBN data from a file-like object dbn_io = io.BytesIO(encoded_bytes) file_decoder = DBNDecoder() file_records = [] for record in file_decoder.decode(dbn_io): file_records.append(record) print(f"\nDecoded {len(file_records)} records from BytesIO.")
Debug
Known issues
breakingThe `ts_out` attribute on all Python record types changed from a dynamic `__dict__` attribute to a permanent `int` field. Simultaneously, `__dict__` was removed from all Python record classes.
fix
Access `record.ts_out` directly. Its value will be `databento_dbn.UNDEF_TIMESTAMP` if not set. Avoid relying on `record.__dict__` for record attributes.
affects: <0.53.0
breakingThe `Record` class was removed from Python type stubs (it was never a true base class in runtime). `DBNRecord` union type was introduced as its replacement for type hinting all DBN record types.
fix
Update type hints and `isinstance` checks to use `DBNRecord` instead of `Record` (e.g., `isinstance(rec, DBNRecord)`).
affects: <0.46.0
gotchaDBN records may have different fields or structures across DBN versions (v1, v2, v3). While `DBNDecoder` handles this automatically, direct instantiation or field access of record types might need to consider version compatibility.
fix
For explicit version-specific types, import from `databento_dbn.v1`, `databento_dbn.v2`, or `databento_dbn.v3` (e.g., `from databento_dbn.v3 import MBO`). When working with decoded records, use `getattr()` or handle `AttributeError` for potentially missing fields if the DBN version is unknown.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'dbn'
The Python package name `databento-dbn` is often confused with the module name `dbn` when attempting to import. Users might try to import `databento_dbn` or `databento-dbn` directly instead of the actual top-level module `dbn`.
fix
Use `import dbn` to correctly import the library after installing it with `pip install databento-dbn`.
AttributeError: 'DBNRecord' object has no attribute 'some_field'
The `databento-dbn` library provides a generic `DBNRecord` type, which is a union of various specific record types (e.g., `TradeMsg`, `Mbp1Msg`). Directly accessing a field like `some_field` without first checking the actual record type or downcasting will result in an `AttributeError` if `some_field` does not exist on the underlying concrete record type.
fix
Inspect the `rtype` (record type) of the `DBNRecord` to determine its specific schema and then access the appropriate fields. For example, `if record.rtype == dbn.RType.TRADE: print(record.price)`.
dbn.dbn.Error: Invalid DBN header: expected 'DBN\0' magic, got 'xxxx'
This error occurs when attempting to decode a file or stream that is not a valid Databento Binary Encoding (DBN) file, or if the file is corrupted. The library checks for a specific 'DBN\0' magic number at the beginning of the file.
fix
Ensure the input file or stream is a legitimate DBN file. Verify the file integrity and that it was generated correctly by a Databento source or converter. If reading from a network stream, confirm the data source is sending DBN formatted data.
Upgrade
Version history
0.60.0latest on PyPI · released Jun 9, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
OpenAI (training)
1
Resources