fix(core): stringify empty list from tool return instead of passing as content blocks#35635
Conversation
Merging this PR will degrade performance by 11.58%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | WallTime | test_import_time[RunnableLambda] |
521.7 ms | 590 ms | -11.58% |
Comparing giulio-leone:fix/empty-list-tool-return-30578 (70a3fce) with master (527fc02)
Footnotes
-
23 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
2d68341 to
2b88c5a
Compare
…s content blocks _is_message_content_type() returns True for empty lists because all() on an empty iterable evaluates to True (vacuous truth). This causes [] returned by a tool to be passed directly as ToolMessage.content instead of being JSON-stringified to '[]', leading to inconsistent behavior where empty lists are treated as zero content blocks rather than empty list values. Add bool(obj) check to short-circuit on empty lists. Fixes langchain-ai#30578
2b88c5a to
70a3fce
Compare
|
Friendly ping — CI is green, tests pass, rebased on latest. Ready for review whenever convenient. Happy to address any feedback. 🙏 |
|
Friendly ping — rebased on latest and ready for review. Happy to address any feedback! |
Problem
BaseToolsubclasses that return an emptylistfrom_run/_arunget inconsistent behavior compared to non-empty lists:This causes downstream failures because
[]is interpreted as zero content blocks rather than an empty list result.Root Cause
_is_message_content_type()usesall()to check if every item in a list is a valid content block. For empty lists,all([])returnsTrue(vacuous truth), so the empty list passes the content-type check and is used directly instead of being stringified.Fix
Add
bool(obj)check to short-circuit on empty lists:Tests
test__is_message_content_typewith empty list casetest_tool_returning_empty_list_is_stringifiedthat verifies bothn=1(non-empty → JSON string) andn=0(empty →"[]") return consistent string contentFixes #30578