top of page

Keep Your Semantic Link Libraries Up to Date in Fabric Notebooks

  • Jan 28
  • 2 min read

Updated: 8 hours ago

It's one of those blog posts I write for my own reference, and I suspect many of you will find it useful too.

If you're working with Semantic Link and Semantic Link Labs in Microsoft Fabric, you've probably hit that moment where something doesn't work, and you realise your library version is out of date. Fabric pre-installs semantic-link, but the version bundled with your runtime isn't always the latest. And semantic-link-labs? That one you need to install yourself.

I got tired of manually checking PyPI, so I put together a quick snippet that does the heavy lifting for me.


What the code does


The script checks your currently installed version of semantic-link against the latest on PyPI and warns you if you're behind. It then installs or upgrades semantic-link-labs automatically.

A few things to note:

  • semantic-link is pre-installed in Fabric (Spark 3.4+), so we're not reinstalling it here. We're just checking whether you're on the latest version.

  • semantic-link-labs needs to be installed separately, and this script handles that for you.

  • The version check uses the PyPI JSON API, so it's lightweight and doesn't require any extra dependencies.


The code

import subprocess

import sys

import json

import urllib.request

from packaging.version import Version

from importlib.metadata import version as pkg_version, PackageNotFoundError



def _get_latest_pypi_version(package_name: str) -> str | None:

"""Fetch latest version from PyPI. Returns None on failure."""

try:

url = f"https://pypi.org/pypi/{package_name}/json"

with urllib.request.urlopen(url, timeout=5) as resp:

return json.loads(resp.read())["info"]["version"]

except Exception as e:

print(f" Could not fetch latest version for {package_name}: {e}")

return None



# ---- Check semantic-link-sempy (runtime-managed, don't upgrade) ----

try:

_sempy_installed = pkg_version("semantic-link-sempy")

_sempy_latest = _get_latest_pypi_version("semantic-link")

print(f"semantic-link-sempy installed: {_sempy_installed}")

if _sempy_latest:

print(f"semantic-link latest: {_sempy_latest}")

if Version(_sempy_installed) < Version(_sempy_latest):

print(f" Note: runtime version is behind PyPI. "

f"Update your Fabric environment if you need newer features.")

except PackageNotFoundError:

print("semantic-link-sempy not found (unexpected in Fabric runtime)")


# ---- Check and upgrade semantic-link-labs ----

try:

_labs_before = pkg_version("semantic-link-labs")

print(f"\nsemantic-link-labs installed: {_labs_before}")

except PackageNotFoundError:

_labs_before = None

print("\nsemantic-link-labs not found, will install")


_labs_latest = _get_latest_pypi_version("semantic-link-labs")

if _labs_latest:

print(f"semantic-link-labs latest: {_labs_latest}")


_needs_upgrade = (

_labs_before is None

or (_labs_latest and Version(_labs_before) < Version(_labs_latest))

)


if _needs_upgrade:

print("\nUpgrading semantic-link-labs...")

_result = subprocess.run(

[sys.executable, "-m", "pip", "install", "--upgrade", "--quiet", "semantic-link-labs"],

capture_output=True, text=True,

)

if _result.returncode == 0:

_labs_after = pkg_version("semantic-link-labs")

print(f" Upgraded: {_labs_before or 'n/a'} -> {_labs_after}")

else:

print(f" Install warning: {_result.stderr[:300]}")

else:

print("\nsemantic-link-labs is up to date.")


# ---- Compatibility smoke test ----

# This catches version mismatches where labs calls into sempy

# with parameters the older runtime version doesn't recognise

# (e.g. the FabricRestClient 'credential' kwarg issue)

try:

from sempy_labs.tom import connect_semantic_model as _test_import # noqa: F401

print("\nCompatibility check: passed")

except Exception as e:

print(f"\nCompatibility issue detected: {e}")

print("Consider upgrading semantic-link-sempy in your Fabric environment.")


Why bother?

Michael Kovalsky and the team are constantly shipping new features in semantic-link-labs. If you're not on the latest version, you might be missing out on new functions or bug fixes. This little snippet at the top of your notebook keeps you up to date.


I hope this saves someone a few minutes


Until next time, Prathy 🙂

Comments


bottom of page