Linked Open Data Modeling Language (LinkML)

1.10.0 · active · verified Thu Apr 16

LinkML is a powerful framework for defining data models, particularly for linked open data and semantic web applications. It allows users to define schemas using a YAML-based language, and then generate artifacts such as dataclasses, JSON schemas, ShEx schemas, and more, for various programming languages and data formats. It supports schema validation, data transformation, and integration with existing ontologies. The current version is 1.10.0, and it has an active development and release cadence, with major versions often introducing significant changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple LinkML schema as a string, load it, generate Python dataclasses from it, create an instance of a generated class, and then serialize that instance back into YAML. This showcases the core model definition, code generation, and data handling capabilities.

import os
from linkml_runtime.linkml_model.meta import SchemaDefinition
from linkml.generators.pythongen import PythonGenerator
from linkml_runtime.loaders import YAMLLoader
from linkml_runtime.utils.datautils import dump_yaml

# Define a simple LinkML schema in YAML string
schema_content = """
id: http://example.org/my_schema
name: my_schema
description: A simple LinkML schema example

prefixes:
  ex: http://example.org/my_schema/

default_prefix: ex

classes:
  Person:
    slots:
      - id
      - name
      - age

slots:
  id:
    identifier: true
    range: string
  name:
    range: string
  age:
    range: integer
    minimum_value: 0
"""

# 1. Load the schema definition
loader = YAMLLoader()
schema = loader.loads(schema_content, target_class=SchemaDefinition)
print(f"Successfully loaded schema: {schema.name}")

# 2. Generate Python dataclasses from the schema
gen = PythonGenerator(schema=schema)
python_code = gen.serialize()

# For quickstart, execute generated code in current namespace
# In a real application, you'd write this to a file and import it.
namespace = {}
exec(python_code, namespace)

# Get the generated 'Person' class
Person = namespace['Person']

# 3. Create an instance of the generated class
p = Person(id="P001", name="Alice Smith", age=30)
print(f"Created person instance: {p.name}")

# 4. Dump the instance to YAML
yaml_output = dump_yaml(p)
print("\n--- Generated YAML output ---")
print(yaml_output)

view raw JSON →