langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Exception thrown when calling the invoke() method on chat object of class ChatSnowflakeCortex

Open sfc-gh-clakkad opened this issue 1 year ago • 1 comments

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 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 for i, m in enumerate(messages): 622 try: 623 results.append( --> 624 self._generate_with_cache( 625 m, 626 stop=stop, 627 run_manager=run_managers[i] if run_managers else None, 628 **kwargs, 629 ) 630 ) 631 except BaseException as e: 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 else: 845 if inspect.signature(self._generate).parameters.get("run_manager"): --> 846 result = self._generate( 847 messages, stop=stop, run_manager=run_manager, **kwargs 848 ) 849 else: 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 l_rows = self._sp_session.sql(sql_stmt).collect() 219 except Exception as e: --> 220 raise ChatSnowflakeCortexError( 221 f"Error while making request to Snowflake Cortex via Snowpark: {e}" 222 ) 224 response = json.loads(l_rows[0]["LLM_RESPONSE"]) 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

sfc-gh-clakkad avatar Sep 09 '24 18:09 sfc-gh-clakkad

facing similar issue

KuroAshi75 avatar Sep 11 '24 13:09 KuroAshi75

facing similar issue..

NabilShabab avatar Sep 17 '24 18:09 NabilShabab

I'm not sure why yet, but making _sp_session a public field in the ChatSnowflakeCortex model makes this problem go away. Just replace _sp_session with sp_session everywhere in snowflake.py

markns avatar Oct 04 '24 06:10 markns

The public variable gets initialized on calling ChatSnowflakeCortex, but the internal variables initialization is not enforced (the one starting with _) , renaming _sp_session to sp_session enforces the initialization of the variable, i.e., snowflake session gets created in this case.

ritmar avatar Oct 10 '24 11:10 ritmar

image

Locate the source code of the ChatSnowflakeCortex class in the module (langchain_community.chat_models) in your Python virtual environment or path, then rename _sp_session to sp_session

seunjonathan avatar Dec 04 '24 00:12 seunjonathan

Hi, @sfc-gh-clakkad. I'm Dosu, and I'm helping the LangChain team manage their backlog. I'm marking this issue as stale.

Issue Summary

  • The issue involves an exception when using the invoke() method on a ChatSnowflakeCortex object.
  • Several users, including @KuroAshi75 and @NabilShabab, have reported similar issues.
  • A resolution involves changing _sp_session to sp_session in the ChatSnowflakeCortex class.
  • @seunjonathan provided guidance on implementing this fix.

Next Steps

  • Please confirm if this issue is still relevant to the latest version of the LangChain repository by commenting here.
  • If there is no further activity, this issue will be automatically closed in 7 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Mar 05 '25 16:03 dosubot[bot]