Portable SDK configuration¶
Python and the OpenMedKit ports for Swift, Kotlin and the web spell the same settings differently today. The portable SDK configuration is one small JSON document that every surface reads with the same key names, types, bounds and defaults. Version 1 has six keys and rejects any other key.
| Key | Type and bounds | Default | Python source |
|---|---|---|---|
confidence_threshold | number in [0, 1] | 0.5 | per-call confidence_threshold argument of extract_pii (default 0.5) and deidentify (default 0.7); not an OpenMedConfig field |
local_only | boolean | true | OpenMedConfig.local_only (Python default false; OPENMED_OFFLINE turns it on) |
model_id | repository id or null | null (kit default model) | OpenMedConfig.pii_model |
model_revision | revision or null; needs a non-null model_id | null | OpenMedConfig.pii_model_revision |
onnx_variant | auto, int8, fp16, fp32 | auto | OpenMedConfig.onnx_variant |
policy_profile | one of the portable policy profiles | hipaa_safe_harbor | per-call policy argument of deidentify; not an OpenMedConfig field |
The schema is openmed/core/schemas/json/sdk_config.schema.json ($id https://openmed.life/schemas/sdk_config.schema.json). Types and enums match openmed/core/config.schema.json. The portable document is stricter in three places:
local_onlydefaults totrue, so a kit that reads an empty document stays offline. The PythonOpenMedConfigdefault staysfalse.model_idmust look like a Hub repository id (namespace/nameorname, ASCII letters, digits,.,_and-, no..). Filesystem paths and URLs are refused because they are not portable.model_revisionuses the same characters without/, and only makes sense with amodel_id; on its own it fails withmissing_dependency.
policy_profile accepts the canonical names in openmed.core.portable_policy.PORTABLE_PROFILES: australia_privacy_act, canada_pipeda, clinical_minimal_redaction, gdpr_art9_health, gdpr_pseudonymization, hipaa_expert_review_assist, hipaa_safe_harbor, research_limited_dataset and strict_no_leak. Aliases and Python-only profiles are refused.
onnx_variant is ignored by runtimes that do not load ONNX weights, such as the Swift kit's MLX and Core ML backends.
Spelling in each kit today¶
OpenMedKit Swift reads this document with OpenMedSDKConfig (see Swift). The Kotlin and web loaders are separate work (#5388 Kotlin, #5389 web). Without a loader, the same intent is spelled like this:
| Key | Swift (OpenMedKit) | Kotlin (OpenMedKit) | TypeScript (openmedkit-web) |
|---|---|---|---|
confidence_threshold | confidenceThreshold: per call, default 0.5 | confidenceThreshold per call, default 0.5f | ExtractPiiOptions.threshold, default 0 |
local_only | allowNetworkAccess: per initializer, inverted, default false | ArtifactAcquisitionRequest.networkConsent, default DENIED | offlineOnly, LoadModelOptions.localFilesOnly, allowRemoteModels |
model_id | OpenMedModelStore.downloadMLXModel(repoID:) | ArtifactRef.repoId | ExtractPiiOptions.model |
model_revision | revision:, default "main" | ArtifactRef.revision | LoadModelOptions.revision |
onnx_variant | not used (MLX and Core ML) | OpenMedKit.fromDirectory(variant =), default "int8" | LoadOnnxModelOptions.variant |
policy_profile | deidentify(_:policy:), required | deidentify(policy =), default hipaa_safe_harbor | loadPortablePolicy(name), default hipaa_safe_harbor |
The defaults that differ today (the web threshold of 0, the Kotlin variant of int8) are what the portable defaults replace once each loader lands.
Web package¶
The openmed web package reads the document with parseSdkConfig(value), which takes a parsed JSON value and returns a frozen configuration with all six keys. validateSdkConfig(value) returns the ordered issues without throwing; parseSdkConfig throws SdkConfigError, whose issues are the same { code, field } list and whose message names codes and known fields only. The existing option types are unchanged; two helpers map onto them:
import {
deidentify,
loadPortablePolicy,
parseSdkConfig,
toExtractPiiOptions,
} from "openmed";
const config = parseSdkConfig(JSON.parse(configText));
const result = await deidentify(note, toExtractPiiOptions(config));
const policy = loadPortablePolicy(config.policy_profile);
| Key | Web option |
|---|---|
confidence_threshold | ExtractPiiOptions.threshold |
local_only | LoadModelOptions.localFilesOnly: true and allowRemoteModels: false; false sets the opposite |
model_id | ExtractPiiOptions.model; null keeps DEFAULT_MODEL_ID |
model_revision | LoadModelOptions.revision; null keeps the loader's revision (DEFAULT_MODEL_REVISION for the default model) |
onnx_variant | LoadOnnxModelOptions.variant; auto keeps the loader default (int8) |
policy_profile | not an extraction option; pass it to loadPortablePolicy() |
toLoadModelOptions(config) returns the loader options and toExtractPiiOptions(config) returns threshold, model and those loaderOptions. Both revalidate their input. local_only: true never downloads from the Hub; a Hub id still resolves against the local model path and the Transformers.js cache. The package's stricter offlineOnly mode, which refuses Hub ids outright, stays a separate opt-in. tests/web/test_sdk_config.spec.ts runs every shared vector.
Validation and error codes¶
Validation fails closed. Every problem is an issue with a code and a field. field is one of the six keys, or null for document-level issues. Issues never contain an input value or a caller-supplied key name.
| Code | Field | Meaning |
|---|---|---|
config_not_object | null | The document is not a JSON object |
unknown_field | null | At least one key is not one of the six keys |
wrong_type | key | Wrong JSON type (true is not a number; null only for model_id and model_revision) |
out_of_range | key | confidence_threshold outside [0, 1] |
not_allowed_value | key | onnx_variant or policy_profile outside its enum |
invalid_format | key | model_id or model_revision does not match its pattern |
missing_dependency | model_revision | A revision is set while model_id is absent or null |
Issues come in a fixed order so every runtime can return the same list: config_not_object alone when the document is not an object; otherwise at most one unknown_field, then at most one issue per key in alphabetical key order.
Python API¶
from openmed.core.config import OpenMedConfig
from openmed.core.sdk_config import resolve_sdk_config, validate_sdk_config
validate_sdk_config({"local_only": "yes"})
# (SdkConfigIssue(code='wrong_type', field='local_only'),)
resolve_sdk_config({"model_id": "OpenMed/OpenMed-PII-ClinicalE5-Small-33M-v1-mlx"})
# all six keys, absent ones filled with the defaults above
OpenMedConfig.from_profile("low_resource").to_sdk_config(policy_profile="strict_no_leak")
OpenMedConfig.to_sdk_config(confidence_threshold=0.5, policy_profile="hipaa_safe_harbor") returns the validated subset of a config. It raises SdkConfigError when a value is outside the portable subset, for example a pii_model that is a local path.
Kotlin API¶
The Android OpenMedKit reads the document with OpenMedSdkConfig:
val config = OpenMedSdkConfig.parse(configJson) // throws OpenMedSdkConfigException
val issues = OpenMedSdkConfig.validate(configJson) // List<OpenMedSdkConfigIssue>
val openMed = OpenMed(backend = null, config = config)
val downloader = ModelDownloader(cache, config)
validate returns the same ordered issues as Python. Each issue holds only code and field, and text that is not a JSON object yields config_not_object. OpenMedSdkConfigException.issues carries the same list, and its message names only codes and keys. OpenMedSdkConfig.DEFAULT is the resolved empty document.
OpenMed(backend, config) uses confidence_threshold as the default threshold of analyzeText, extractPII and extractPIIChunked. ModelDownloader(cache, config) still serves a cached model when local_only is true, the portable default. For a model that is not cached, it throws ModelDownloadException before calling the HuggingFaceModelClient. When download gets no revision and the entry is model_id, it requests model_revision. The existing constructors are unchanged and keep their current behaviour. onnx_variant and policy_profile are exposed as properties for the caller to pass to OpenMedKit.fromDirectory and PolicyProfiles.load.
Swift (OpenMedKit)¶
OpenMedSDKConfig is a Codable, Sendable value with one immutable property per key: confidenceThreshold, localOnly, modelID, modelRevision, onnxVariant (ONNXVariant) and policyProfile (PolicyProfile, raw values as above). Decoding is strict and returns the issues above in the same order, as OpenMedSDKConfigError.issues. Data that is not JSON at all also fails with config_not_object.
OpenMedSDKConfig.load reads strict RFC 8259 JSON, like the Kotlin and web loaders. As there and in Python's json.loads, the last value of a repeated key wins and a number too small for a Double resolves to 0. A byte-order mark, comments, trailing commas, unquoted keys, NaN, Infinity, text after the object and nesting deeper than 512 levels fail with config_not_object. Decoding with JSONDecoder directly uses Foundation's own grammar, which keeps the first value of a repeated key, so read configuration text with load.
let url = Bundle.main.url(forResource: "openmed", withExtension: "json")!
let config = try OpenMedSDKConfig.load(contentsOf: url) // or OpenMedSDKConfig.load(data)
let openmed = try await OpenMed.load(
backend: .mlx(modelDirectoryURL: modelDirectory),
config: config // allowNetworkAccess = !config.localOnly
)
let result = try openmed.deidentify(
text,
policy: config.policyProfile.rawValue,
confidenceThreshold: Float(config.confidenceThreshold)
)
OpenMed(backend:config:maxSeqLength:tokenizerPin:) and OpenMed.load(backend:config:maxSeqLength:tokenizerPin:) map the configuration onto the existing initializers with allowNetworkAccess = !config.localOnly; the existing signatures are unchanged. With the default local_only: true, initialization makes no network request, even when a tokenizer pin would allow a download. As in Python, confidence_threshold and policy_profile are per-call settings, and onnx_variant is ignored by the Core ML and MLX backends. Pass modelID and modelRevision to OpenMedModelStore, and only download when localOnly is false. The typed initializer OpenMedSDKConfig(confidenceThreshold:...) applies the same bounds and formats. OpenMedSDKConfig.validate(_:) returns the issue list without throwing.
Shared vectors¶
tests/fixtures/parity/sdk_config_v1.json is the cross-runtime contract. It is synthetic and holds the field list, the defaults, the closed issue-code list, valid documents with their resolved form, and invalid documents (unknown keys, wrong types, out-of-range values, bad formats, a missing dependency) with the exact ordered issues each must produce. Every port must pass every vector. tests/unit/core/test_sdk_config.py checks the Python reference and the JSON Schema against them.
Adding a key or changing a default is a new version: it bumps x-sdk-config-version in the schema and ships a new sdk_config_v2.json vector file. The version 1 vectors are never edited.