Repository navigation
fix(langchain): keep tool envelopes JSON-serializable for date and decimal rows - #2816
MohammadHijjawi97 wants to merge 1 commit into
Conversation
…cimal rows wren_query put `table.to_pylist()` straight into `data.rows`, so a DATE, TIMESTAMP or DECIMAL column left `datetime.date` / `Decimal` objects in the envelope. LangChain builds the ToolMessage content with `json.dumps` and falls back to `str()` when that raises, so the agent received a Python repr (single quotes, `datetime.date(...)`, `True`) instead of JSON. The same applied to wren_recall_queries, whose LanceDB rows carry `created_at`. make_success now runs `data` through the existing `json_safe` helper, as format_error already does for error metadata.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughThe success envelope now applies ChangesJSON-safe success envelopes
Priority: ⬇️ Low Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix Suggested reviewers: Merge Risk: ⚪ Minimal · up to
|
Summary
make_successnow passesdatathrough the existingjson_safehelper, sowren_query/wren_recall_queriesenvelopes stay JSON-serializable when rows contain DATE, TIMESTAMP or DECIMAL values. An agent now receives JSON tool output instead of a Python repr.What failure does this repair?
wren_querystorestable.to_pylist()indata.rows, which keepsdatetime.dateandDecimalobjects. LangChain builds theToolMessagecontent withjson.dumpsand falls back tostr()when that raises (langchain_core.tools.base._stringify), so the model gets a Python repr. Reproduction: DuckDB project withorders(id INTEGER, order_date DATE, amount DECIMAL(10,2)), tool invoked as an agent would ({"type": "tool_call", ...}):Before:
After:
wren_recall_queriesgoes through the same path; its LanceDB rows include acreated_atdatetime. The conversion matches whatcontentalready shows (default=str) and whatformat_erroralready does for metadata (ISO strings, Decimal as string). Calling the tool directly now also returns these values as strings indata;toolkit.query()is unchanged and still returns Arrow.How is it tested?
tests/unit/test_envelope.py::test_make_success_makes_query_rows_json_serializabletests/integration/test_runtime_tools.py::test_wren_query_tool_call_returns_json_for_date_and_decimal_rows: real DuckDB project and a real tool call; assertsjson.loads(message.content). Not markedslow, so it runs in the sdk-langchain CI job.Both fail without the change (
TypeError/JSONDecodeError) and pass with it. Fullpytestinsdk/wren-langchain: 124 passed, 2 deselected (slow).ruff check .andruff format --check .are clean.Duplicate check
Checked open and closed PRs and issues for wren-langchain /
json_safe/ envelope serialization. No open PR touchessdk/wren-langchain.Summary by CodeRabbit