Skip to content

Commit 9086026

Browse files
marcusrehmMarcus Rehmgraphite-app[bot]mdrxy
authored
fixes ChatSnowflakeCortex parsing issues because of special characters in message_json (#337)
This PR fixes parsing issues of special characters in message_json when using ChatSnowflakeCortex. Fixes: langchain-ai/langchain#30115 --------- Co-authored-by: Marcus Rehm <[email protected]> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> Co-authored-by: Mason Daugherty <[email protected]>
1 parent 214d639 commit 9086026

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

libs/community/langchain_community/chat_models/snowflake.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ def _generate(
277277
sql_stmt = f"""
278278
select snowflake.cortex.{self.cortex_function}(
279279
'{self.model}',
280-
parse_json('{message_json}'),
281-
parse_json('{options_json}')
280+
parse_json($${message_json}$$),
281+
parse_json($${options_json}$$)
282282
) as llm_response;
283283
"""
284284

@@ -364,8 +364,8 @@ def _stream(
364364
sql_stmt = f"""
365365
select snowflake.cortex.{self.cortex_function}(
366366
'{self.model}',
367-
parse_json('{message_json}'),
368-
parse_json('{options_json}')
367+
parse_json($${message_json}$$),
368+
parse_json($${options_json}$$)
369369
) as llm_stream_response;
370370
"""
371371

libs/community/tests/integration_tests/chat_models/test_snowflake.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,18 @@ def test_chat_snowflake_cortex_generate(chat: ChatSnowflakeCortex) -> None:
5757
assert isinstance(generation, ChatGeneration)
5858
assert isinstance(generation.text, str)
5959
assert generation.text == generation.message.content
60+
61+
62+
def test_chat_snowflake_cortex_message_with_special_characters(
63+
chat: ChatSnowflakeCortex,
64+
) -> None:
65+
"""Test ChatSnowflakeCortex with messages containing special characters.
66+
67+
Args:
68+
chat: The ChatSnowflakeCortex instance to test with.
69+
"""
70+
system_message = SystemMessage(content="You are to chat with the user.")
71+
human_message = HumanMessage(content="Can you give me the weather in Tokyo?\n\n")
72+
response = chat([system_message, human_message])
73+
assert isinstance(response, BaseMessage)
74+
assert isinstance(response.content, str)

0 commit comments

Comments
 (0)