Registry / http-networking / pygnmi

pygnmi

JSON →
library0.8.15pypypi✓ verified 87d ago

pygnmi (current version 0.8.15) is a pure Python gNMI client designed for managing network functions and collecting telemetry data. It provides an interface to interact with gNMI-enabled network devices, supporting operations like Get, Set, and Subscribe. The library is actively maintained with frequent minor releases, typically addressing bug fixes, performance improvements, and adding support for new gNMI features.

pip install pygnmi
INSTALL
IMPORT
SIG · PYGNMI
P
pygnmi
http-networkingpythonv0.8.15
Install
3.8s avg
Import
709ms
Disk
54MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.15 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.861s · 56.6MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 3.8s · import 0.556s · 54MB
54MB installed
● package 54MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gNMIclient
from pygnmi.client import gNMIclient

This quickstart demonstrates how to connect to a gNMI target, retrieve its capabilities, and perform a 'Get' operation for a specific path. It includes placeholders for host, port, and credentials, defaulting to common local development values or environment variables. An example for 'Subscribe' is provided but commented out.

import os from pygnmi.client import gNMIclient # Environment variables for connection details or default values GNMI_HOST = os.environ.get('GNMI_HOST', 'localhost') GNMI_PORT = int(os.environ.get('GNMI_PORT', '50051')) GNMI_USERNAME = os.environ.get('GNMI_USERNAME', 'admin') GNMI_PASSWORD = os.environ.get('GNMI_PASSWORD', 'admin') # Configure the gNMI target target = { 'addr': f'{GNMI_HOST}:{GNMI_PORT}', 'username': GNMI_USERNAME, 'password': GNMI_PASSWORD, 'insecure': True, # Set to False for secure TLS connections and provide certificates 'encoding': 'JSON_IETF', # Or None to auto-detect based on device capabilities } try: with gNMIclient(target=target) as c: # Get device capabilities capabilities = c.capabilities() print("\n--- Device Capabilities ---") print(capabilities) # Example: Get system hostname (path may vary by device/OS) # Using a common path, adjust as needed for your device hostname_path = ['/system/state/hostname'] get_response = c.get(path=hostname_path, encoding='JSON_IETF') print("\n--- Get Response (Hostname) ---") print(get_response) # Example: Subscribe to an operational state (uncomment to run) # subscription_paths = [ # ('/interfaces/interface[name=eth0]/state/oper-status', True) # ] # print("\n--- Subscribe (Streaming) ---") # for update in c.subscribe2(path_tuples=subscription_paths, subscribe_mode='STREAM', encoding='JSON_IETF'): # print(update) # # Add logic to break from loop after a few updates or a timeout except Exception as e: print(f"An error occurred: {e}") print("Please ensure the gNMI server is running and accessible with correct credentials.")
Debug
Known issues
deprecatedThe `subscribe()` method is being deprecated in favor of `subscribe2()`. It is strongly recommended to migrate telemetry collection to `subscribe2()` for future compatibility and improved functionality.
fix
Refactor telemetry code to use `pygnmi.client.gNMIclient.subscribe2()` for all new and existing telemetry collection.
affects: v0.8.6+
gotchaPrior to v0.8.9, `encoding` had a default value. From v0.8.9, `encoding` defaults to `None` and `capabilities()` is called on connect to auto-detect the supported encoding (with `json` and `json_ietf` preferred). Explicitly setting an encoding might be necessary if auto-detection doesn't pick the desired one or if upgrading from older versions.
fix
When creating `gNMIclient` or calling `get`/`subscribe2`, explicitly pass the `encoding` parameter (e.g., `encoding='JSON_IETF'`) if auto-detection is not sufficient or if migrating from pre-0.8.9 code.
affects: v0.8.9+
breakingVersion 0.8.11 fixed a telemetry break for Juniper devices caused by inconsistencies between `Capabilities()` communicated encodings and actual `Subscribe()` support. If experiencing issues with Juniper telemetry on versions prior to 0.8.11, upgrade immediately.
fix
Upgrade `pygnmi` to v0.8.11 or later to resolve Juniper telemetry issues and ensure correct encoding handling.
affects: pre-v0.8.11
gotchaBy default, `gNMIclient` uses `insecure=True`, which is suitable for development but not for production. For secure deployments, TLS must be configured.
fix
For production, set `insecure: False` in the `target` dictionary and provide `root_certificates`, `private_key`, and `certificate_chain` for proper TLS authentication.
affects: All versions
Errors
Common errors & fixes
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAUTHENTICATED details = "Authentication failed"
Incorrect username/password, or missing authentication details in the gNMI client configuration.
fix
Verify the `username` and `password` in the `target` dictionary. Ensure these credentials match what the gNMI server expects.
_InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLE details = "Connect Failed"
The gNMI server is unreachable, incorrect host/port in the target address, or a firewall is blocking the connection.
fix
Check the `addr` in the `target` dictionary for correct host and port. Ensure the gNMI server is running and network connectivity exists between the client and server.
TypeError: 'NoneType' object is not subscriptable
Attempting to access elements of a gNMI response that is `None` or empty, often due to an invalid gNMI path or the requested data not existing on the device.
fix
Verify the gNMI `path` is correct and exists on the target device. Add checks for `None` or empty responses (e.g., `if response and response.notification:`) before processing data.
TypeError: subscribe() got an unexpected keyword argument 'qos'
Using arguments (like `qos`) that are only supported by the `subscribe2()` method with the older `subscribe()` method, or attempting to use a feature not supported by the target device.
fix
Migrate from `subscribe()` to `subscribe2()` for all telemetry subscriptions, as `subscribe2()` is the recommended and actively developed method. Ensure all arguments are appropriate for `subscribe2()` and supported by your gNMI target.
Upgrade
Version history
0.8.15latest on PyPI · released Mar 10, 2025
Audit
Dependencies
grpciorequiredRequired for gRPC communication with gNMI targets.
protobufrequiredUnderlying data serialization for gRPC messages.
Agent activity
24 hits · last 30 days
node
22
OpenAI (training)
2
Resources
pygnmi — pip install pygnmi · libregistry