Documents¶
LegalDoc ¶
Bases: BaseModel
Unified base model for all legal documents processed by Contractex.
Every document that passes through a Contractex pipeline is represented as a LegalDoc (or a typed subclass). The doc_type field acts as the discriminator for routing and rendering logic.
Key design principles¶
- Privacy first —
privacy_profileis attached at construction and travels with the document through every pipeline stage. ThePrivacyAwareLLMRouterreads it before any external API call. - Provenance everywhere —
provenancemaps every extracted field to aSourceSpan(chunk ID, page, char offsets, verbatim snippet). - Composable —
extractedis a free-form dict so task outputs can be layered without subclassing. Typed subclasses (Contract) overlay strongly-typed fields on top. - Confidence aware —
confidencesmirrorsextractedso routing and review queues can be applied field-by-field.
Attributes¶
doc_id: Stable UUID string assigned at construction if not supplied. doc_type: Primary document classification (DocType enum). jurisdiction: Jurisdiction code (e.g. "US-Federal", "CA-ON"). language: BCP-47 language code (default "en"). full_text: Full extracted plain text of the document. metadata: Provenance and processing metadata. privacy_profile: Privacy controls — routing, sensitivity, redaction state. Populated lazily; callers should always attach one before passing the document to a pipeline that calls an LLM. extracted: Free-form extraction results keyed by field name. confidences: Per-field confidence scores (mirrors extracted). provenance: Maps field names to SourceSpan records. tags: Arbitrary string tags for filtering and grouping.
content_hash: str | None property ¶
SHA-256 of full_text for deduplication. None if no text.
effective_jurisdiction_tag: Any | None property ¶
Return the JurisdictionTag for this document.
If jurisdiction_tag is already set, returns it directly. Otherwise parses the flat jurisdiction string on-the-fly. Returns None when neither field is populated.
authority_weight: float property ¶
Normalised authority weight [0.01, 1.00] for retrieval scoring.
Returns 0.01 (AuthorityLevel.UNKNOWN) when no authority_profile is attached.
provenance_coverage: float property ¶
Fraction of extracted fields that have a SourceSpan.
is_privacy_restricted: bool property ¶
True if a PrivacyProfile is attached and routing is not 'any'.
set_field(field_name: str, value: Any, confidence: float = 0.0, *, chunk_id: str | None = None, source_url: str | None = None, page: int | None = None, char_start: int | None = None, char_end: int | None = None, snippet: str | None = None) -> None ¶
Set an extracted field together with its confidence score and optional provenance span in a single call.
add_provenance(field_name: str, chunk_id: str, *, source_url: str | None = None, page: int | None = None, char_start: int | None = None, char_end: int | None = None, snippet: str | None = None) -> None ¶
Record a SourceSpan for field_name.
LegalDocMetadata = LegalDocumentMetadata module-attribute ¶
DocType ¶
Bases: str, Enum
Primary document classification.
SourceSpan ¶
Bases: BaseModel
Precise provenance reference for a single extracted field or text span.
Maps a piece of extracted information back to its exact origin so that consumers can verify, cite, or re-read the raw text.
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_id | str | Stable ID of the source chunk (assigned by ProvenanceTracker). |
source_url | str | None | Canonical URL of the source document. |
page | int | None | 1-based page number in the source document. |
char_start | int | None | Character offset of the match start within the full document. |
char_end | int | None | Character offset of the match end. |
snippet | str | None | Short verbatim excerpt (≤ 300 chars) from the source. |
LegalDocument ¶
Bases: BaseModel
General-purpose model for any legal document processed by Contractex.
Design principles¶
- Provenance first — every extracted field can be traced to its exact source location via
provenance[field_name]→SourceSpan. - Schema-agnostic extraction —
extracted_fieldsholds a plain dict of doc-type–specific values; callers overlay typed sub-schemas (e.g.PassportData) on top as needed. - Confidence aware —
field_confidencesmirrorsextracted_fieldswith per-field confidence scores consumed byConfidenceRouter.
Example::
doc = LegalDocument(
doc_type=DocType.STATUTE,
title="17 U.S.C. § 107 — Fair Use",
jurisdiction="US-Federal",
citation="17 U.S.C. § 107",
hierarchy_path=["Title 17", "Chapter 1", "§ 107"],
metadata=LegalDocumentMetadata(
source_url="https://www.law.cornell.edu/uscode/text/17/107"
),
)
doc.extracted_fields["governing_body"] = "Congress"
doc.field_confidences["governing_body"] = 0.97
doc.add_provenance("governing_body", chunk_id="chunk-0000-ab12",
page=1, snippet="enacted by Congress …")
content_hash: str | None property ¶
SHA-256 of full_text for deduplication. None if no text.
provenance_coverage: float property ¶
Fraction of extracted_fields that have a SourceSpan record.
add_provenance(field_name: str, chunk_id: str, *, source_url: str | None = None, page: int | None = None, char_start: int | None = None, char_end: int | None = None, snippet: str | None = None) -> None ¶
Record a SourceSpan for field_name.
set_field(field_name: str, value: Any, confidence: float, span: SourceSpan | None = None) -> None ¶
Convenience method to set an extracted field, its confidence, and optionally its provenance in one call.
to_json(file_path: str | None = None) -> str ¶
Serialise to JSON string, optionally writing to file_path.