Python datamodel packages
Every datamodel project gets a Python package next to its Java artifact. The datamodel pipeline does not need to know
about it: the pipeline already runs gradle publish jreleaserFullRelease in the generated Java project on main, and
for a project with projectType = DATAMODEL the plugin makes publish depend on publishKlokPythonDatamodel.
| Java |
Python |
dev.klok.klok.datamodel:klokai-v1:1.0.0-build.260923013036 |
klok-datamodel-klokai-v1==1.0.0.post260923013036 |
dev.klok.klok.datamodel.common.klokai_v1.KlokAiMessageV1 |
from klok_datamodel_klokai_v1 import KlokAiMessageV1 |
| published to the project's Maven registry |
published to the project's PyPI registry |
The distribution is <tenant>-datamodel-<artifact> and the import package is the same name with underscores. The
version is the Java version in PEP 440 form (-build. becomes .post), so pip installs it without --pre and newer
builds sort higher.
Installing a datamodel package
The packages are in the GitLab PyPI registry of each datamodel project. The group index of the tenant aggregates them:
pip install klok-datamodel-klokai-v1 \
--index-url "https://__token__:<token>@git.klok.dev/api/v4/groups/<group id>/-/packages/pypi/simple"
The token is a personal access token with read_api (by hand) or CI_JOB_TOKEN with the user gitlab-ci-token (in a
pipeline). Use --index-url, not --extra-index-url, so a name on public PyPI can never shadow the private one. Every
release of the datamodel pipeline publishes a new version, so pin the version in a service's requirements file the same
way a Java project follows the catalog.
What the package contains
klok_datamodel_klokai_v1/
__init__.py every class, plus KLOK_TENANT, KLOK_JAVA_ARTIFACT, KLOK_DOC_ID, KLOK_WIRE_SCHEMA_SOURCE
models.py one dataclass per record, one Enum per enum
_avro.py Avro binary encoding with fastavro
py.typed
schemas/reflected/<Record>.avsc the wire schema of each record class
A record class:
@dataclass
class KlokTaskStatusEventV1:
taskId: Optional[str] = None
status: Optional[KlokTaskStatusType] = None
sequence: int = 0 # a Java primitive keeps Java's default
timestamp: Optional[datetime] = None
...
KLOK_JAVA_CLASS: ClassVar[str] # the Java class it mirrors
KLOK_MODEL_NAME: ClassVar[str] # the simple name, which the topic name is derived from
KLOK_AVRO_SCHEMA: ClassVar[str] # the exact schema string Java registers on the topic
KLOK_DATA_SCHEMA: ClassVar[str | None] # urn:klok:schema:<docId>:<Record>:<Java version>, for ce-dataschema
def to_avro_bytes(self) -> bytes: ...
@classmethod
def from_avro_bytes(cls, data: bytes, writer_schema: str | None = None): ...
The package depends on fastavro only and knows nothing about the broker. The Klok broker client for Python
(klok-brokerclient, in klok/framework/klok-brokerclient-java) reads the KLOK_* attributes to name the topic,
register the schema and set ce-dataschema, and calls to_avro_bytes / from_avro_bytes for the payload.
Enum values are the symbols Java writes, which are the Java constant names (KlokAiRoleType.SYSTEM == "SYSTEM"), not
the values in the XSD. A field whose Avro name is a Python keyword gets a trailing underscore in Python (from_) and
keeps its name on the wire.
Where the schema comes from
The package is generated from the schema Java registers on the topic, not from the .avsc the pipeline derives from
the XSD. dumpKlokWireSchemas loads every public class of the datamodel in a separate JVM, calls Schema.AVRO(clazz)
with the pulsar-client version of the catalog (klokLibs.pulsarClient), and writes
build/klok/datamodel/wire-schemas.json. That is the call KlokBrokerClient makes, so the result is the wire schema by
construction:
- while the generated Java classes are plain POJOs, Pulsar derives the schema by reflection, which differs from the XSD
.avsc in namespace, field order, nullability and enum symbols (see "Publishing from TypeScript" in the broker client
documentation); the package then carries schemas/reflected/<Record>.avsc and KLOK_WIRE_SCHEMA_SOURCE = "reflected";
- once a datamodel is re-released with the schema embedded in the Java class (
SCHEMA$), Pulsar registers that one, and
the same dump yields it; the package then carries schemas/<Record>.avsc and KLOK_WIRE_SCHEMA_SOURCE = "embedded",
which is the layout the wire contract in klok-common describes.
No generator change is needed when the migration happens.
The dump JVM runs with the datamodel's classes and runtime class path plus the configuration klokWireSchemaTool, which
holds org.apache.pulsar:pulsar-client and org.apache.pulsar:pulsar-client-admin-api, both at the catalog's
pulsarClient version. pulsar-client 5.0.0 uses classes of pulsar-client-admin-api without declaring it as a
dependency, so the plugin adds it itself; the catalog needs no entry for it (see Design
decisions).
Tasks
| Task |
Does |
dumpKlokWireSchemas |
writes build/klok/datamodel/wire-schemas.json; shared with the TypeScript package |
buildKlokPythonDatamodel |
writes the source tree (pyproject.toml, README.md, src/<module>/) and dist/<wheel> under build/klok/python/ |
publishKlokPythonDatamodel |
uploads the wheel; publish depends on it |
Run them by hand in a generated datamodel project:
./gradlew updateKlokLibsToml buildKlokPythonDatamodel
pip install build/klok/python/dist/*.whl
Publishing target
| Setting |
Gradle property |
Environment variable |
Default |
| Upload URL |
klokPypiUploadUrl |
KLOK_PYPI_UPLOAD_URL |
$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/pypi |
| User |
klokPypiUsername |
KLOK_PYPI_USERNAME |
gitlab-ci-token with the job token, __token__ otherwise |
| Token |
klokPypiToken |
KLOK_PYPI_TOKEN |
CI_JOB_TOKEN, then klokGitAuthToken / GITLAB_TOKEN |
| Switch off |
klokPythonDatamodel=false |
KLOK_PYTHON_DATAMODEL=false |
on |
Without an upload URL (a laptop, outside a pipeline) the publish task reports where the wheel is and does nothing, so
./gradlew publish still works. A registry that refuses the upload fails the build: a release without its Python
package is not a complete release. klokPythonDatamodel=false is the way out if a release has to go out without it.
Known limits
- The package is not recorded in the dependency catalog, which is Gradle's format; a Python service pins the version in
its own requirements (klok/framework/klok-gradle-plugin#4).
from_avro_bytes decodes with the package's schema unless it is given the producer's schema (writer_schema=). The
broker client does that for you: it fetches the producer's schema version from the topic once and passes it.
- Publishing to public PyPI (
klok-integration-datamodel-* for public datamodels) is not done yet
(klok/framework/klok-gradle-plugin#3).
- The TypeScript package of the datamodel is generated from the same
build/klok/datamodel/wire-schemas.json, for the
reasons above; see TypeScript datamodel packages.