Forum Discussion
AI functions with PySpark error
- 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 Pandasimport synapse.ml.spark.aifunc as aifunc --> for PySparkNevertheless, thanks for the support here in this thread!v-karpurapud may I can take my own answer as solution?
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.
- Mauro898 months agoSuper 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-karpurapud8 months agoCommunity 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.
- Mauro898 months agoSuper User
Hi v-karpurapud,
Iam still not sure to which extension you relate (AI Spark Extensions being enabled on the specific capacity)?
Is it in the admin portal or where can I find it? As Iam Admin in the Fabric environment as well as capacity I assume I can enable it once found.And the workaround throughs the follwoing error:
No module named 'fabricext'Thanks and best regards!