API reference
The strict standalone public API is:
File operations infer formats from filename suffixes unless an explicit
format is provided. String operations require format. See
Usage for the read/write format matrix, including the distinction
between built-in Markdown table output and optional reference-list parsing.
Loading API
Raw loaders return an ordered list of record dictionaries with parser-level field names and values (for example, strings, lists, or mappings). They do not apply normalization. Markdown loading assigns sequential IDs to parsed GROBID references so they remain identifiable as records.
- bibliography_io.load_raw(path, *, format=None, mapping_fields=(), list_fields=(), missing_ok=False)
- bibliography_io.loads_raw(string, *, format, mapping_fields=(), list_fields=())
mapping_fields and list_fields declare structured BibTeX fields.
File operations infer format from the suffix unless supplied explicitly;
string operations require it. load_raw() and load() raise
FileNotFoundError for a missing path by default. Pass missing_ok=True
to return [] from load_raw() or {} from load() instead. This
option only applies to missing paths: errors for existing non-file paths and
parsing errors are still raised. loads() and loads_raw() accept text
and do not take missing_ok. load_primary() retains its existing behavior
of returning {} for a missing path.
BibTeX repair API
- bibliography_io.fix_bib_file(filename, *, logger=None)
Repair malformed BibTeX entries by modifying the file in place.
The file is updated directly without creating a backup. Repairs replace semicolons and whitespace in citation keys with underscores, append alphabetic suffixes to duplicate citation keys, and replace whitespace in field names with underscores.
- Parameters:
filename (str | Path) – String or
pathlib.Pathfilename of the BibTeX file.logger (Logger | None) – Optional logger used to report corrections. Defaults to the
bibliography_iopackage logger.
- Returns:
None
- Return type:
None
The repair operates on the supplied file in place and does not create a backup.
It replaces semicolons and whitespace in citation keys with underscores,
appends alphabetic suffixes to duplicate citation keys, and replaces whitespace
in field names with underscores. The optional logger argument defaults to
the bibliography_io package logger.
The convenience loaders apply generic normalization when source is omitted.
If source is supplied, they select an installed CoLRev search-source package
by its unique endpoint identifier (for example, colrev.crossref).
source_version is an optional export-version label. Both normalized loaders
return a dictionary keyed by unique, non-empty record IDs; invalid or duplicate
IDs raise MissingRecordIDError or DuplicateRecordIDError. This mapping
supports processing imports as primary records, including merging and processing
them, while retaining IDs for back-tracing. File suffixes are used to infer the
format unless format is supplied; pass format when the suffix is
ambiguous or does not match the file’s actual format. Use the raw loading APIs
for parser-level values.
For the return-value contracts of the raw, normalized, and primary loaders, see the Record shape section in Usage.
- bibliography_io.load(path, *, source=None, source_version=None, format=None, missing_ok=False)
It returns an ID-indexed dictionary of record dictionaries, preserving source
order. CoLRev search-source packages register search_source entry points in
the colrev entry-point group; bibliography-io selects the package whose
endpoint matches source. Source-specific hook behavior and package
registration are described in Source extensions for bibliography-io. A missing package raises
OptionalBackendUnavailableError.
- bibliography_io.loads(text, *, format, source=None, source_version=None)
It returns the same ID-indexed mapping contract as load() for text input.
load_df() calls load() and returns its normalized records as DataFrame
rows. Parser-level data is available through the list-returning raw APIs rather
than a raw DataFrame helper.
- bibliography_io.load_primary(path, *, format=None, header_only=False, mapping_fields=(), list_fields=())
Load primary records as an ID-indexed dictionary.
A missing path returns an empty dictionary.
For BibTeX,
header_only=TrueextractsID,ENTRYTYPE,colrev_origin,colrev_status,file,screening_criteria, andcolrev_masterdata_provenancewhile skipping other fields. Primary records are assumed to be controlled: IDs are unique,ENTRYTYPEvalues are non-empty strings, and ordinary field values are strings. Declare fields throughmapping_fieldsorlist_fieldsto decode their BibTeX values into dictionaries or lists. Other value types raiseRecordValidationErrorrather than being converted or combined.- Parameters:
path (str | Path)
format (str | None)
header_only (bool)
mapping_fields (Any)
list_fields (Any)
- Return type:
dict[str, dict[str, Any]]
- bibliography_io.loads_primary(text, *, format, mapping_fields=(), list_fields=())
Load controlled primary records as an ID-indexed dictionary.
Primary records must have unique, non-empty string IDs, non-empty string
ENTRYTYPEvalues, and string values for ordinary fields. Declare fields throughmapping_fieldsorlist_fieldsto retain their structured values. Values are validated rather than normalized.- Parameters:
text (str)
format (str)
mapping_fields (Any)
list_fields (Any)
- Return type:
dict[str, dict[str, Any]]
- bibliography_io.write(records, *, path, format=None, mapping_fields=(), list_fields=(), field_order=None, sort_fields=True, primary=False, sort_by_id=True)
Write records, replacing existing contents.
For BibTeX,
mapping_fieldsandlist_fieldsdeclare provenance mappings and semicolon-delimited list fields. Supplying either option for other formats raisesUnsupportedFormatError. Setprimary=Trueto validate an ID-keyed primary-record mapping with unique IDs, non-empty stringENTRYTYPEvalues, and string values outside declared mapping and list fields, sort records by ID, and restrict output to BibTeX. By default,field_order(or the CoLRev-oriented order when omitted) is followed by remaining fields alphabetically. Setsort_fields=Falseto preserve input field order without sorting. Inputs are sorted by record ID by default. CSV, Markdown, Excel, and RIS output always uses ID order;sort_by_id=Falsepreserves input order only for formats that support it.primary=Truecontinues to sort the validated primary mapping by ID.- Parameters:
records (Any)
path (str | Path)
format (str | None)
mapping_fields (Any)
list_fields (Any)
field_order (Any)
sort_fields (bool)
primary (bool)
sort_by_id (bool)
- Return type:
None
- bibliography_io.to_string(records, *, format, mapping_fields=(), list_fields=(), field_order=None, sort_fields=True, sort_by_id=True)
Return records written as a formatted string.
For BibTeX,
mapping_fieldsandlist_fieldsdeclare provenance mappings and semicolon-delimited list fields. Supplying either option for other formats raisesUnsupportedFormatError. The declared names match the record field names (matching is case-insensitive, as BibTeX field names are). By default,field_order(or the CoLRev-oriented order when omitted) is followed by remaining fields alphabetically. Setsort_fields=Falseto preserve the input field order without sorting. Inputs are sorted by record ID by default. CSV, Markdown, Excel, and RIS output always uses ID order;sort_by_id=Falsepreserves input order only for formats that support it.- Parameters:
records (Any)
format (str)
mapping_fields (Any)
list_fields (Any)
field_order (Any)
sort_fields (bool)
sort_by_id (bool)
- Return type:
str
- bibliography_io.load_df(path, *, source=None, source_version=None, format=None)
Load normalized records into a DataFrame.
formatoverrides suffix inference when a file suffix is ambiguous or does not match the file’s actual format. Source-specific normalization can be selected withsourceandsource_version.- Parameters:
path (str | Path)
source (str | None)
source_version (str | None)
format (str | None)
- Return type:
Any
- bibliography_io.load_primary_df(path, *, format=None, header_only=False, mapping_fields=(), list_fields=())
Load primary records into a DataFrame indexed by record ID.
- Parameters:
path (str | Path)
format (str | None)
header_only (bool)
mapping_fields (Any)
list_fields (Any)
- Return type:
Any
- bibliography_io.get_nr_records(filename)
Return the number of raw records in a file.
- Parameters:
filename (str | Path)
- Return type:
int