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

Get Fabric certified for FREE! Don't miss your chance! Learn more

Reply
bruinsmm
New Contributor III

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
bruinsmm
New Contributor III

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
Ugk161610
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

bruinsmm
New Contributor III

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!!

 

tayloramy
Contributor III

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.





If you found this helpful, consider giving some Kudos.
If I answered your question or solved your problem, mark this post as the solution!

Proud to be a Super User!





Helpful resources

Announcements
Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Free Fabric Certifications

Free Fabric Certifications

Get Fabric certified for free! Don't miss your chance.

January Fabric Update Carousel

Fabric Monthly Update - January 2026

Check out the January 2026 Fabric update to learn about new features.

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.

Users online (12,785)