From d89009cf268e12fea1f16cc7c384af6c17c7b3cc Mon Sep 17 00:00:00 2001 From: Praveen Mittal Date: Wed, 5 Aug 2026 21:34:42 +0200 Subject: [PATCH] Fix chat_with_SQL_3_ways.ipynb: SQLQuery breaks with OpenAIChatGenerator llm.replies from OpenAIChatGenerator is List[ChatMessage], but SQLQuery.run() declared queries: List[str] -- pipeline.connect("llm.replies", "sql_querier.queries") raised PipelineConnectError, so the notebook couldn't even build. SQLQuery now accepts List[Union[str, ChatMessage]] and extracts .text when given a ChatMessage, so it still works with the notebook's other two call sites that pass plain strings directly (the standalone .run() call, and the function-calling variant). Also fixed a second bug in the same notebook's conditional-routing section: the ConditionalRouter's 'no_answer' in/not in replies[0] check operated on a ChatMessage object directly, which raises TypeError (ChatMessage isn't iterable) -- needs .text. Its declared output_type: List[str] for the sql route was also inconsistent with the actual List[ChatMessage] value being routed. Fixes #212. --- notebooks/chat_with_SQL_3_ways.ipynb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/notebooks/chat_with_SQL_3_ways.ipynb b/notebooks/chat_with_SQL_3_ways.ipynb index 43e8948f..d8949cea 100644 --- a/notebooks/chat_with_SQL_3_ways.ipynb +++ b/notebooks/chat_with_SQL_3_ways.ipynb @@ -232,8 +232,9 @@ }, "outputs": [], "source": [ - "from typing import List\n", + "from typing import List, Union\n", "from haystack import component\n", + "from haystack.dataclasses import ChatMessage\n", "\n", "@component\n", "class SQLQuery:\n", @@ -242,12 +243,15 @@ " self.connection = sqlite3.connect(sql_database, check_same_thread=False)\n", "\n", " @component.output_types(results=List[str], queries=List[str])\n", - " def run(self, queries: List[str]):\n", + " def run(self, queries: List[Union[str, ChatMessage]]):\n", " results = []\n", + " processed_queries = []\n", " for query in queries:\n", - " result = pd.read_sql(query, self.connection)\n", + " sql_query = query.text if isinstance(query, ChatMessage) else query\n", + " result = pd.read_sql(sql_query, self.connection)\n", " results.append(f\"{result}\")\n", - " return {\"results\": results, \"queries\": queries}" + " processed_queries.append(sql_query)\n", + " return {\"results\": results, \"queries\": processed_queries}" ] }, { @@ -484,13 +488,13 @@ "\n", "routes = [\n", " {\n", - " \"condition\": \"{{'no_answer' not in replies[0]}}\",\n", + " \"condition\": \"{{'no_answer' not in replies[0].text}}\",\n", " \"output\": \"{{replies}}\",\n", " \"output_name\": \"sql\",\n", - " \"output_type\": List[str],\n", + " \"output_type\": List[ChatMessage],\n", " },\n", " {\n", - " \"condition\": \"{{'no_answer' in replies[0]}}\",\n", + " \"condition\": \"{{'no_answer' in replies[0].text}}\",\n", " \"output\": \"{{question}}\",\n", " \"output_name\": \"go_to_fallback\",\n", " \"output_type\": str,\n",