Forum Discussion
Access variable library in UDF
Microsoft has published the following code to get the values of the variable library, but in UDF in the connection manager i cann't find a variable library to select them. What's do i'm wrong?
@udf.connection(argName="varLib", alias="<My Variable Library Alias>")
@udf.function()
def get_storage_path(dataset: str, varLib: fn.FabricVariablesClient) -> str:
"""
Description: Determine storage path for a dataset based on environment configuration from Variable Library.
Args:
dataset_name (str): Name of the dataset to store.
varLib (fn.FabricVariablesClient): Fabric Variable Library connection.
Returns:
str: Full storage path for the dataset.
"""
# Retrieve variables from Variable Library
variables = varLib.getVariables()
# Get environment and base paths
env = variables.get("ENV")
dev_path = variables.get("DEV_FILE_PATH")
prod_path = variables.get("PROD_FILE_PATH")
# Apply environment-specific logic
if env.lower() == "dev":
return f"{dev_path}{dataset}/"
elif env.lower() == "prod":
return f"{prod_path}{dataset}/"
else:
return f"incorrect settings define for ENV variable"
Hi Ugk161610 ,
When i press the Add connection button in the UDF manager, i can only select the following items:
when i refresh the UDF page, now i see the item Variable Library.
Thanks!!
3 Replies
- Ugk161610Super User
Hi bruinsmm ,
I ran into the same issue before — the Variable Library wasn’t showing up in the UDF connection manager. It turned out that I hadn’t actually added the variable library as a connection inside the UDF.
Here’s what worked for me:
Go to your workspace and make sure your Variable Library is created and published.
In your User Data Function, open the Connection Manager and click Add connection → choose Variable Library.
Select your variable library and give it an alias name (for example, MyVarLibAlias).
Use that same alias in your code:
@udf.connection(argName="varLib", alias="MyVarLibAlias")The alias must match exactly what you gave in the connection manager.
If you still don’t see the variable library in the list, try refreshing the UDF page or check that you have permission to access it in the workspace.
Once the connection is set up, your code should work fine — you’ll be able to read the variables using:
variables = varLib.getVariables()
Hope that helps!
Gopi Krishna
- tayloramySuper User
Hi bruinsmm,
In Fabric, a UDF can read a Variable Library only after you both create a Manage connections entry that points at the Variable Library item and reference that connection’s alias in your @udf.connection(...) decorator. If you skip step 1, the Variable Library won’t appear in the UDF’s connection picker.
- Create the Variable Library item
Workspace > New > Variable library > add variables (e.g., ENV, DEV_FILE_PATH, PROD_FILE_PATH).
Docs: Variable Library overview - Create a workspace connection to that Variable Library
Workspace header > Manage connections > New connection > Fabric item > Variable library > pick your VL item > note the Alias
Docs: Connect to data sources for UDFs - Reference the alias in your UDF code and use the correct type
import fabric.functions as fn from fabric.functions import udf @udf.connection(argName="varLib", alias="MyVarLib") @udf.function() def get_storage_path(dataset: str, varLib: fn.FabricVariablesClient) -> str: variables = varLib.getVariables() env = (variables.get("ENV") or "").lower() dev_path = variables.get("DEV_FILE_PATH") or "" prod_path = variables.get("PROD_FILE_PATH") or "" if env == "dev": return f"{dev_path}{dataset}/" elif env == "prod": return f"{prod_path}{dataset}/" else: return "incorrect settings defined for ENV variable"4. Publish the UDF, then open the UDF item and check the Connections pane. You should now see the Variable Library bound via the alias.
If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution.
- Create the Variable Library item