API reference¶
The importable Python surface: the loading and summary functions, the
Bai2Summary dataclass, and Bai2StatementParser, the
BankStatementParser the core discovers through the
bankstatementparser.loaders entry point. Generated from the docstrings
by mkdocstrings.
bankstatementparser_loader_bai2.loader
¶
BAI2 -> bankstatementparser Transaction loader.
BAI2 <https://www.bai.org/>_ (Bank Administration Institute, version 2)
is the de-facto US cash-management file format that banks ship for
intraday and prior-day balance reporting. The published
bankstatementparser <https://pypi.org/project/bankstatementparser/>_
library does not parse BAI2; this companion loader fills that gap by
turning a BAI2 payload into a flat list of
:class:bankstatementparser.transaction_models.Transaction objects that
downstream deterministic logic can consume.
Supported (pragmatic) subset¶
BAI2 files are line-oriented. Each physical record is a sequence of
comma-delimited fields and ends with a / record delimiter. Every
record begins with a numeric type code identifying the record kind.
This loader implements the following records:
01 File Header
01,senderId,receiverId,fileDate,fileTime,fileId,... -- the file
must start with this record. fileId is captured for the summary.
02 Group Header
02,ultimateReceiver,originator,groupStatus,asOfDate,asOfTime,currency,...
-- the group currency and asOfDate are captured. The as-of
date becomes each transaction's booking_date; the group
currency is the fallback currency for accounts that omit one.
03 Account Identifier
03,accountNumber,currencyCode,typeCode,amount,itemCount,fundsType,...
-- accountNumber and the optional account currencyCode are
captured. The account currency, when present, overrides the group
currency for every transaction under this account.
16 Transaction Detail
16,typeCode,amount,fundsType,[funds-type subfields,]bankRefNum,customerRefNum,text
-- one transaction. amount is an integer in the account
currency's minor units (see "Amounts" below). text becomes the
description.
The ``text`` field is **free-form and runs to the end of the
record, commas included**. Real-world BAI2 puts structured prose
there (``ACH Credit Payment,Entry Description: EXP; -, SEC: CCD,
...``), so the loader splits only the fixed leading fields and keeps
the remainder verbatim rather than naively splitting the whole
record on commas.
The ``fundsType`` field selects how many subfields sit between it
and ``bankRefNum``: ``V`` (value-dated) inserts ``valueDate`` and
``valueTime``; ``S`` (distributed availability) inserts three
availability amounts; everything else (``0``/``1``/``2``/``Z`` or
empty) inserts none. The loader counts these so ``bankRefNum``,
``customerRefNum``, and ``text`` are located correctly regardless of
funds type.
88 Continuation
Continues the text of the immediately preceding 03 or 16
record; its content (everything after the leading 88 field,
commas included) is appended to that record's description. A 88
that continues an 03 account summary, or one that has no
preceding detail to attach to, is dropped rather than mis-attached.
49 Account Trailer, 98 Group Trailer, 99 File Trailer
Control-total records. This loader ignores them -- it does not
validate the control sums. Ignoring is a deliberate, documented
choice: the goal is faithful transaction extraction, not file-level
reconciliation.
Any other (or unknown) leading type code is ignored so that vendor extensions do not abort the parse.
Amounts¶
BAI2 amounts are unsigned integers expressed in the account currency's
minor units (e.g. cents), with no decimal point. They are converted
to :class:decimal.Decimal by dividing by 100 -- Decimal is used
throughout (never float) to avoid binary rounding error. An empty
amount field is treated as 0.
Sign convention (debit / credit)¶
BAI2 transaction type codes are grouped into documented numeric ranges
that encode the direction of funds. This loader applies the following
convention based on the numeric range of the 16 record's type code:
100-399-> credit (amount kept positive)400-699-> debit (amount made negative)700-799-> loan detail. Loan codes describe disbursements, advances, and payments on the loan side of a relationship file. This loader treats them as a debit-side movement (amount made negative), matching the "money leaving the reported balance" reading used for the400-699range.900-999-> custom / summary / status. These are non-detail codes (institution-specific status and summary lines, not individual postings). This loader does not emit a Transaction for them; they are skipped so a status line never pollutes the posting list. Any continuations attached to a skipped status code are dropped with it.- anything else (including non-numeric codes) -> kept positive; the raw type code is preserved.
The raw BAI2 type code is always preserved on every emitted
Transaction in both the category field (as bai2:<code>) and
the reference field, so no information is lost.
Type-code descriptions¶
A small, optional lookup of well-known BAI2 transaction type codes (for
example 142 "ACH credit" or 475 "Check paid") is used to enrich
the Transaction.description when the 16 record itself carries no
free-text. When the lookup has no entry, or the record already has text,
the record's own text is used unchanged.
Bai2Summary
dataclass
¶
High-level counts and identifiers for a parsed BAI2 file.
Attributes:
| Name | Type | Description |
|---|---|---|
file_id |
str | None
|
The |
group_count |
int
|
Number of |
account_count |
int
|
Number of |
transaction_count |
int
|
Number of emitted transactions -- one per
|
currency |
str | None
|
The first currency seen (account currency preferred,
otherwise the group currency), or |
Source code in bankstatementparser_loader_bai2/loader.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
Bai2StatementParser
¶
Bases: BankStatementParser
BankStatementParser-compatible wrapper for BAI2 bank statement files.
Source code in bankstatementparser_loader_bai2/loader.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
__init__(file_name)
¶
Initialize parser with path to BAI2 bank statement file.
Source code in bankstatementparser_loader_bai2/loader.py
671 672 673 674 675 | |
parse()
¶
Parse BAI2 statement transactions into a pandas DataFrame.
Source code in bankstatementparser_loader_bai2/loader.py
677 678 679 680 681 682 683 684 | |
get_summary()
¶
Return standardized summary record for the BAI2 statement.
Source code in bankstatementparser_loader_bai2/loader.py
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
load_bai2(text)
¶
Parse a BAI2 payload into a flat list of Transaction objects.
Every 16 Transaction Detail record across all groups and
accounts becomes one transaction, carrying its account number and
currency. 88 continuation records extend the description of the
preceding 03 or 16 record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The raw BAI2 payload. CRLF / LF endings, blank lines, and
an optional trailing |
required |
Returns:
| Type | Description |
|---|---|
list[Transaction]
|
The parsed transactions in file order. May be empty if the file |
list[Transaction]
|
contains headers but no |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file does not start with an |
Source code in bankstatementparser_loader_bai2/loader.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | |
load_bai2_file(path)
¶
Parse a BAI2 file from disk into Transaction objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Filesystem path to the BAI2 file. UTF-8 is assumed. |
required |
Returns:
| Type | Description |
|---|---|
list[Transaction]
|
The parsed transactions, identical to calling :func: |
list[Transaction]
|
on the file's text content. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file does not start with an |
OSError
|
If the file cannot be read. |
Source code in bankstatementparser_loader_bai2/loader.py
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
summarize_bai2(text)
¶
Summarise a BAI2 payload without materialising every transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The raw BAI2 payload. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
A |
Bai2Summary
|
class: |
Bai2Summary
|
transaction counts, and the first currency observed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file does not start with an |
Source code in bankstatementparser_loader_bai2/loader.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |