fix(langchain): skip non-dict rows in format helpers - #2521
Conversation
Guard fetch-context, recall, and list-models formatters so a single non-dict payload element cannot AttributeError the tool envelope.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughFormatting helpers now defensively process malformed fetch-context, recall, and model-list inputs. Invalid entries are skipped, valid output is numbered correctly, values are normalized, fallback sentinels are preserved, and unit tests cover these cases. ChangesFormatting robustness
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
sdk/wren-langchain/src/wren_langchain/_format.py (2)
78-81: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsolidate validation checks.
For brevity and consistency with
format_list_models_content, consider consolidating the type and emptiness checks.♻️ Proposed refactor
- if not isinstance(items, list): - return "_No relevant context items found._" - if not items: - return "_No relevant context items found._" + if not isinstance(items, list) or not items: + return "_No relevant context items found._"🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@sdk/wren-langchain/src/wren_langchain/_format.py` around lines 78 - 81, Consolidate the separate type and emptiness checks in the relevant formatting function into one validation condition, matching the pattern used by format_list_models_content, while preserving the existing "_No relevant context items found._" return behavior for non-list and empty inputs.
119-122: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsolidate validation checks.
For brevity and consistency with
format_list_models_content, consider consolidating the type and emptiness checks. Checkingisinstancebefore emptiness is also slightly safer ifrowshappens to be an unexpected non-list object where boolean evaluation is unsupported (e.g., a numpy array or pandas DataFrame).♻️ Proposed refactor
- if not rows: - return "_No similar past queries found._" - if not isinstance(rows, list): - return "_No similar past queries found._" + if not isinstance(rows, list) or not rows: + return "_No similar past queries found._"🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@sdk/wren-langchain/src/wren_langchain/_format.py` around lines 119 - 122, Consolidate the validation in the relevant formatting function by checking that rows is a list and non-empty in a single condition, evaluating isinstance(rows, list) before its emptiness. Preserve the existing "_No similar past queries found._" fallback and match the validation style used by format_list_models_content.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@sdk/wren-langchain/src/wren_langchain/_format.py`:
- Around line 78-81: Consolidate the separate type and emptiness checks in the
relevant formatting function into one validation condition, matching the pattern
used by format_list_models_content, while preserving the existing "_No relevant
context items found._" return behavior for non-list and empty inputs.
- Around line 119-122: Consolidate the validation in the relevant formatting
function by checking that rows is a list and non-empty in a single condition,
evaluating isinstance(rows, list) before its emptiness. Preserve the existing
"_No similar past queries found._" fallback and match the validation style used
by format_list_models_content.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: c3d8dcd1-17d8-404c-bc58-b1e4e4dba5b4
📒 Files selected for processing (2)
sdk/wren-langchain/src/wren_langchain/_format.pysdk/wren-langchain/tests/unit/test_format_malformed.py
ReviewSmall, well-targeted defensive fix — scope is Correctness ✅The three formatters' Main note (non-blocking): new branches are under-testedThe 3 new tests only cover "a non-dict element interleaved with a valid dict is skipped." The edges this PR exists to handle aren't exercised:
A few cheap cases would close this (non-list container → fallback, all-invalid → fallback, and asserting Minor consistency (agreeing with CodeRabbit's nitpick)The three functions use three different validation shapes. if not isinstance(x, list) or not x:
return "<sentinel>"Minor style: test import mechanismThe new test loads from wren_langchain._format import (
format_fetch_context_content,
format_recall_content,
format_list_models_content,
)Non-blocking
|
…unify guards Address review polish on _format helpers: - Unify empty/non-list validation order in format_recall_content and format_fetch_context_content to match format_list_models_content (isinstance-then-empty). - Switch test import to direct 'from wren_langchain._format import ...' (matches repo convention; -e .[dev] provides deps). - Add coverage for non-list containers -> fallback, all-invalid -> fallback, field normalization (non-str summary/desc, non-list columns), and assert leading '1.' renumbering after skipped rows.
|
Thanks for the thorough review @goldmedal — really appreciate the care. Pushed 45bd89f addressing all the non-blocking notes:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
sdk/wren-langchain/tests/unit/test_format_malformed.py (1)
103-107: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winCover non-dict
propertiesnormalization.This test covers malformed
columns, but not the formatter’s defensive handling of non-dictproperties. Add a case such as"properties": "oops"and assert the model still renders with the expected description/fallback.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@sdk/wren-langchain/tests/unit/test_format_malformed.py` around lines 103 - 107, Extend test_format_list_models_normalizes_non_list_columns to include a model with non-dict properties such as "properties": "oops", then assert format_list_models_content renders that model with the expected description and fallback values without raising an error.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@sdk/wren-langchain/tests/unit/test_format_malformed.py`:
- Around line 103-107: Extend
test_format_list_models_normalizes_non_list_columns to include a model with
non-dict properties such as "properties": "oops", then assert
format_list_models_content renders that model with the expected description and
fallback values without raising an error.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 130fc6f3-29e3-423d-b54d-a21d9c4da3da
📒 Files selected for processing (2)
sdk/wren-langchain/src/wren_langchain/_format.pysdk/wren-langchain/tests/unit/test_format_malformed.py
🚧 Files skipped from review as they are similar to previous changes (1)
- sdk/wren-langchain/src/wren_langchain/_format.py
|
Pushed 5139f56 covering CodeRabbit's last nitpick — added |
Summary
Motivation
Partial or malicious memory/search payloads can inject non-dict rows. Removing those edges silently is more useful than crashing the LangChain tool content formatter mid-response.
License
Touches
sdk/**only (Apache-2.0).Verification
Test plan
Summary by CodeRabbit