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(结果)。

异常

校验按规范 core §2.8 的顺序进行:形状 → category → 封闭 schema → 逐字段。因此同一输入的错误码与其他实现一致,并由规范的拒绝类向量 invalid_objects 校验。

全部继承 DpeHashError(它是 ValueError 的子类)。每个异常带两个属性:.code 是规范错误码,.path 是出错位置的 RFC 6901 JSON Pointer(相对于传入的对象),可用于日志或 problem 的 detail 文本(HTTP problem 体不携带违例位置,core §2.8)。

异常 code
ValidationError,及其子类 UndefinedFieldError、FileTypeUnknownError、IntegerOutOfRangeError、InvalidUnicodeError(I-JSON:孤立代理项) DPE_VALIDATION
CategoryUnknownError DPE_CATEGORY_UNKNOWN
ContractUnsupportedError DPE_CONTRACT_UNSUPPORTED

本包接收的是已解析的对象。core §2.8 第 0 步要求整个请求体是 I-JSON,这一步先于契约头判定,由服务端负责:

  • 重复键:解析时用拒绝重复键的解析器,例如 json.loads(..., object_pairs_hook=...);
  • 孤立代理项:解析后先用 has_invalid_unicode(body) 检查整个请求体(含 staging_session 等信封字段),命中即返回 DPE_VALIDATION。Python 的 json 会放行孤立代理项。

Metadata

Release files for dpe-hash 0.2.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 dpe-hash 0.2.0
File Size Uploaded Provenance
dpe_hash-0.2.0.tar.gz 14.9 kB With provenance Details

Built distribution (wheel)

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

Total release size: 33.9 kB

Release files / dpe_hash-0.2.0.tar.gz

Download URL dpe_hash-0.2.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
22d7ce0e569e5d731abe9a55386d573e6010e152e3c638fad4f8028c3c114bfd
BLAKE2b-256 checksum
How to use checksums
660a7a7d26270ea9cf85b29afdf4f8054411fa3d3f4def1ac02870439d7d4af8
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 6, 2026.

Transparency log

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

Download URL dpe_hash-0.2.0-py3-none-any.whl
Size 19.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0dab1def1ae81dc2f0addc6129423bb76b8b5764d236d4a3456d0123ddd4f2d9
BLAKE2b-256 checksum
How to use checksums
d79b93ecc871605a841c6b3b4dad64495c5f9fd9b17233312865d8bd7eecce9a
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 6, 2026.

Transparency log

Security for dpe-hash 0.2.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.

Full provenance

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

Select from 2 files

Attestation for dpe_hash-0.2.0.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 6, 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