<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic No Spark Session Available Within Model Serving Environment in Machine Learning</title>
    <link>https://community.databricks.com/t5/machine-learning/no-spark-session-available-within-model-serving-environment/m-p/102511#M3860</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Is it possible to have a Spark session, that can be to used query the Unity Catalog etc, available within a Model Serving?&lt;/P&gt;&lt;P data-unlink="true"&gt;I have an MLFlow Pyfunc model that needs to get data from a Feature Table as part of its `.predict()` method. See my &lt;A href="https://community.databricks.com/t5/machine-learning/feature-lookup-help/m-p/102391" target="_self"&gt;earlier question&lt;/A&gt;&amp;nbsp;&amp;nbsp;for more context, and why I can't use the Feature Lookups via the Feature Engineering client.&lt;/P&gt;&lt;P&gt;My solution following that was to instead just query for the historical data I need within the `.predict()` method via `spark.read.table()`.&lt;/P&gt;&lt;P&gt;This works fine within a notebook environment, that already has a Spark session created (with access to the Unity Catalog).&lt;/P&gt;&lt;P&gt;However, when I deploy a model serving for the model, and try to use it for inference -- I ultimately get the following exception: `&lt;SPAN&gt;Exception: No SparkSession Available!`. Presumably because the serving's environment does &lt;EM&gt;not&amp;nbsp;&lt;/EM&gt;have any Spark session created.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(I get the same error if I try the MLFlow validation `&lt;/SPAN&gt;&lt;SPAN&gt;mlflow.models.&lt;/SPAN&gt;&lt;SPAN&gt;predict()` function).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I suppose one alternative solution could be to create a Feature Serving endpoint for the Unity Catalog table I need to query, then query that from within my model's `predict()` method? Is there a convenient way of handling this, or will I simply need to send a POST request to the appropriate URL?&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Dec 2024 15:50:25 GMT</pubDate>
    <dc:creator>mharrison</dc:creator>
    <dc:date>2024-12-18T15:50:25Z</dc:date>
    <item>
      <title>No Spark Session Available Within Model Serving Environment</title>
      <link>https://community.databricks.com/t5/machine-learning/no-spark-session-available-within-model-serving-environment/m-p/102511#M3860</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Is it possible to have a Spark session, that can be to used query the Unity Catalog etc, available within a Model Serving?&lt;/P&gt;&lt;P data-unlink="true"&gt;I have an MLFlow Pyfunc model that needs to get data from a Feature Table as part of its `.predict()` method. See my &lt;A href="https://community.databricks.com/t5/machine-learning/feature-lookup-help/m-p/102391" target="_self"&gt;earlier question&lt;/A&gt;&amp;nbsp;&amp;nbsp;for more context, and why I can't use the Feature Lookups via the Feature Engineering client.&lt;/P&gt;&lt;P&gt;My solution following that was to instead just query for the historical data I need within the `.predict()` method via `spark.read.table()`.&lt;/P&gt;&lt;P&gt;This works fine within a notebook environment, that already has a Spark session created (with access to the Unity Catalog).&lt;/P&gt;&lt;P&gt;However, when I deploy a model serving for the model, and try to use it for inference -- I ultimately get the following exception: `&lt;SPAN&gt;Exception: No SparkSession Available!`. Presumably because the serving's environment does &lt;EM&gt;not&amp;nbsp;&lt;/EM&gt;have any Spark session created.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;(I get the same error if I try the MLFlow validation `&lt;/SPAN&gt;&lt;SPAN&gt;mlflow.models.&lt;/SPAN&gt;&lt;SPAN&gt;predict()` function).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I suppose one alternative solution could be to create a Feature Serving endpoint for the Unity Catalog table I need to query, then query that from within my model's `predict()` method? Is there a convenient way of handling this, or will I simply need to send a POST request to the appropriate URL?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 15:50:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/no-spark-session-available-within-model-serving-environment/m-p/102511#M3860</guid>
      <dc:creator>mharrison</dc:creator>
      <dc:date>2024-12-18T15:50:25Z</dc:date>
    </item>
    <item>
      <title>Re: No Spark Session Available Within Model Serving Environment</title>
      <link>https://community.databricks.com/t5/machine-learning/no-spark-session-available-within-model-serving-environment/m-p/102515#M3861</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/134754"&gt;@mharrison&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;Creating a Spark session within a Model Serving environment is not directly supported, which is why you are encountering the Exception: No SparkSession Available! error. This limitation arises because the serving environment does not automatically create a Spark session.&lt;/P&gt;
&lt;P class="p1"&gt;Here are a few potential solutions to address this issue:&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Feature Serving Endpoint&lt;/STRONG&gt;: As you suggested, creating a Feature Serving endpoint for the Unity Catalog table you need to query is a viable solution. You can then query this endpoint from within your model's predict() method. This approach involves sending a POST request to the appropriate URL to retrieve the necessary data.&lt;/LI&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Alternative Data Retrieval Methods&lt;/STRONG&gt;: If creating a Feature Serving endpoint is not feasible, consider other methods to retrieve the data required for your model's predictions. This could involve pre-fetching the data and storing it in a format that your model can access without needing a Spark session.&lt;/LI&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Batch Inference&lt;/STRONG&gt;: If real-time inference is not a strict requirement, you might consider performing batch inference using a Spark cluster. This way, you can leverage the Spark session to read from the Unity Catalog and perform the necessary computations&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 18 Dec 2024 15:58:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/machine-learning/no-spark-session-available-within-model-serving-environment/m-p/102515#M3861</guid>
      <dc:creator>Alberto_Umana</dc:creator>
      <dc:date>2024-12-18T15:58:11Z</dc:date>
    </item>
  </channel>
</rss>

