Skip to main content

Generate typed code interfaces from RDF/Turtle ontologies (e.g., schema.org).

Project description

rdfcodegen

Generate typed code interfaces from RDF/Turtle ontologies (e.g., schema.org).

Currently supports TypeScript output. Additional language generators planned.

In this README :point_down:

Features

rdfcodegen parses RDF/Turtle ontology files (like schema.org) and generates language-specific code with full type definitions, documentation, and inheritance.

:rocket: What it produces:

  • Type-safe interfaces/classes extracted from RDFS/OWL class hierarchies.
  • JSDoc/Docstring comments pulled from rdfs:comment annotations.
  • Automatic dependency resolution — request one class and get all its parent types and field types transitively.
  • Primitive type mapping (e.g., schema:Textstring, schema:Numbernumber).

:computer: Supported output languages:

  • TypeScriptinterface definitions with optional properties (?) and array types.
  • Go (planned)
  • Python (planned)

Installation

pip install rdfcodegen

Requires Python 3.9+ and rdflib.

Usage

Command line

Generate TypeScript interfaces for all schema.org types:

rdfcodegen --ttl https://schema.org/version/latest/schemaorg-current-https.ttl \
           --namespace https://schema.org/ \
           --out schema.ts

Generate only specific classes (and everything they depend on):

rdfcodegen --class schema:Person --class schema:Organization --out schema.ts

Write to stdout (-):

rdfcodegen > schema.ts

CLI options:

Option Default Description
--ttl schema.org/version/latest/schemaorg-current-https.ttl URL or path to Turtle file
--namespace https://schema.org/ Ontology namespace URI
--out - Output file (- for stdout)
--class (all classes) Generate specific classes (repeatable)

Python API

from rdfcodegen import TypescriptGenerator

gen = TypescriptGenerator()

# Generate all types
ts_code = gen.generate(
    ttl_url="https://schema.org/version/latest/schemaorg-current-https.ttl",
    namespace_url="https://schema.org/",
)

# Generate a subset
ts_code = gen.generate(
    chosen=["schema:Person", "schema:Event"],
    ttl_url="https://schema.org/version/latest/schemaorg-current-https.ttl",
    namespace_url="https://schema.org/",
)

with open("schema.ts", "w") as f:
    f.write(ts_code)

Filtering specific classes

When you specify classes with --class (or the chosen parameter), the generator:

  1. Includes the requested classes.
  2. Recursively includes all parent types up the rdfs:subClassOf hierarchy.
  3. Recursively includes all field types referenced via schema:rangeIncludes.
  4. Skips primitive types (e.g., schema:Text, schema:Number) — these map directly to language primitives.

This ensures the generated output is always self-contained and compilable.

How it works

  1. Parse — The Turtle file is loaded into an RDF graph using rdflib.
  2. Discover classes — All subjects typed as rdfs:Class within the target namespace are collected.
  3. Traverse hierarchyrdfs:subClassOf relationships are followed to build the class hierarchy.
  4. Collect propertiesschema:domainIncludes and schema:rangeIncludes are used to determine which fields belong to which classes and what types they return.
  5. Extract documentationrdfs:comment values become JSDoc/docstring comments.
  6. Resolve dependencies — If filtering is requested, a dependency graph walk collects all transitive type references.
  7. Generate output — A language-specific generator (e.g., TypescriptGenerator) emits the final code.

TypeScript output example

From schema:Person, the generator produces:

// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT DIRECTLY.

/**
 * A person (alive, dead, undead, or fictional).
 */
export interface SchemaPerson extends SchemaThing {
  /**
   * An additional name for a Person, can be used for a middle name.
   */
  additionalName?: string[];
  /**
   * Physical address of the item.
   */
  address?: (SchemaPostalAddress | string)[];
  /**
   * Date of birth.
   */
  birthDate?: string[];
  // ... more fields
}

Roadmap

  • Typescript types generator
    • Helper function to generate JSON-LD from those structs
  • Go struct generator with JSON tags
  • Python dataclass/pydantic generator
  • Choose other languages

Contributing

If you find a bug :bug:, please open a bug report. If you have an idea for an improvement or new feature :rocket:, please open a feature request

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

rdfcodegen-0.1.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

rdfcodegen-0.1.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file rdfcodegen-0.1.1.tar.gz.

File metadata

  • Download URL: rdfcodegen-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • 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":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rdfcodegen-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aa51fe575ae9d6c15ca9263096443635fa4c36090ec08d17fa6b2d633e5cbd37
MD5 1bb5c52a1ff537b973bb4cde48538722
BLAKE2b-256 7c2715c0021b3f90ddb543cc228525033607fee1c72cd8247fb105ea6c7e4d0f

See more details on using hashes here.

File details

Details for the file rdfcodegen-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: rdfcodegen-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • 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":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for rdfcodegen-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 83a42675729a6b6c03794b21f5c8dc39497160e57a1ee69788e53e105be54b21
MD5 7540b360394491112b9e8d7322681cd5
BLAKE2b-256 36869bc10ebab3464ff31cd785703d26558f622c879753f8e911e73210eb9b94

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