LIANJie-Jason Claude Opus 4.6 commited on
Commit
3a0fe98
·
1 Parent(s): 2317f6c

fix: harden SQL ingest connection handling and brace-escape all prompt parameters

Browse files

- Extract _ingest_tables() helper in sql_ingest.py to ensure SQLite
connection is always closed via try/finally
- Apply _escape_braces() to bot_name and domain in build_prompt() and
build_query_understanding_prompt() to prevent KeyError on user-supplied
text containing curly braces

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (2) hide show
  1. src/prompts.py +3 -3
  2. src/sql_ingest.py +19 -5
src/prompts.py CHANGED
@@ -245,7 +245,7 @@ def build_query_understanding_prompt(
245
  kb_overview_block = ""
246
 
247
  return QUERY_UNDERSTANDING_PROMPT_TEMPLATE.format(
248
- domain=domain,
249
  history=_escape_braces(history_str),
250
  query=_escape_braces(query),
251
  sql_routing_block=sql_routing_block,
@@ -284,8 +284,8 @@ def build_prompt(
284
  overview_section = ""
285
 
286
  return SYSTEM_PROMPT_TEMPLATE.format(
287
- bot_name=bot_name,
288
- domain=domain,
289
  context=_escape_braces(context),
290
  kb_overview_section=overview_section,
291
  )
 
245
  kb_overview_block = ""
246
 
247
  return QUERY_UNDERSTANDING_PROMPT_TEMPLATE.format(
248
+ domain=_escape_braces(domain),
249
  history=_escape_braces(history_str),
250
  query=_escape_braces(query),
251
  sql_routing_block=sql_routing_block,
 
284
  overview_section = ""
285
 
286
  return SYSTEM_PROMPT_TEMPLATE.format(
287
+ bot_name=_escape_braces(bot_name),
288
+ domain=_escape_braces(domain),
289
  context=_escape_braces(context),
290
  kb_overview_section=overview_section,
291
  )
src/sql_ingest.py CHANGED
@@ -199,6 +199,25 @@ def ingest_to_sql(files: list[tuple], documents_dir: str, cfg: dict) -> dict:
199
  conn = sqlite3.connect(db_path)
200
  schema_registry = {}
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  for file_path, dataset_name in tabular_files:
203
  ext = file_path.suffix.lower()
204
  rel_path = file_path.relative_to(documents_dir)
@@ -270,9 +289,4 @@ def ingest_to_sql(files: list[tuple], documents_dir: str, cfg: dict) -> dict:
270
  }
271
  print(f" SQL: {table_name} ({row_count} rows, {len(headers)} columns)")
272
 
273
- conn.close()
274
-
275
- with open(schema_path, "w", encoding="utf-8") as f:
276
- json.dump(schema_registry, f, indent=2, default=str)
277
-
278
  return schema_registry
 
199
  conn = sqlite3.connect(db_path)
200
  schema_registry = {}
201
 
202
+ try:
203
+ schema_registry = _ingest_tables(conn, tabular_files, documents_dir)
204
+ finally:
205
+ conn.close()
206
+
207
+ with open(schema_path, "w", encoding="utf-8") as f:
208
+ json.dump(schema_registry, f, indent=2, default=str)
209
+
210
+ return schema_registry
211
+
212
+
213
+ def _ingest_tables(
214
+ conn: sqlite3.Connection,
215
+ tabular_files: list[tuple],
216
+ documents_dir: str,
217
+ ) -> dict:
218
+ """Load tabular files into SQLite tables. Returns the schema registry."""
219
+ schema_registry = {}
220
+
221
  for file_path, dataset_name in tabular_files:
222
  ext = file_path.suffix.lower()
223
  rel_path = file_path.relative_to(documents_dir)
 
289
  }
290
  print(f" SQL: {table_name} ({row_count} rows, {len(headers)} columns)")
291
 
 
 
 
 
 
292
  return schema_registry