Python package to interact with Microsoft Fabric items.
Project description
msfabric-devops
A Python package to interact with Microsoft Fabric objects, providing functionality to manage workspaces, semantic models, and items through the Fabric REST API.
Table of Contents
- Installation
- Requirements
- Quick Start
- FabricClient
- Semantic Models
- Error Handling
- Complete Example
- License
- Author
Installation
pip install msfabric-devops
Requirements
- Python >= 3.10
- Azure AD service principal or any credential supported by
DefaultAzureCredential(managed identity, Azure CLI, etc.)
Quick Start
from msfabric_devops import FabricClient
# Authenticate with a service principal
client = FabricClient(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-client-secret",
)
# List workspaces
for ws in client.get_workspaces():
print(ws["displayName"])
# Import a semantic model
result = client.import_item(
workspace_id="your-workspace-id",
path=r"C:\exports\MyModel.SemanticModel",
)
print(f"Imported: {result['displayName']} ({result['id']})")
FabricClient
FabricClient is the recommended way to interact with the Fabric REST API. It manages credentials and refreshes the bearer token automatically when it is about to expire, so you never need to handle tokens yourself.
Authentication
from msfabric_devops import FabricClient
# Option 1 — service principal (explicit credentials)
client = FabricClient(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-client-secret",
)
# Option 2 — DefaultAzureCredential (managed identity, Azure CLI, environment variables, …)
client = FabricClient()
Parameters
| Parameter | Type | Description |
|---|---|---|
tenant_id |
str, optional |
Azure AD Tenant ID |
client_id |
str, optional |
Azure AD Application (client) ID |
client_secret |
str, optional |
Azure AD Client Secret |
When all three are omitted, DefaultAzureCredential is used (suitable for managed identities, az login, and environment-variable-based auth).
Workspaces
get_workspaces
Returns all Fabric workspaces accessible to the authenticated principal.
workspaces = client.get_workspaces()
for ws in workspaces:
print(f"{ws['displayName']} ({ws['id']})")
Returns: list[dict] — each dict contains at minimum id and displayName.
get_workspace_by_id
Returns a single workspace by its GUID.
ws = client.get_workspace_by_id("workspace-id")
print(ws["displayName"])
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the target workspace |
Returns: dict — workspace object, or empty dict if not found.
get_workspaces_by_name
Returns all workspaces whose displayName matches the given name (exact, case-sensitive). Multiple results are possible because Fabric permits duplicate names.
matches = client.get_workspaces_by_name("My Workspace")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_name |
str |
Exact display name to search for |
Returns: list[dict]
create_workspace
Creates a new workspace. Returns None with a warning if a workspace with that name already exists.
ws = client.create_workspace("My New Workspace")
if ws:
print(f"Created: {ws['id']}")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_name |
str |
Display name for the new workspace |
Returns: dict on success, None if the name already exists.
delete_workspace
Deletes a workspace by its GUID.
client.delete_workspace("workspace-id")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace to delete |
Items
get_items
Returns all items in a workspace.
items = client.get_items("workspace-id")
for item in items:
print(f"{item['displayName']} [{item['type']}]")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace |
Returns: list[dict]
get_item_by_id
Returns a single item by its GUID.
item = client.get_item_by_id("workspace-id", "item-id")
print(item["displayName"])
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace |
item_id |
str |
The GUID of the item |
Returns: dict
get_items_by_name
Returns all items whose displayName matches the given name (exact, case-sensitive).
items = client.get_items_by_name("workspace-id", "My Model")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace |
item_name |
str |
Exact display name to search for |
Returns: list[dict]
get_item_definition_by_id
Exports an item definition from a workspace. Optionally writes the decoded files to a local directory.
# Get the raw definition dict
definition = client.get_item_definition_by_id("workspace-id", "item-id")
# Export files to disk
definition = client.get_item_definition_by_id(
workspace_id="workspace-id",
item_id="item-id",
output_dir=r"C:\exports\MyModel",
format="TMDL",
)
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace |
item_id |
str |
The GUID of the item |
output_dir |
str, optional |
Local directory where decoded files are written |
format |
str, optional |
Export format, e.g. "TMDL" or "PBIP" |
Returns: dict — raw API response containing the definition with a parts array. Each part includes path, payload (Base64-encoded), and payloadType.
import_item
Imports a .pbism (semantic model) or .pbir (report) from a local PBIP folder into a workspace. Creates a new item if none exists with the same name and type; otherwise updates the existing item.
# Import a semantic model
result = client.import_item(
workspace_id="workspace-id",
path=r"C:\exports\MyModel.SemanticModel",
item_properties={"displayName": "My Model"},
retain_roles=True,
retain_all_partitions=True,
)
# Import a semantic model and preserve partitions for specific tables only
result = client.import_item(
workspace_id="workspace-id",
path=r"C:\exports\MyModel.SemanticModel",
item_properties={"displayName": "My Model"},
retain_partitions_tables=["Sales", "Items"],
)
# Import a report linked to a semantic model
result = client.import_item(
workspace_id="workspace-id",
path=r"C:\exports\MyReport.Report",
item_properties={
"displayName": "My Report",
"semanticModelId": "existing-semantic-model-id",
},
)
print(f"{result['displayName']} ({result['id']})")
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
workspace_id |
str |
— | The GUID of the destination workspace |
path |
str |
— | Local folder containing the PBIP export (must contain a .pbism or .pbir file) |
item_properties |
dict, optional |
None |
Override displayName, type, or semanticModelId |
skip_if_exists |
bool |
False |
Return existing item immediately without updating its definition |
retain_roles |
bool |
False |
Preserve RLS roles from the published model |
retain_all_partitions |
bool |
False |
Preserve partition definitions for all tables |
retain_partitions_tables |
list[str], optional |
None |
Preserve partitions for listed table names only (ignored when retain_all_partitions=True) |
Note: When importing a report that uses a
byPathconnection to a semantic model,item_properties.semanticModelIdis required — the importer converts the connection tobyConnectionformat automatically.
Returns: dict with id, displayName, and type.
delete_item_by_id
Deletes an item by its GUID.
client.delete_item_by_id("workspace-id", "item-id")
Parameters
| Parameter | Type | Description |
|---|---|---|
workspace_id |
str |
The GUID of the workspace |
item_id |
str |
The GUID of the item to delete |
Semantic Models
Semantic model parameter updates operate on local files and do not require a FabricClient or any credentials.
set_semantic_model_parameters
Updates Power Query parameter values in a local semantic model definition. Supports both TMDL (definition/expressions.tmdl) and TMSL (model.bim) formats, detected automatically.
from msfabric_devops import set_semantic_model_parameters
set_semantic_model_parameters(
path=r"C:\exports\MyModel.SemanticModel",
parameters={
"Param_Server": "prod-server",
"Param_Database": "sales_db",
},
)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path |
str |
— | Root directory of the semantic model (parent of definition/ or folder containing model.bim) |
parameters |
dict[str, str] |
— | Mapping of parameter name → new value |
fail_if_not_found |
bool |
False |
Raise ValueError if a parameter name is not found; when False, logs a warning and continues |
Raises: FileNotFoundError if the model definition cannot be found at path.
Error Handling
All API errors raise typed exceptions that can be caught individually:
from msfabric_devops import FabricClient, FabricError, FabricThrottlingError
client = FabricClient(...)
try:
result = client.import_item(workspace_id="...", path="...")
except FabricThrottlingError:
print("API is throttled — retry later")
except FabricError as e:
print(f"API error: {e}")
| Exception | Raised when |
|---|---|
FabricError |
Base class for all API errors |
FabricAuthError |
Authentication or token acquisition fails |
FabricNotFoundError |
A requested resource does not exist |
FabricThrottlingError |
The API returns 429 after all automatic retries are exhausted |
Note: The library retries 429 responses automatically (up to 3 times, respecting the
Retry-Afterheader) before raisingFabricThrottlingError.
Complete Example
from msfabric_devops import FabricClient, set_semantic_model_parameters
# --- 1. Connect ---
client = FabricClient(
tenant_id="your-tenant-id",
client_id="your-client-id",
client_secret="your-client-secret",
)
# --- 2. Workspaces ---
workspaces = client.get_workspaces()
print(f"Found {len(workspaces)} workspace(s)")
ws = client.create_workspace("My DevOps Workspace")
ws_id = ws["id"]
# --- 3. Update local parameters before publishing ---
set_semantic_model_parameters(
path=r"C:\exports\MyModel.SemanticModel",
parameters={"Param_Environment": "prod"},
)
# --- 4. Import the semantic model ---
model = client.import_item(
workspace_id=ws_id,
path=r"C:\exports\MyModel.SemanticModel",
item_properties={"displayName": "My Model"},
retain_roles=True,
)
print(f"Model imported: {model['displayName']} ({model['id']})")
# --- 5. Import a report connected to the model ---
report = client.import_item(
workspace_id=ws_id,
path=r"C:\exports\MyReport.Report",
item_properties={
"displayName": "My Report",
"semanticModelId": model["id"],
},
)
print(f"Report imported: {report['displayName']} ({report['id']})")
# --- 6. Clean up ---
client.delete_workspace(ws_id)
print("Workspace deleted")
License
MIT
Author
Hugo Salaun (hcrsalaun@gmail.com)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file msfabric_devops-1.0.1.tar.gz.
File metadata
- Download URL: msfabric_devops-1.0.1.tar.gz
- Upload date:
- Size: 19.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03137492fe3189b83861131541a33eb51d8c56fe95f84a9cf880c8b944e59ae8
|
|
| MD5 |
5d03ee1b1da2a8ab7d4f84400a57f39c
|
|
| BLAKE2b-256 |
6b095fa47a94427d3a7cb84d7ad5272243727191efb986f969178da7ca690674
|
File details
Details for the file msfabric_devops-1.0.1-py3-none-any.whl.
File metadata
- Download URL: msfabric_devops-1.0.1-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b59f5eec4882d579b6a41ce5a5dc60fd8527dbf3a7c96221e7eeef8ab2b00cf3
|
|
| MD5 |
c48041ec3638d0c56c86ee297bb0d7dc
|
|
| BLAKE2b-256 |
18e04f861b66d870d7a520bc8c8ccf344b29ed2041f2d03f52f6e449fe33dab7
|