Skip to main content

yang2sdk

From YANG to a typed Python SDK — without hand-written wrappers.

Point yang2sdk at a network device, confirm the modules you care about, and get a Pydantic v2 client that knows the device's real data model: strict models, typed navigators, RPCs, and both RESTCONF and NETCONF transports.

ci-pr Python 3.12+ License: Apache-2.0 Transports: RESTCONF | NETCONF

Quick start · What you get · Authentication · Validation · Documentation · Contributing

Why yang2sdk

Network automation breaks when the SDK and the device disagree. yang2sdk removes the drift: the device supplies the YANG modules and feature set, the generator compiles the augment closure, and the emitted client is a self-contained package pinned to that device and OS version.

Device-derived Downloads the device's own YANG modules and capabilities, so the models describe what the box actually runs — not a generic vendor bundle.
Typed end to end Pydantic v2 models with enums, constraints, aliases, strict validation, and RFC 7951 64-bit handling. Bad values fail in the IDE, not on the device.
One navigator model RESTCONF and NETCONF expose the same intent — retrieve / update / replace / create / delete and RPC calls — with the protocol-appropriate verb set documented explicitly.
Safe by default Strict models, host-key/TLS verification, auto_commit=False, and a replace() guard that refuses to silently delete unset fields.
Verifiable sdk-verify walks the generated navigator tree and calls its own methods on a lab device, instead of trusting the wire shape by inspection.
Publishable One installable package per device and OS version, with its own pyproject.toml, models, navigators, and provenance manifest.

How it works

flowchart LR
    A[Device<br/>NETCONF get-schema] --> B[yang-downloader<br/>YANG + features]
    B --> C[yang2sdk<br/>confirm roots]
    C --> D[pyang AST → IR → Jinja]
    D --> E[versioned Python package]
    E --> F[RESTCONF / NETCONF client]
    E -. lab only .-> G[sdk-verify<br/>read · RPC · gated CRUD]

Quick start

Requires Python >= 3.12 and uv.

git clone https://github.com/ConsortiumGARR/yang2sdk.git
cd yang2sdk
uv sync --locked --extra lab
cp .env.example .env   # never commit .env

Set the target in .env: DEVICE_NAME, DEVICE_VERSION, DEVICE_IP, DEVICE_USER, and either DEVICE_PASS or, for NETCONF, DEVICE_SSH_KEY / DEVICE_SSH_KEY_PASSPHRASE.

uv run yang-downloader
uv run yang2sdk --protocol netconf   --device <device>
uv run yang2sdk --protocol restconf --device <device>

The compiler proposes root modules interactively; press Enter to accept them, or pass --roots <mod1>,<mod2> to make the choice explicit. temp/ is an untracked workspace created on demand — removing it only loses re-downloadable or recompilable output.

Optional: inspect the downloaded tree with pyang before compiling.

