Forum Discussion

Ira_27's avatar
Ira_27
Helper II
4 months ago
Solved

Using aifunc in notebook to extract csv data

Hi Microsoft Fabric Community,   I am testing the new AI Functions in Microsoft Fabric notebooks and using df.ai.extract() with aifunc.ExtractLabel() to extract values from a csv file that has comm...
  • arabalca's avatar
    4 months ago

    Hi Ira_27 ,

    The behavior you're describing is expected. By default, ai.extract returns a list for each label, even if only one value is found. That's why you see ["ABC123"] instead of ABC123 — it's not a bug, it's how it works internally.

     

    The fix is max_items=1 (https://learn.microsoft.com/en-us/fabric/data-science/ai-functions/pyspark/extract?tabs=labels#return) That parameter tells the function to return a scalar instead of a list. Give this a try this:

    labels=[
            aifunc.ExtractLabel(
                "Name",
                description="Return only the Name without brackets or quotes.",
                max_items=1,
                type="string"
            ),
            aifunc.ExtractLabel("Age", max_items=1, type="integer"),
            aifunc.ExtractLabel("Total %", max_items=1, type="number"),
        ]

     

    For other hand If your data starts at row 10, it's worth filtering those rows out before calling ai.extract — otherwise the model will try to extract values from lines that aren't actual data, which can produce empty or incorrect results for those rows.

     

    If my comment helped solve your question, it would be great if you could like the comment and mark it as the accepted solution. It helps others with the same issue and also motivates me to keep contributing.

     

    Thanks a lot, I really appreciate it.