Skip to main content

oatschema-adapters

PyPI Python versions CI Coverage License: Apache 2.0

Export OATS (Open Annotation Task Schemas) records to standard formats, and parse those formats back into records.

  • audio-transcription records become SRT or WebVTT subtitles, Markdown, plain text or a TSV table, and each of those parses back.
  • document-extraction records become HTML, hOCR, Markdown, plain text or a TSV table. hOCR, plain text and TSV parse back.
  • Validated. Records are validated with oatschema before export and after parsing, against the schema version you name.
  • Safe with untrusted records. Every value is escaped in HTML and hOCR, whether or not the record was validated, and no TSV cell runs as a spreadsheet formula.
  • Typed. Ships py.typed.

Quick start

pip install oatschema-adapters
from oatschema_adapters import get_adapter

record = {
    "language": "eng",
    "segments": [
        {"text": "Welcome back.", "start_time": 0.5, "end_time": 2.25, "speaker": {"id": "s1", "name": "Maria"}},
        {"text": "Thank you.", "start_time": 2.5, "end_time": 3.0, "speaker": {"id": "s2", "name": "Jean"}},
    ],
}

vtt = get_adapter("audio-transcription", "vtt")
captions = vtt.export(record, include_speakers=True)
# WEBVTT
#
# 00:00:00.500 --> 00:00:02.250
# <v Maria>Welcome back.
# ...

parsed = vtt.parse(captions)  # an audio-transcription record, validated

Requires Python 3.11 or later.

Formats

Schema Format Aliases Extension Parses
audio-transcription srt srt yes
vtt webvtt vtt yes
md markdown md yes
txt txt yes
tsv tsv yes
document-extraction html html no
hocr hocr.html hocr.html yes
md markdown md no
txt txt yes
tsv tsv yes

list_schemas() and list_formats(schema) list them at run time, and format_aliases(schema) maps each alias to its format.

from oatschema_adapters import get_adapter, list_formats

list_formats("document-extraction")
# [('hocr', 'hOCR (.html) - ...'), ('html', 'Semantic HTML (.html) - ...'), ...]

adapter = get_adapter("document-extraction", "markdown")
adapter.format_name  # 'md'
adapter.extension  # 'md'
adapter.can_parse  # False

An Adapter has export(record, **options) and parse(content, **options), and export_file and parse_file for file objects. export takes a dict, or a JSON object as str or bytes.

Options

Every exporter and parser takes validate (default True) and schema_version (default None, the newest version oatschema bundles). Parsers also take producer, the parsed record's producer.

Schema Format Export options
audio-transcription srt, vtt, tsv include_speakers=False
md include_timestamps=True, include_milliseconds=False, include_speakers=False, include_frontmatter=True, low_confidence_threshold=0.8
document-extraction html layout="col" ("col", "grid" or "wireframe"), style="default" ("default", "dark" or "none"), style_path=None, footer=None, fragment=False, gap_x=-1.0, gap_y=-1.0, and the layout options below
md include_frontmatter=True, low_confidence_threshold=0.8, and the layout options below
txt the layout options below

The layout options decide how blocks are joined into lines and paragraphs: strict=False, line_tolerance=5.0, paragraph_margin=20.0, column_jump=-10.0 and scale=None. The document tsv parser takes unit="px", and the hocr parser takes target_class="ocr_line".

Options are keyword arguments. A value given as a string, as from a command line, is converted to the option's type: "false", "no", "off" and "0" are false, and "0.5" is a number. A value that can't be converted, or an option the adapter doesn't have, raises an error naming it:

adapter.export(record, include_speakers="maybe")
# ValueError: to_vtt() option 'include_speakers' must be true or false, not 'maybe'
adapter.export(record, include_speaker=True)
# TypeError: to_vtt() got unknown option(s) 'include_speaker'; options: validate, schema_version, include_speakers

You can also call the functions directly, from oatschema_adapters.audio and oatschema_adapters.document:

from oatschema_adapters.document import to_html