mkdir -p temp/yang_tree
uv run pyang -p temp/yang_modules/<device>/ -f tree \
  temp/yang_modules/<device>/*.yang > temp/yang_tree/<device>.txt

What you get

Each compile emits one self-contained, installable project per device and OS version, inside the --output-dir container:

temp/<protocol>_clients/<device>/          # container: holds one project
└── <device>_<os-version>/                 # the project: `uv add` this
    ├── pyproject.toml                     # name/version/deps (hatchling, src layout)
    ├── README.md                          # device, OS version, YANG revisions
    ├── MANIFEST.yang-revisions.json       # provenance: modules, features, deviations
    └── src/<device>_<os-version>/         # the importable package
        ├── __init__.py                    # RestconfClient / NetconfClient
        ├── session_manager.py             # transport and credentials
        ├── data_models/
        ├── data_navigators/               # client.data.<module>_<node>(...)
        └── py.typed

One copy of the code, on disk and in the sdist. Install it like any other dependency:

uv add path/to/temp/restconf_clients/<device>/<device>_<os-version>
from <device>_<os_version> import RestconfClient  # or NetconfClient

One package per device and OS version: different YANG revisions produce different models. See the generated-package section for the full layout and the HAL sketch for multi-vendor composition.

Example

from g30_1_4_0 import RestconfClient

# Credentials come from constructor args or the environment at runtime.
# Nothing is baked into generated code; verify=True is the default.
client = RestconfClient(management_ip="192.0.2.10")

# Top-level data is "<module>_<node>"; lists take key(s) in `key` order.
iface = client.data.ietf_interfaces_interface("ethernet-1/1")

port = iface.retrieve(content="config", depth=2)
port.description = "uplink-to-core"
iface.update(port)  # PATCH merge — the verb you normally want

Pydantic validates on assignment, so the IDE catches bad values before they reach the device:

port.admin_status = "dowm"
# ValidationError: Input should be 'up' or 'down'

replace() is PUT / nc:operation="replace" — the body is the whole resource (RFC 8040 §4.5, RFC 6241 §8.2.1). It refuses a depth-truncated model instead of silently deleting the rest; read deeper, use update(), or pass allow_partial=True when deletion is intentional.

Authentication

Credentials are resolved by source. An explicit password suppresses the environment key pair; an explicit key suppresses DEVICE_PASS and takes its passphrase only from the explicit passphrase argument. Generated clients and yang-downloader reject both explicit values as ambiguous; sdk-verify warns and uses the key for NETCONF, so --protocol both can pair a RESTCONF password with a NETCONF key. Otherwise generated clients read the environment; if both DEVICE_PASS and DEVICE_SSH_KEY are set, the key wins with a logged warning. A username is always required.

Transport Credentials
RESTCONF DEVICE_USER + DEVICE_PASS (TLS verification on by default)
NETCONF DEVICE_USER + DEVICE_PASS, or DEVICE_USER + DEVICE_SSH_KEY / DEVICE_SSH_KEY_PASSPHRASE

NETCONF also accepts an OpenSSH certificate by passing the conventional <private-key>-cert.pub path as key_filename; the adjacent private key must exist. An explicit key disables ncclient's SSH-agent and ~/.ssh fallbacks, so a typo or an unauthorized key fails instead of silently authenticating as another identity. The account password is never placed in ncclient's key-passphrase slot. For an encrypted key, ncclient puts the passphrase in its single password slot and will also try it as a password if public-key authentication fails; use an unencrypted key if that fallback is unacceptable.

If no usable credential is available, construction fails with ValueError before any transport work. DEVICE_USERNAME / DEVICE_PASSWORD are retired and ignored; do not reintroduce them as aliases.

Standards note. NETCONF public-key authentication uses the RFC 4252 §7 publickey method over the RFC 6242 SSH transport. The OpenSSH certificate encoding is a vendor extension documented by the IETF Internet-Draft draft-ietf-sshm-cert, not by an RFC; the authentication method remains RFC 4252 §7.

sdk-verify exposes the same credential contract for NETCONF as --ssh-key / --ssh-key-passphrase (or the environment).

Safety by default

  • Host-key / TLS verification is on (verify=True). Disabling it requires an explicit opt-out and logs a warning; it is for lab/self-signed gear only.
  • auto_commit=False on generated NETCONF clients. The safe sequence is edit(target="candidate") → validate(source="candidate") → commit(); auto_commit=True is opt-in and commits after every edit.
  • replace() refuses incomplete models. A replace body is the complete resource; allow_partial=True is the deliberate escape hatch.
  • Reads are depth-bounded by default. A whole-tree read is a lab operation, not a production convenience. The target node is level 1; a list and its entries share that level, so entry children are one level deeper. At the boundary, leaves and leaf-lists survive, keyed lists keep their keys (keys are addressability metadata), and containers/keyless lists are dropped. Legacy NETCONF (no NMDA) sends an RFC 6241 subtree filter that cannot select the whole subtree; if the filter would collapse to the target, the client does not send it, logs a warning, and returns a skeleton (empty container model, [] for a keyless list, path keys for a keyed list item). NMDA uses max-depth and RESTCONF uses ?depth=; both read normally. depth=1 is valid. depth="unbounded" is the only full-subtree read. A skeleton is the depth-bounded answer when the filter cannot be expressed; it is not proof the node exists, and it does not distinguish not-found from an empty node. A server that ignores the filter can still send extra bytes; the local pruner bounds the model but cannot undo the transfer. A non-boundary container whose only kept child sits on the boundary is omitted from the filter rather than sent empty (an empty element would select its whole subtree), so a full read pruned to the same depth can retain that container as an empty shell. A direct client.retrieve(depth=N) on legacy NETCONF without a subtree filter is refused, because it would be a whole-datastore read; use a navigator or depth="unbounded" explicitly.
  • sdk-verify and yang-downloader are lab-only. The write tiers modify the device; --write alone is not enough (see lab validation).

Validation

The generated client is exercised through its own public API on a lab device:

Tier What it does Writes?
read Recursively walks the navigator tree at bounded depth and validates every payload. No
rpc Builds and serialises every RPC Input; optionally dispatches an allow-listed RPC. No by default
crud retrieve → update → read back per container, then proves restoration via a whole-tree digest. Yes, gated
# Read every node and round-trip every RPC model. Nothing is modified.
uv run sdk-verify --device <device> --protocol both --tiers read,rpc

# RESTCONF CRUD requires --write plus --allow-restconf-writes.
uv run sdk-verify --device <device> --protocol restconf --tiers crud --write --allow-restconf-writes

NETCONF with :candidate needs only --write (edits are discarded on failure); without :candidate it also requires --allow-running-writes.

read,rpc is a blocking gate per simulator image in CI. CRUD is not run across the notconf simulator matrix; it runs only in the non-blocking SR Linux job, gated by --write plus --allow-running-writes. A generic synthesiser cannot satisfy must / when / leafref / mandatory constraints across a vendor tree, so that run is evidence rather than a merge gate. See docs/lab.md for the tier contract and the difference between a device rejection (a skip) and a client failure.

Documentation

Doc Audience Contents
docs/usage.md network engineer Compile flags, credentials, transport defaults, NETCONF write safety, RPCs/actions, packaging, HAL
docs/lab.md network engineer sdk-verify tiers and write gates, simulator matrix, SR Linux job
docs/dev.md contributor Gates, tool pinning and the bump procedure, tests, CI
AGENTS.md contributor Normative generator contract (IR ↔ templates, RFC compliance, safety)
CONTRIBUTING.md contributor Setup, the gate checklist, house rules
SECURITY.md security reporter Scope, private reporting, credential and transport-default guarantees
CHANGELOG.md everyone Added, changed, and fixed per release

Status

Public prototype. Both transports generate; the wire shapes are pinned offline in tests/test_matrix.py and exercised live against notconf simulators, SR Linux (full vendor tree, non-NMDA), and the Groove G30. sdk-verify read,rpc is blocking per simulator image; the SR Linux pytest job is non-blocking; live NMDA coverage remains a documented gap. The exact coverage matrix lives in docs/dev.md.

Compared with model-only tools such as pydantify and pyangbind, yang2sdk generates the models and the operation layer on top of pyang.

Contributing

uv sync --locked --group dev --extra lab
uv run ruff check . && uv run ruff format --check . \
  && uv run ty check && uv run pyrefly check \
  && uv run pytest tests/test_matrix.py   # offline gate: no docker, no device

ruff is the only linter/formatter. ty and pyrefly must both pass on src/ (temp/ output is excluded). All four gates are pinned exactly and run from the venv, never via uvx — see docs/dev.md for why, and for the bump procedure. Live-matrix tests need docker or lab gear — see docs/dev.md for the test/CI matrix and AGENTS.md for the normative generator contract.

License

Apache-2.0.

Metadata

Release files for yang2sdk 2026.11.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for yang2sdk 2026.11.2
File Size Uploaded Provenance Actions
yang2sdk-2026.11.2.tar.gz 472.4 kB With provenance Details for yang2sdk-2026.11.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for yang2sdk 2026.11.2
File Interpreter ABI Platform Actions
yang2sdk-2026.11.2-py3-none-any.whl Python 3 none any Details for yang2sdk-2026.11.2-py3-none-any.whl

Total release size: 649.9 kB

Release files / yang2sdk-2026.11.2.tar.gz

Download URL yang2sdk-2026.11.2.tar.gz
Size 472.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9ae0b169c309edc4394f22833af721ed5207ed039de222bec3d65e4321536628
BLAKE2b-256 checksum
How to use checksums
4a333914b83aa8f21c56f0e95dd27a93ac617f1d4a494b2c5669a4bf1a74959e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / yang2sdk-2026.11.2-py3-none-any.whl

Download URL yang2sdk-2026.11.2-py3-none-any.whl
Size 177.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73faabcb690eef2ae2cb0c08566fcf13cd8822645eec205aff42fad0b3a31d56
BLAKE2b-256 checksum
How to use checksums
47426bc9361c5069314a5fb2b200faa5620f2847a73dd3df5055eff3fb396736
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for yang2sdk 2026.11.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for yang2sdk-2026.11.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page