Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions sdk/wren-langchain/src/wren_langchain/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,27 @@ def format_fetch_context_content(result: dict[str, Any]) -> str:
return _cap_to_bytes(text, suffix="\n\n...[truncated]")

items = result.get("results", []) or []
if not isinstance(items, list):
return "_No relevant context items found._"
if not items:
return "_No relevant context items found._"

lines = []
for i, item in enumerate(items, start=1):
n = 0
for item in items:
if not isinstance(item, dict):
continue
n += 1
item_type = item.get("item_type", "item")
name = item.get("name", "")
summary = item.get("summary") or item.get("text") or ""
if not isinstance(summary, str):
summary = str(summary) if summary is not None else ""
if len(summary) > 120:
summary = summary[:117] + "..."
lines.append(f"{i}. [{item_type}] {name} — {summary}")
lines.append(f"{n}. [{item_type}] {name} — {summary}")
if not lines:
return "_No relevant context items found._"
return _cap_to_bytes("\n".join(lines), suffix="\n...[truncated]")


Expand All @@ -108,12 +118,20 @@ def format_recall_content(rows: list[dict[str, Any]]) -> str:
"""Render recalled NL→SQL pairs as a numbered list with code fences."""
if not rows:
return "_No similar past queries found._"
if not isinstance(rows, list):
return "_No similar past queries found._"

chunks = []
for i, row in enumerate(rows, start=1):
n = 0
for row in rows:
if not isinstance(row, dict):
continue
n += 1
nl = row.get("nl_query") or row.get("nl") or ""
sql = row.get("sql_query") or row.get("sql") or ""
chunks.append(f'{i}. "{nl}"\n ```sql\n {sql}\n ```')
chunks.append(f'{n}. "{nl}"\n ```sql\n {sql}\n ```')
if not chunks:
return "_No similar past queries found._"
return "\n".join(chunks)


Expand All @@ -132,18 +150,28 @@ def format_list_models_content(manifest: dict[str, Any]) -> str:
Columns: model | cols | description.
"""
models = manifest.get("models", []) or []
if not models:
if not isinstance(models, list) or not models:
return "_No models defined in this Wren project._"

lines = ["| model | cols | description |", "|---|---|---|"]
any_model = False
for m in models:
if not isinstance(m, dict):
continue
any_model = True
name = m.get("name", "")
col_count = len(m.get("columns", []) or [])
desc = (
(m.get("properties") or {}).get("description") or m.get("description") or ""
)
cols = m.get("columns", []) or []
col_count = len(cols) if isinstance(cols, list) else 0
props = m.get("properties") or {}
if not isinstance(props, dict):
props = {}
desc = props.get("description") or m.get("description") or ""
if not isinstance(desc, str):
desc = str(desc) if desc is not None else ""
# Trim long descriptions to keep table compact.
if len(desc) > 80:
desc = desc[:77] + "..."
lines.append(f"| {name} | {col_count} | {desc} |")
if not any_model:
return "_No models defined in this Wren project._"
return "\n".join(lines)
63 changes: 63 additions & 0 deletions sdk/wren-langchain/tests/unit/test_format_malformed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""format_* helpers must tolerate non-dict list rows from memory/search APIs."""

from __future__ import annotations

import importlib.util
import pathlib

_PATH = (
pathlib.Path(__file__).resolve().parents[2]
/ "src"
/ "wren_langchain"
/ "_format.py"
)
_spec = importlib.util.spec_from_file_location("wren_langchain_format_ut", _PATH)
_fmt = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_fmt)


def test_format_fetch_context_skips_non_dict_items() -> None:
out = _fmt.format_fetch_context_content(
{
"strategy": "search",
"results": [
"not-a-dict",
None,
{
"item_type": "model",
"name": "orders",
"summary": "Orders table",
},
],
}
)
assert "[model] orders" in out
assert "not-a-dict" not in out


def test_format_recall_skips_non_dict_rows() -> None:
out = _fmt.format_recall_content(
[
"x",
{"nl": "List orders", "sql": "SELECT 1"},
]
)
assert "List orders" in out
assert "SELECT 1" in out


def test_format_list_models_skips_non_dict_models() -> None:
out = _fmt.format_list_models_content(
{
"models": [
"bad",
None,
{
"name": "customers",
"columns": [{}, {}],
"description": "desc",
},
]
}
)
assert "| customers | 2 | desc |" in out
Loading