Skip to main content

Turns Harvard Dataverse Project metadatablocks schema and dataset JSON into Pydantic models.

Project description

dv_schema_models

Pydantic models for Dataverse metadata — parse the schema, load dataset exports, and validate field values against the schema.

[!CAUTION] This library is under active development and the API is not yet stable. Breaking changes may occur between releases. Please pin to a specific version in your pyproject.toml or requirements.txt if you want to avoid surprises.

Pre-requisites

  1. Python 3.13+

Installation

  1. With uv (recommended):
uv add dv_schema_models
  1. With pip:
pip install dv_schema_models

Concepts

Thing What it is
Schema /api/metadatablocks response — defines what fields can exist, their types, and rules
Dataset instance GET /api/datasets/:id response — the actual metadata values for one dataset
Record model A Pydantic model generated from the schema, used to validate instance values

Usage

1. Load and query the schema

import json
from dv_schema_models.dataverse_schema import load_schema

schema = load_schema(json.load(open("dv_schema.json")))

schema.block_names()                        # ['citation', 'geospatial', ...]
block = schema.get_block("citation")
block.fields.keys()                         # top-level field names
block.required_fields()                     # leaf fields where isRequired=True
block.all_leaf_fields()                     # flattened, including nested compound fields

field = block.get_field("keyword")
field.is_compound()                         # True — has childFields
field.iter_leaf_fields()                    # [keywordValue, keywordVocabulary, ...]

2. Load a dataset and read values

import json
from dv_schema_models.dataset_instance import load_dataset

dataset = load_dataset(json.load(open("ds_metadata.json")))

# Shortcut from the top level
dataset.get_value("citation", "title")      # plain string

# Or drill down
block = dataset.data.latestVersion.metadataBlocks.get("citation")
block.get_value("keyword")                  # unwrapped Python value (str / list / dict)
block.get_field("author").simple_value()    # same, from the DatasetFieldValue directly

3. Validate instance values against the schema

import json
from dv_schema_models.dataverse_schema import load_schema
from dv_schema_models.dataset_instance import load_dataset
from dv_schema_models.schema_driven_records import build_record_model, flatten_instance


schema = load_schema(json.load(open("dv_schema.json")))
dataset = load_dataset(json.load(open("ds_metadata.json")))

citation_schema = schema.get_block("citation")
CitationRecord = build_record_model(citation_schema)   # dynamic Pydantic model

block = dataset.data.latestVersion.metadataBlocks.get("citation")
raw = flatten_instance(block)              # {typeName: value, ...}
record = CitationRecord.model_validate(raw)

The generated model enforces field names, required/optional status, list wrapping for multiple=True fields, and int/float types where declared by the schema.

4. Discover available fields

# Fields actually present in this dataset instance
block = dataset.data.latestVersion.metadataBlocks.get("citation")
block.field_names()                            # e.g. ['title', 'author', 'keyword', ...]

# All fields the schema defines (including absent/optional ones)
schema.get_block("citation").all_leaf_fields().keys()

# After validation, access as typed attributes
record = CitationRecord.model_validate(flatten_instance(block))
record.title          # str
record.author         # list[...] for multiple=True compound fields
record.keyword        # None if not present in this dataset (optional fields default to None)
# Note: field names with dots become underscores — e.g. 'resolution.Spatial' → record.resolution_Spatial

Input file shapes

Schema — output of Dataverse /api/metadatablocks:

{"status": "OK", "data": [{"id": 10, "name": "citation", "fields": {...}}]}

Dataset — output of Dataverse GET /api/datasets/:id:

{"status": "OK", "data": {"latestVersion": {"metadataBlocks": {"citation": {"fields": [...]}}}}}

Citation

If you use this library in your work, please cite according to CITATION

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dv_schema_models-0.2.0a1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dv_schema_models-0.2.0a1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file dv_schema_models-0.2.0a1.tar.gz.

File metadata

  • Download URL: dv_schema_models-0.2.0a1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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}

File hashes

Hashes for dv_schema_models-0.2.0a1.tar.gz
Algorithm Hash digest
SHA256 51360615630a30f97a98592344a81ad681c1595026a827e20a2b95edd12784b8
MD5 125bcb94910d178db54d553ba1657540
BLAKE2b-256 5083875f2a1822bc5a14af6d94d1039c2a47fbdbccd8c6056281531edca9ed56

See more details on using hashes here.

File details

Details for the file dv_schema_models-0.2.0a1-py3-none-any.whl.

File metadata

  • Download URL: dv_schema_models-0.2.0a1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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}

File hashes

Hashes for dv_schema_models-0.2.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f251323ea09eb227c575c80b55bde45a55affd1c529628d949291b7095964fd
MD5 cc0e7bd15aef7f55a7c53928daace997
BLAKE2b-256 149fa5467db5ef4bd69fd8394a46cd13fe16138c040f164968fe18bb45f956d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page