Skip to main content

dpe-hash

DPE(Document / Page / Element)协议的独立 hash 核心:按 hash 契约 1 计算元素、页、文档三层 hash(dpe1: 前缀),并导出契约常量。

pip install "dpe-hash>=0.1.5,<0.2.0"

版本约定

hash 契约升级对应 minor 版本(0.2.0 = 契约 2),不影响 hash 行为的修订走 patch。依赖时锁定 minor:dpe-hash>=0.1.5,<0.2.0。

API

from dpe_hash import content_hash, page_hash, doc_hash, object_hash, document_hashes

h = content_hash({"category": "NarrativeText", "text": "Hello, DPE."})  # 元素对象
p = page_hash({"title": "p1"}, [h])  # 页字段 + 已有 content_hash
d = doc_hash({"file_type": "md", "title": "报告"}, [p])  # 文档字段 + 已有 page_hash
assert d == object_hash({"file_type": "md", "title": "报告", "pages": [p]}, "document")  # 线上原像
入口 用途
content_hash(element, contract=CONTRACT) 元素对象的 content_hash
page_hash(page, element_hashes, contract) 页自身字段 + 已有的 content_hash 列表;page 带 elements 即报错
doc_hash(document, page_hashes, contract) 文档自身字段 + 已有的 page_hash 列表,不需加载元素;document 带 pages 即报错
object_hash(obj, kind, contract) 线上原像(页带 elements、文档带 pages),kind 为 "element" / "page" / "document"
document_hashes(expanded, contract) 展开视图一次算出三层,结果同向量的 expected
children(obj, kind, contract) 原像引用的下一层:页 → content_hash,文档 → page_hash,元素 → blob 引用
parse_hash(s, contracts=SUPPORTED_CONTRACTS) / parse_blob_ref(s) 校验并拆分 dpe1:<hex> / sha256:<hex>
blob_ref(data) blob 字节的引用 sha256:<hex>
jcs(value) RFC 8785 规范化字符串

逐层入口的参数是 TypedDict,便于在编译期发现字段拼错;从线上反序列化、未加类型的 JSON(Any)可以直接传入,已标注为 dict[str, Any] 的数据请用 object_hash(接受 Mapping[str, Any]),或先 cast。

所有入口都对封闭 schema 做完整校验,未定义字段、未知 category / file_type、超过 2^53−1 的整数、NaN 等一律拒绝。

原位重算(契约 1 §6):各入口的 contract 参数决定按哪个契约计算。DRILL_CONTRACT(dpe2)是仅用于升级演练的假想契约,只在显式传入时接受,不在 SUPPORTED_CONTRACTS 中,parse_hash 默认拒绝它。

常量与类型

  • CONTRACT("dpe1")、SUPPORTED_CONTRACTS(服务端 capabilities 的 hash_contracts)、DRILL_CONTRACT;
  • FILE_TYPES:file_type 封闭枚举,按规范顺序;CATEGORY_CONTENT_FIELDS:category → 允许的内容字段(只读映射)。两者与规范 vectors/manifest.json 一致,消费方直接 import,不维护副本;
  • TypedDict:ElementObject、PageObject、DocumentObject(线上原像),PageFields、DocumentFields(不含子列表),ExpandedDocument / ExpandedPage(展开视图),DocumentHashes / PageHashes(结果)。

异常

全部继承 DpeHashError(它是 ValueError 的子类)。每个异常带两个属性:.code 是规范错误码,.path 是出错位置的 RFC 6901 JSON Pointer(相对于传入的对象),可直接映射为 problem 体。

异常 code
ValidationError,及其子类 UndefinedFieldError、FileTypeUnknownError、IntegerOutOfRangeError DPE_VALIDATION
CategoryUnknownError DPE_CATEGORY_UNKNOWN
ContractUnsupportedError DPE_CONTRACT_UNSUPPORTED

Metadata

Release files for dpe-hash 0.1.5

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

Source distribution (sdist)

Source distribution for dpe-hash 0.1.5
File Size Uploaded Provenance
dpe_hash-0.1.5.tar.gz 13.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for dpe-hash 0.1.5
File Interpreter ABI Platform
dpe_hash-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 30.3 kB

Release files / dpe_hash-0.1.5.tar.gz

Download URL dpe_hash-0.1.5.tar.gz
Size 13.2 kB
Tags Source
SHA-256 checksum
How to use checksums
03013e403e4b393c91d35582b31d769799d4c77574181e110349b9c07482bd0a
BLAKE2b-256 checksum
How to use checksums
62047ea7d229514fdef15fecaafa9338956c610fcc0bde2f9d6d0ffc4944bdc4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 2, 2026.

Transparency log

Release files / dpe_hash-0.1.5-py3-none-any.whl

Download URL dpe_hash-0.1.5-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e105a0b2242a5a01b5dab82675f7073a368586840d95c5399676c37bdf1e421e
BLAKE2b-256 checksum
How to use checksums
f3a67a45bae012c6152362fa36c36bb722bb852fea52b88dc5f72861593c249f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 2, 2026.

Transparency log

Security for dpe-hash 0.1.5

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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for dpe_hash-0.1.5.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 Oct 2, 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