migrating the DANDI archive metadata models
dandi-schema — today's subjectmodels.py
# https://github.com/dandi/dandi-schema/blob/0.14.0/dandischema/models.py#L1702-L1720
class Dandiset(CommonModel):
...
name: str = Field(
title="Dandiset title",
description="A title associated with the Dandiset.",
max_length=150,
json_schema_extra={"nskey": "schema"},
)
description: str = Field(
description="A description of the Dandiset",
max_length=10000,
json_schema_extra={"nskey": "schema"},
)
contributor: List[
Annotated[Union[Person, Organization], Field(discriminator="schemaKey")]
] = Field(
title="Dandiset contributors",
description="People or Organizations that have contributed to this Dandiset.",
json_schema_extra={"nskey": "schema"},
min_length=1,
)
Dandiset, Asset, Person, Organization, Activity, ...
— contributors, funding, license, species, approaches, techniques, provenancenskey, title, readOnly: hints for JSON-LD and the web formmodels.py is the only place the schema is written
%%{init: {'theme': 'base', 'flowchart': {'htmlLabels': false}, 'themeVariables': {'fontSize': '22px', 'primaryColor': '#fdf6e3', 'lineColor': '#555'}}}%%
flowchart LR
NWB["NWB files"]
BIDS["BIDS datasets<br/>+ HED"]
Z["OME-Zarr"]
X["dandi-cli<br/>extract + validate"]
A["Asset records<br/>automatic extracts"]
R["one validation report<br/>NWB Inspector, BIDS,<br/>HED, OME-Zarr, ..."]
H["humans + AI<br/>in the Meditor"]
D["Dandiset record<br/>curated"]
S["dandi-schema<br/>models"]
NWB --> X
BIDS --> X
Z --> X
X --> A
X --> R
H --> D
A --> S
D --> S
click X href "https://github.com/dandi/dandi-cli/tree/0.84.0/dandi/validate" _blank
click A href "https://github.com/dandi/dandi-cli/blob/0.84.0/dandi/metadata/nwb.py" _blank
click S href "https://github.com/dandi/dandi-schema/blob/0.14.0/dandischema/models.py" _blank
style S fill:#f7dfd3,stroke:#c0392b,stroke-width:2px
dandi/schema: per schema version,
dandiset.json, asset.json, published-*.json, context.jsonmodels.py (publish_model_schemata())validate() — on upload and before publishjson_validation=True)
{
"@context": "https://raw.githubusercontent.com/dandi/schema/master/releases/0.6.0/context.json",
"id": "DANDI:000003/0.261122.2143",
"schemaKey": "Dandiset",
"doi": "10.48324/dandi.000003/0.261122.2143",
"license": ["spdx:CC-BY-4.0"],
"about": [{"schemaKey": "Anatomy", "name": "hippocampus",
"identifier": "UBERON:0002421"}],
"contributor": [{"schemaKey": "Person", "name": "Senzai, Yuta",
"identifier": "0000-0002-2990-9889",
"roleName": ["dcite:Author", "dcite:ContactPerson", ...],
"affiliation": [{"identifier": "https://ror.org/005dvqh91", ...}]},
...],
...
}
%%{init: {'theme': 'base', 'flowchart': {'htmlLabels': false}, 'themeVariables': {'fontSize': '22px', 'primaryColor': '#fdf6e3', 'lineColor': '#555'}}}%%
flowchart TB
P["models.py<br/>Pydantic models<br/>+ custom validators"]
J["dandi/schema<br/>JSON Schema per release<br/>+ JSON-LD context"]
CLI["dandi-cli<br/>extract, validate, upload"]
V["dandischema.validate()<br/>Pydantic and/or JSON Schema"]
BE["archive backend / API<br/>Celery validation → Postgres"]
ME["Meditor<br/>vjsf web form"]
DLP["Dataset landing page"]
LD["JSON-LD on S3,<br/>DataCite DOIs"]
PUB(["Publish<br/>(blocked if invalid)"])
P -->|"GitHub CI per release"| J
P --> CLI
P --> V
J -->|"any version"| V
V --> BE
CLI -->|"asset metadata"| BE
J -->|"generates form"| ME
ME -->|"user edits"| BE
BE --> DLP
BE --> LD
BE --> PUB
click P href "https://github.com/dandi/dandi-schema/blob/0.14.0/dandischema/models.py" _blank
click J href "https://github.com/dandi/schema/tree/master/releases" _blank
click CLI href "https://github.com/dandi/dandi-cli/tree/0.84.0" _blank
click V href "https://github.com/dandi/dandi-schema/blob/0.14.0/dandischema/metadata.py#L241" _blank
click BE href "https://github.com/dandi/dandi-archive/blob/v1.2.6/dandiapi/api/services/metadata/__init__.py" _blank
click ME href "https://github.com/dandi/dandi-archive/tree/v1.2.6/web/src/components/Meditor" _blank
click DLP href "https://github.com/dandi/dandi-archive/tree/v1.2.6/web/src/components/DLP" _blank
click LD href "https://dandiarchive.s3.us-east-2.amazonaws.com/dandisets/000003/0.261122.2143/dandiset.jsonld" _blank
style P fill:#f7dfd3,stroke:#c0392b,stroke-width:2px
style J fill:#eee8f7,stroke:#6a4b8a
boxes are links; adapted from docs.dandiarchive.org → Integrate External Services
field_validator/model_validators never reach JSON Schema
→ Meditor happily accepts what the backend then rejects (and blocks publishing)
# https://github.com/dandi/dandi-schema/blob/0.14.0/dandischema/models.py#L1653-L1664
@field_validator("contributor")
@classmethod
def contributor_musthave_contact(
cls, values: List[Union[Person, Organization]]
) -> List[Union[Person, Organization]]:
contacts = []
for val in values:
if val.roleName and RoleType.ContactPerson in val.roleName:
contacts.append(val)
if len(contacts) == 0:
raise ValueError("At least one contributor must have role ContactPerson")
return values
validate() can check both, to be conservativemigrate() them up,
or down by stripping newer fields where possiblepattern, required,
rules, ...)
→ the same checks everywhere: CLI, backend, web formDone when all three hold (playbook):
Evidence beyond unit tests: validate the metadata of all archived Dandisets with both
(dashboard,
Claude skill dandi-linkml-validation-report)
Only then flip: models.yaml becomes the source, models.py is generated.
LinkML workshop, Berkeley, February 2024
pydantic2linkml -M dandischema/models_merge.yaml -O dandischema/models_overlay.yaml dandischema.models \
| sed ... | tools/linkml_conversion_tools/sanitize-yaml > dandischema/models.yaml
notes: right in models.yaml — an inventory of the gaps-M/-O: what is not (yet) in Pydantic — prefixes, designates_type, range overridessanitize-yaml: drop notes we triaged as acceptablegen-pydantic + one Jinja override: enum names without COLON munging
# models_merge.yaml
slots:
schemaKey:
designates_type: true
required: true
classes:
BareAsset:
slot_usage:
wasGeneratedBy:
range: Any
Dandiset:
slot_usage:
wasGeneratedBy:
range: Project
%%{init: {'theme': 'base', 'flowchart': {'htmlLabels': false}, 'themeVariables': {'fontSize': '22px', 'primaryColor': '#fdf6e3', 'lineColor': '#555'}}}%%
flowchart LR
M["models.py<br/>Pydantic, from master"]
Q["patch-queue<br/>branches"]
MG["models_merge.yaml<br/>models_overlay.yaml"]
P2L["pydantic2linkml<br/>+ sanitize-yaml"]
Y["models.yaml<br/>LinkML + notes"]
LK["LinkML<br/>generators"]
GP["gen-pydantic<br/>+ enum template"]
GJ["gen-json-schema"]
CMP(["analyze:<br/>tests, schema diff,<br/>all Dandisets"])
M --> P2L
Q --> P2L
MG --> P2L
P2L --> Y
Y --> LK
LK --> GP --> CMP
LK --> GJ --> CMP
CMP -. "make LinkML-friendlier & regenerate" .-> M
CMP -. "issues & PRs" .-> LK
click M href "https://github.com/dandi/dandi-schema/blob/master/dandischema/models.py" _blank
click P2L href "https://github.com/dandi/pydantic2linkml" _blank
click Y href "https://github.com/dandi/dandi-schema/blob/linkml-auto-converted/dandischema/models.yaml" _blank
click LK href "https://github.com/linkml/linkml" _blank
click CMP href "https://github.com/dandi/dandisets-linkml-status/blob/main/reports/linkml_translation/dandi/summary.md" _blank
style Y fill:#eee8f7,stroke:#6a4b8a,stroke-width:2px
style P2L fill:#f7dfd3,stroke:#c0392b
style LK fill:#e3f0e3,stroke:#2e7d32
linkml-conversion
(dandi-schema#387: tooling), tracking master
+ a “poor man's patch queue” of branches (e.g. remove-discriminated-unions)linkml-auto-converted
(dandi-schema#381: preview, never to be merged)
driver: tools/linkml_conversion ·
output: models.yaml
| Merged PRs | Issues (selected) | |
|---|---|---|
| Isaac To | linkml#3537 gen-json-schema emits readOnlylinkml#2363 validation results carry the originating error linkml#2323, linkml-runtime#340 module-level loggers linkml-runtime#338, linkml-runtime#341 SchemaBuilder fixes |
linkml#3604 JSON Schema draft 2020-12 linkml#3719 value_presence: ABSENT in ruleslinkml#3377 slot title not emitted to JSON Schemalinkml#2155 nullability in generated Pydantic linkml#2215 no validation of uri rangelinkml#2104 exact_cardinality,
linkml#2206 equals_string_in without effectlinkml#3439–3442 JSON vs YAML loader asymmetries |
| John Lee | linkml#3839 uncompactable URI type designators linkml#3843 gen-json-schema: URI form of uriorcurie |
linkml#3836 uriorcurie type designator round-trip
(mostly from ORINOCO-Lite work) |
required ·
✔ dandi-schema#406 range ·
✔ dandi-schema#407 pattern ·
✔ dandi-schema#413 readonly ·
◯ dandi-schema#438 revisit dandi-schema#406 after dandi-schema#437AnyHttpUrl wrap validator ·
◯ dandi-schema#391 “after” validation functionsmax_length ·
✔ dandi-schema#418 ±inf / NaNtitles (used by the web form) ·
◯ pydantic2linkml#57 notes inherited by subclassesstate as of 2026-12-01; the links are live
required, ...slot_usage may only add restrictions →
54 “cannot express in a slot_usage entry”, 41 of them on schemaKey| What changed in a subclass | How we handled it |
|---|---|
schemaKey: Literal["Person", "Organization", ...] |
single-value Literals in Pydantic (dandi-schema#437);
designates_type: true via merge file; drop the per-class slot_usage |
required False → True |
pydantic2linkml (pydantic2linkml#66) + LinkML contract test (dandi-schema#405) |
range narrowed (wasGeneratedBy: Activity → Project) |
models_merge.yaml + contract test (dandi-schema#406) |
pattern replaced (PublishedDandiset.id) |
consolidated Asset/Dandiset models in Pydantic (dandi-schema#419) — no more Published* classes |
| Pydantic | LinkML | Resolution |
|---|---|---|
max_length=150 on str | no direct support (linkml#4155, open) | separate length pattern under all_of
(pydantic2linkml#63, dandi-schema#395);
gen-pydantic does not enforce it |
datetime microseconds “truncate” | no equivalent | accepted (dandi-schema#393) |
float allowing ±inf / NaN | not supported | accepted (dandi-schema#418) |
AnyHttpUrl wrap validator | range: uri |
open: to test in both generators (dandi-schema#390) |
per-field title overrides (used by the Meditor) | title on slot / slot_usage |
translated (dandi-schema#394) |
| discriminated unions | no construct | patch-queue branch remove-discriminated-unions |
@field_validator / @model_validator
(ensure_contact_person_has_email, digest_check, ...) | rules, partially |
open (dandi-schema#391) — next slide |
rules (e.g. “if datePublished then doi required”):
gen-json-schema emits if/then and rejects;
gen-pydantic keeps them as inert linkml_meta and accepts
→ custom template, upstream fix, or hand-written validatorsall_of slot expressions: the max_length pattern (Gotcha #2) is an
allOf in JSON Schema, but only linkml_meta in gen-pydantic output;
and its Python-only regex syntax ({,150}, \Z) does not compile in the Meditor's Ajv (/u regexps)assetsSummary.numberOfBytes, digest["dandi:sha2-256"]) are not cleanly expressible as rulesgen-json-schema hard-codes draft 2019-09; Pydantic and the frontend (Ajv) use 2020-12
— mostly cosmetic, since the Meditor strips $schema, until a tuple array appearsdesignates_type, a superclass-typed slot downcasts a Project to Activity (Pydantic)
or rejects its extra fields (JSON Schema)details, evidence and confidence: playbook findings.md
rules that both generators honor (dandi-schema#391)models.yaml is the single source of truthnotes drive the
issue tracker