Repository: langchain
Issue: Exception thrown when calling the invoke() method on chat object of class ChatSnowflakeCortex
Labels: 🤖:bug,investigate

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangChain rather than my code.
- [X] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

### Example Code

Issue reproduced using the Snowflake Cortex integration tutorial on the official documentation here : https://python.langchain.com/v0.2/docs/integrations/chat/snowflake/

during generation at the line `chat.invoke(messages)`

This is from the [langchain-community](https://pypi.org/project/langchain-community/) package and not the core langchain. 

### Error Message and Stack Trace (if applicable)

`
...
File ~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:624, in BaseChatModel.generate(self, messages, stop, callbacks, tags, metadata, run_name, run_id, **kwargs)
    [621](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:621) for i, m in enumerate(messages):
    [622](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:622)     try:
    [623](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:623)         results.append(
--> [624](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:624)             self._generate_with_cache(
    [625](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:625)                 m,
    [626](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:626)                 stop=stop,
    [627](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:627)                 run_manager=run_managers[i] if run_managers else None,
    [628](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:628)                 **kwargs,
    [629](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:629)             )
    [630](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:630)         )
    [631](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:631)     except BaseException as e:
    [632](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:632)         if run_managers:

File ~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:846, in BaseChatModel._generate_with_cache(self, messages, stop, run_manager, **kwargs)
    [844](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:844) else:
    [845](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:845)     if inspect.signature(self._generate).parameters.get("run_manager"):
--> [846](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:846)         result = self._generate(
    [847](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:847)             messages, stop=stop, run_manager=run_manager, **kwargs
    [848](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:848)         )
    [849](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:849)     else:
    [850](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_core/language_models/chat_models.py:850)         result = self._generate(messages, stop=stop, **kwargs)

File ~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:220, in ChatSnowflakeCortex._generate(self, messages, stop, run_manager, **kwargs)
    [218](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:218)     l_rows = self._sp_session.sql(sql_stmt).collect()
    [219](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:219) except Exception as e:
--> [220](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:220)     raise ChatSnowflakeCortexError(
    [221](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:221)         f"Error while making request to Snowflake Cortex via Snowpark: {e}"
    [222](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:222)     )
    [224](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:224) response = json.loads(l_rows[0]["LLM_RESPONSE"])
    [225](https://file+.vscode-resource.vscode-cdn.net/Users/clakkad/Documents/Work/demos/Cortex/asking-questions-pdfs-snowflake-cortex/~/opt/anaconda3/envs/cortex_base/lib/python3.10/site-packages/langchain_community/chat_models/snowflake.py:225) ai_message_content = response["choices"][0]["messages"]

**ChatSnowflakeCortexError: Error while making request to Snowflake Cortex via Snowpark: 'NoneType' object has no attribute 'sql'`**



### Description

* Ideally the invoke() method should be able to return a completion as returned by Snowflake Cortex Complete function.
* The issue seems to be that "_sp_session" being used at invocation isnt set to the actual value grabbed during validation. Hence the sql() method supported on Snowpark session throws the above None error as by default _sp_session is set to None. 


### System Info

langchain==0.2.16
langchain-community==0.2.16
langchain-core==0.2.38
langchain-text-splitters==0.2.4

platform: mac
python==3.10