Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Special holiday offer! You and a friend can attend FabCon with a BOGO code. Supplies are limited. Register now.

Reply
bruinsmm
Frequent Visitor

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"

1 ACCEPTED SOLUTION

Hi @Ugk161610 ,

 

When i press the Add connection button in the UDF manager, i can only select the following items:

bruinsmm_0-1762960016582.png

when i refresh the UDF page, now i see the item Variable Library.

Thanks!!

 

View solution in original post

3 REPLIES 3
tayloramy
Community Champion
Community Champion

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.

 

  1. Create the Variable Library item
    Workspace > New > Variable library > add variables (e.g., ENV, DEV_FILE_PATH, PROD_FILE_PATH).
    Docs: Variable Library overview
  2. 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
  3. 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.

Ugk161610
Continued Contributor
Continued Contributor

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:

  1. Go to your workspace and make sure your Variable Library is created and published.

  2. In your User Data Function, open the Connection Manager and click Add connection → choose Variable Library.

  3. Select your variable library and give it an alias name (for example, MyVarLibAlias).

  4. Use that same alias in your code:

    @udf.connection(argName="varLib", alias="MyVarLibAlias")
    1. 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

Hi @Ugk161610 ,

 

When i press the Add connection button in the UDF manager, i can only select the following items:

bruinsmm_0-1762960016582.png

when i refresh the UDF page, now i see the item Variable Library.

Thanks!!

 

Helpful resources

Announcements
December Fabric Update Carousel

Fabric Monthly Update - December 2025

Check out the December 2025 Fabric Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors