Forum Discussion

Mauro89's avatar
Mauro89
Super User
9 months ago
Solved

AI functions with PySpark error

Hi there,

 

I have tried the AI functions capabilities in notebooks. The example with pandas dataframe worked for me. But the example as of the MS documentation with PySpark does not work.

Example from docs:

# This code uses AI. Always review output for mistakes.

df = spark.createDataFrame([
("Scarves",),
("Snow pants",),
("Ski goggles",)
], ["product"])

responses = df.ai.generate_response(prompt="Write a short, punchy email subject line for a winter sale.", output_col="response")
display(responses)

 

Error message is:

AttributeError: 'DataFrame' object has no attribute 'ai'

 

Anyone a idea about it?

 

Thanks!

 

PS: yes, openai lib is installed and admin setting is enabled, thats why it works with pandas df's 😉

  • Mauro89's avatar
    Mauro89
    8 months ago

    v-karpurapud sooooo. I turned out to be the issue was sitting infront of the screen not in the screen 😅

    I was only importing the libary for pandas and was not award that the pyspark library is a different one. So for all ready this post, the solution is to import the correct relevant library.

     

    import synapse.ml.aifunc as aifunc --> for Pandas
    import synapse.ml.spark.aifunc as aifunc --> for PySpark
     
    Nevertheless, thanks for the support here in this thread!
    v-karpurapud may I can take my own answer as solution?

     

12 Replies

  • Hi Mauro89 

     

    The error happens because your notebook isn’t running with the Spark AI features that Microsoft Fabric requires for the .ai accessor to work on PySpark DataFrames. Even though pandas AI functions run fine, the Spark version only works when your notebook uses Fabric Runtime 1.2 or later and when your workspace has AI in Notebooks and AI Spark Extensions enabled. If either of these is missing, Spark won’t load the AI extension, which is why you see the AttributeError. To fix it, switch your notebook to Fabric Runtime 1.2+, enable the AI features in the Admin Portal, and restart the kernel. After that, the PySpark example from the Fabric docs will work as expected. If Spark AI still isn’t available in your environment, you can temporarily call it using the AISpark helper class until the extension is enabled.

    • Mauro89's avatar
      Mauro89
      Super User

      Hi ABD128,

       

      runtime is 1.3.

      Do you mean this setting in the admin portal? If yes, it is enabled:

       

      Any other advise?

      • v-karpurapud's avatar
        v-karpurapud
        Community Support

        Hi Mauro89 
        Thank you for contacting the Microsoft Fabric community forum.
         

        The setting shown in your screenshot enables Copilot/OpenAI features for the whole tenant, but it doesn't ensure that the Spark AI extension is active in your notebook session. The .ai accessor comes from the Spark AI integration, which relies on the AI Spark Extensions being enabled on the specific capacity where your workspace is running.
         

        Even with Runtime 1.3, the extension might not load if the environment kernel starts without the AI Spark package or if the capacity-level AI features for “AI in notebooks / Spark AI Extensions” are not enabled.To check this, you can run the following in your notebook:

         

        hasattr(spark.createDataFrame([("x",)], ["col"]), "ai")

         

        If this returns False, the Spark AI extension did not load. If your capacity settings are correct but it still doesn’t load, it could be a deployment issue in that case, you should contact Fabric support, since .ai should be available on Runtime 1.3.

         

        As a workaround, you can use the helper class:

         

        from fabricext.ai import AISpark

         

        I hope this information is helpful. If you have any further questions, please let us know. we can assist you further.

         

        Regards,

        Microsoft Fabric Community Support Team.
         

  • v-karpurapud's avatar
    v-karpurapud
    Community Support

    Hi Mauro89

    Could you let us know if the issue was resolved after creating the support case?If you have any further questions, please let us know. we can assist you further.

     

    Regards,

    Microsoft Fabric Community Support Team