fragment = to_html(document_record, layout="wireframe", fragment=True)

Validation

Records are validated before they're exported, and parsed records before they're returned. A record that doesn't conform raises oatschema.ValidationError, which says where and why without quoting the record's values:

import oatschema

try:
    get_adapter("audio-transcription", "srt").export({"segments": [{"text": "hi", "start_time": -1, "end_time": 1}]})
except oatschema.ValidationError as error:
    print(error)  # audio-transcription 0.8.0 at /segments/0/start_time: … is less than the minimum of 0
    print(error.keyword)  # minimum

Pass schema_version="0.6.0" to validate against an older version, such as the one a stored record was written for; any version from 0.4.0 that oatschema bundles works. An unbundled version raises LookupError. Pass validate=False to skip validation.

Output is escaped whether or not the record was validated, so validate=False is safe for untrusted records: every value is escaped in HTML and hOCR, a style value is only ever a theme name, never a file to read, and a stylesheet from style_path can't close its <style> element.

TSV and spreadsheets

A TSV cell that starts with =, +, -, @, a tab or a carriage return gets a leading ', unless the whole cell is a number, so Excel, LibreOffice Calc and Google Sheets show it as text instead of running it as a formula. A cell that already starts with ' followed by one of those characters gets another '. The TSV parsers remove exactly one, so exporting and parsing gives back the original text. The one exception is the audio exporter, which, as before, replaces tabs in a segment's text with spaces and trims surrounding whitespace.

Adding adapters

register_adapter adds an adapter to the same registry, for another schema or another format of an OATS schema. Give the functions as "module:function" strings, so they're imported only when used:

from oatschema_adapters import get_adapter, register_adapter

register_adapter(
    "example/citation",
    "bibtex",
    description="BibTeX (.bib) - A bibliographic reference format.",
    extension="bib",
    export="my_package.bibtex:to_bibtex",
    aliases=["bib"],
)

get_adapter("example/citation", "bib").export(record)

The export function takes the record first and its options as keyword arguments; a parse function takes the content first. Registering a format that already exists raises ValueError unless you pass replace=True. Validating records of schemas other than OATS is up to your functions.

Versions

oatschema-adapters uses semantic versioning, independently of the OATS schema versions. Until 1.0, a minor release (0.2.0) may change the API, and a patch release (0.1.1) won't. Pin with oatschema-adapters>=0.1,<0.2. It depends on oatschema[validate]>=0.8.0.2,<0.9.

See the CHANGELOG for what changed in each release.

License

Apache-2.0. Copyright The OATS Authors; see AUTHORS.

Metadata

Release files for oatschema-adapters 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for oatschema-adapters 0.1.0
File Size Uploaded Provenance Actions
oatschema_adapters-0.1.0.tar.gz 91.6 kB Without provenance Details for oatschema_adapters-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for oatschema-adapters 0.1.0
File Interpreter ABI Platform Actions
oatschema_adapters-0.1.0-py3-none-any.whl Python 3 none any Details for oatschema_adapters-0.1.0-py3-none-any.whl

Total release size: 138.2 kB

Release files / oatschema_adapters-0.1.0.tar.gz

Download URL oatschema_adapters-0.1.0.tar.gz
Size 91.6 kB
Tags Source
SHA-256 checksum
How to use checksums
a8295ed49cfc347fa9d4810f07b1f5a590a9adc32f1f6cd416593e281d458484
BLAKE2b-256 checksum
How to use checksums
d8e76fb1a1864731488c34fa243659c69ba98e8655691eea89191f0e01a4d9a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / oatschema_adapters-0.1.0-py3-none-any.whl

Download URL oatschema_adapters-0.1.0-py3-none-any.whl
Size 46.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b176cd693e1e0286d8979e362bff767f20e4ca46ba4ddfd54a08dd3bcd21ba40
BLAKE2b-256 checksum
How to use checksums
b541a3596e3ee3788349263c68931ec3b99159e3b41a1dd7234794ec2d261cb2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for oatschema-adapters 0.1.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page