Skip to main content

opencost

Pydantic models for the openCost metadata schema — a schema for the financial metadata of scholarly publications — plus a serializer that emits schema-valid openCost XML.

openCost describes two top-level entities, both collectable in one opencost:data document:

  • publication — a single article (or other publication) and its costs: identifiers, paying institution, COAR publication type, and cost_data (invoices with itemized amounts, or a link to a contract).
  • contract — payment models such as transformative agreements, memberships, or subscriptions, with grouped invoices per accounting period.

Install

uv add opencost   # or: pip install opencost

Usage

Building documents

from decimal import Decimal

import opencost

publication = opencost.PublicationType(
    primary_identifier=opencost.PublicationPrimaryIdentifier(doi="10.1234/abcd"),
    institution=opencost.InstitutionType(
        id=[opencost.InstitutionId(type="ror", value="010zzcb52")]
    ),
    publication_type=opencost.CoarPublicationType.journal_article,
    cost_data=opencost.PublicationCostDataType(
        invoice=[
            opencost.PublicationInvoiceType(
                invoice_number="INV-4711",
                creditor="Publisher GmbH",
                dates=opencost.Dates(invoice="2026-05-01", paid="2026-05-20"),
                amount_invoice=opencost.AmountInvoice(
                    amount=Decimal("1980.00"), currency="EUR"
                ),
                amounts_paid=opencost.PublicationAmountsPaid(
                    amount_paid=[
                        opencost.PublicationAmountPaidType(
                            amount=Decimal("1650.00"),
                            currency="EUR",
                            cost_type="gold-oa",
                            vat=Decimal("342.00"),
                        )
                    ]
                ),
            )
        ]
    ),
)

xml = opencost.to_xml(opencost.Data(publication=[publication]))

which produces

<data xmlns="https://opencost.de">
  <publication>
    <primary_identifier>
      <doi>10.1234/abcd</doi>
    </primary_identifier>
    ...
  </publication>
</data>

Parsing documents

from_xml is the exact inverse — children are matched to fields by element name, and every value is validated through the same models:

from pathlib import Path

data = opencost.from_xml(Path("report.xml").read_text())

for publication in data.publication or []:
    doi = publication.primary_identifier.doi
    for invoice in publication.cost_data.invoice or []:
        for amount in invoice.amounts_paid.amount_paid:
            print(doi, amount.amount, amount.currency, amount.cost_type.value)

Parsed values are fully typed (Decimal amounts, enum members, bools), unknown elements are rejected, and the round-trip is lossless: from_xml(to_xml(d)) == d.

Validation

The models enforce the schema rules that can be expressed as type constraints:

  • required lists (Annotated[list[T], Field(min_length=1)]) reject empty lists — e.g. PublicationSecondaryIdentifiers(id=[]) fails;
  • either/or rules (EitherFieldMixin) — e.g. Dates needs invoice or paid, Data needs publication or contract;
  • exactly-one rules — PublicationPrimaryIdentifier takes a doi or a bibliographic_information block, never both or neither;
  • patterns — Currency is a three-letter ISO 4217 code, DateFormat is YYYY, YYYY-MM or YYYY-MM-DD;
  • strict model config — unknown/misspelled fields are rejected (extra="forbid"), and aliased fields accept both the Python name and the wire alias (from_ or from).

Validating generated documents

The XSD is owned by the upstream opencost repository (doc/opencost.xsd). This package deliberately does not ship a copy — validate generated documents against the upstream schema, e.g. with xmllint:

xmllint --noout --schema path/to/opencost/doc/opencost.xsd data.xml

In this repository the upstream schema is pinned as a git submodule (vendor/opencost) and used by the test suite — it is a development-only dependency, never a runtime one.

XML (de)serialization

to_xml derives the XML shape from the models themselves:

  • field declaration order → child order (deterministic; the XSD uses xs:all/symmetric choices, so order is not semantically constrained);
  • None → element omitted (minOccurs=0);
  • list → repeated sibling elements (maxOccurs="unbounded");
  • field alias → element name (e.g. from_ → <from>);
  • enums → their XSD wire values (journal article, gold-oa);
  • bool → true/false; Decimal → two decimal places.

from_xml is the exact inverse — the models drive parsing too: children are matched to fields by element name, type coercion (Decimal, booleans, enum-by-value, patterns) is pydantic's job, and unknown elements are rejected via extra="forbid". Documents with the default namespace or an opencost: prefix parse identically. Round-trip stable: from_xml(to_xml(d)) == d.

data = opencost.from_xml(Path("report.xml").read_text())
for pub in data.publication or []:
    ...

Development

git submodule update --init   # pins upstream schema for the validation tests
uv sync
uv run pytest

License

GPL-3.0-or-later.

Metadata

Release files for opencost 0.1.1

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

Source distribution (sdist)

Source distribution for opencost 0.1.1
File Size Uploaded Provenance Actions
opencost-0.1.1.tar.gz 15.4 kB With provenance Details for opencost-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opencost 0.1.1
File Interpreter ABI Platform Actions
opencost-0.1.1-py3-none-any.whl Python 3 none any Details for opencost-0.1.1-py3-none-any.whl

Total release size: 36.1 kB

Release files / opencost-0.1.1.tar.gz

Download URL opencost-0.1.1.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d531f3841dcd6f0bc557c5c6fef1885e4bc898d57669641955dd3dc875f1a205
BLAKE2b-256 checksum
How to use checksums
d988a4c66b09203ee651d3a2ae60211e44360c6d83a71a9d9116a82aac543e9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.

Transparency log

Release files / opencost-0.1.1-py3-none-any.whl

Download URL opencost-0.1.1-py3-none-any.whl
Size 20.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f793c137d27225033dbddfe9f2f9b419a60edbe2b2dc7af9b9297804e0aa4e4
BLAKE2b-256 checksum
How to use checksums
856e81d63423c93a6394c27915dbe60dfcf5ec7fa8673ad16b7d9eab34630885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.

Transparency log

Security for opencost 0.1.1

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for opencost-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 9, 2026.

Transparency log

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.

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