{"id":7371,"library":"linkml-runtime","title":"LinkML Runtime","description":"LinkML Runtime is the core Python runtime environment for LinkML, the Linked open data modeling language. It provides essential functionality for working with LinkML schemas, including schema loading, data validation, serialization, and deserialization. The current version is 1.10.0, and it maintains a close release cadence with the broader LinkML ecosystem, typically featuring bug fixes and enhancements aligned with LinkML core developments.","status":"active","version":"1.10.0","language":"en","source_language":"en","source_url":"https://github.com/linkml/linkml-runtime","tags":["linkml","schema","datamodeling","validation","pydantic","serialization"],"install":[{"cmd":"pip install linkml-runtime","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for data modeling and validation; LinkML Runtime relies heavily on Pydantic models for its internal operations and generated classes. Version 2.x is required for linkml-runtime 1.x.","package":"pydantic>=2.0.0","optional":false}],"imports":[{"symbol":"SchemaView","correct":"from linkml_runtime.utils.schemaview import SchemaView"},{"symbol":"YAML_LOADER","correct":"from linkml_runtime.loaders.yaml_loader import YAML_LOADER"},{"symbol":"JSON_LOADER","correct":"from linkml_runtime.loaders.json_loader import JSON_LOADER"},{"symbol":"dict_to_object","correct":"from linkml_runtime.utils.dictutils import dict_to_object"},{"symbol":"object_to_dict","correct":"from linkml_runtime.utils.dictutils import object_to_dict"},{"note":"Core LinkML metaclasses are nested under the 'meta' module.","wrong":"from linkml_runtime.linkml_model import SchemaDefinition","symbol":"SchemaDefinition","correct":"from linkml_runtime.linkml_model.meta import SchemaDefinition"}],"quickstart":{"code":"import os\nfrom linkml_runtime.utils.schemaview import SchemaView\nfrom linkml_runtime.utils.dictutils import dict_to_object, object_to_dict\n\n# 1. Define a simple LinkML schema (YAML string)\nschema_yaml = \"\"\"\nid: https://example.org/my_data_model\nname: my_data_model\nprefixes:\n  ex: https://example.org/my_data_model/\ndefault_prefix: ex\n\nclasses:\n  Person:\n    slots:\n      - id\n      - name\n      - age\n    required:\n      - id\n      - name\n  \nslots:\n  id:\n    range: string\n    identifier: true\n  name:\n    range: string\n  age:\n    range: integer\n    minimum_value: 0\n\"\"\"\n\n# 2. Load the schema into a SchemaView object\nschema_view = SchemaView(schema_yaml)\n\n# 3. Define a simple data instance (Python dictionary)\nperson_data = {\n    \"id\": \"P001\",\n    \"name\": \"Alice Wonderland\",\n    \"age\": 30\n}\n\n# 4. Convert the dictionary data to a Python object based on the schema\n# `dict_to_object` uses the schema definition to instantiate a Pydantic-backed object.\nperson_object = dict_to_object(person_data, target_class=schema_view.get_class(\"Person\"), schemaview=schema_view)\n\nprint(f\"Original dict: {person_data}\")\nprint(f\"Converted object: {person_object}\")\nprint(f\"Object ID: {person_object.id}\")\nprint(f\"Object Name: {person_object.name}\")\n\n# Demonstrate implicit validation (attempting to convert invalid data)\ninvalid_person_data = {\n    \"id\": \"P002\",\n    \"name\": None, # 'name' is required and cannot be None\n    \"age\": -5     # 'age' must be non-negative\n}\n\ntry:\n    print(\"\\nAttempting to convert invalid data...\")\n    dict_to_object(invalid_person_data, target_class=schema_view.get_class(\"Person\"), schemaview=schema_view)\nexcept Exception as e:\n    print(f\"Caught expected validation error: {e.__class__.__name__}: {e}\")\n\n# 5. Convert the object back to a dictionary\nconverted_back_dict = object_to_dict(person_object, schemaview=schema_view)\nprint(f\"\\nObject converted back to dict: {converted_back_dict}\")","lang":"python","description":"This quickstart demonstrates how to load a LinkML schema from a YAML string using `SchemaView`, convert a Python dictionary into a schema-compliant object using `dict_to_object`, access its attributes, and handle validation errors. It also shows how to convert an object back to a dictionary."},"warnings":[{"fix":"Upgrade your project's Pydantic dependency to `pydantic>=2.0.0` or use an older `linkml-runtime` version (pre-1.0.0) if Pydantic V1 is strictly required by other dependencies.","message":"LinkML Runtime versions 1.0.0 and above require Pydantic V2. If your project uses Pydantic V1, installing `linkml-runtime>=1.0.0` will lead to `ImportError` or `ValidationError` incompatibilities.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Validate your LinkML schema using the `linkml-validate` command-line tool (from `linkml`) or a dedicated LinkML IDE/linter before attempting to load it programmatically. Ensure all class, slot, and type definitions are correctly formatted and referenced.","message":"Errors in LinkML schema definition (YAML/JSON syntax, incorrect LinkML elements) can result in obscure `jsonschema` or `Pydantic` errors during schema loading or when generating models. These can be hard to debug without careful inspection of the schema structure.","severity":"gotcha","affected_versions":"all"},{"fix":"For very strict validation, you might need to combine `dict_to_object` with an explicit validation step or directly use the Pydantic models generated by `linkml-generator` for your schema, which offer more granular control over validation behavior (e.g., `extra='forbid'`).","message":"By default, `dict_to_object` can sometimes be more permissive than anticipated, silently ignoring extra fields or performing type coercion. For stricter validation, ensure your schema explicitly defines all expected fields and types.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Upgrade Pydantic to version 2 or newer: `pip install 'pydantic>=2.0.0'`","cause":"You are using `linkml-runtime>=1.0.0` with an older Pydantic V1 installation (`pydantic<2.0.0`).","error":"ImportError: cannot import name 'BaseModel' from 'pydantic.main' (C:\\Users\\...\\site-packages\\pydantic\\main.py)"},{"fix":"Inspect your input data and the corresponding LinkML schema definition. Ensure all `required` slots are present and values conform to `range` and other constraints.","cause":"The data being converted is missing a required field, or a field has an invalid type/value according to the LinkML schema's rules (e.g., `required: true`, `minimum_value`, `pattern`).","error":"ValidationError: 1 validation error for Person\\nname\\n  field required (type=value_error.missing)"},{"fix":"Verify the file path. Use an absolute path or ensure the relative path is correct from where your script is executed. Check for typos in the filename.","cause":"The path provided to `SchemaView` or loaders (e.g., `YAML_LOADER.load()`) for your LinkML schema file is incorrect or the file does not exist at that location.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'my_schema.yaml'"}]}