Skip to content

fix: wrong error message and swallowed JSONDecodeError in tool.py#35642

Open
jnMetaCode wants to merge 1 commit intolangchain-ai:masterfrom
jnMetaCode:fix/tool-message-bugs
Open

fix: wrong error message and swallowed JSONDecodeError in tool.py#35642
jnMetaCode wants to merge 1 commit intolangchain-ai:masterfrom
jnMetaCode:fix/tool-message-bugs

Conversation

@jnMetaCode
Copy link

Summary

  • Fix incorrect error message in ToolMessageChunk.__add__: The code checks tool_call_id but the error message said "different names" instead of "different tool_call_ids".
  • Capture JSONDecodeError in default_tool_parser: When JSON parsing fails, the exception was caught but error=None was passed to invalid_tool_call(), discarding the actual error information. Now passes str(e) so callers can see why parsing failed.

Files Changed

  • libs/core/langchain_core/messages/tool.py

Test plan

  • Verified error message now correctly references tool_call_ids
  • Verified JSONDecodeError message is preserved in invalid_tool_call.error

Two bug fixes:
1. ToolMessageChunk.__add__ checks tool_call_id but the error message
   incorrectly says "different names" instead of "different tool_call_ids".
2. default_tool_parser catches JSONDecodeError but passes error=None
   instead of the actual error message, silently swallowing the error.

Signed-off-by: JiangNan <1394485448@qq.com>
@github-actions github-actions bot added core `langchain-core` package issues & PRs external fix For PRs that implement a fix labels Mar 8, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 8, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 13 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing jnMetaCode:fix/tool-message-bugs (1d91c35) with master (29134dc)2

Open in CodSpeed

Footnotes

  1. 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.

  2. No successful run was found on master (532b014) during the generation of this report, so 29134dc was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@github-actions github-actions bot added the size: XS < 50 LOC label Mar 9, 2026
Copy link
Contributor

@gambletan Ethan T. (gambletan) left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small but meaningful fixes here:

  1. Error message fix in ToolMessageChunk.__add__ (line 186): Changing "different names" to "different tool_call_ids" is correct — the check is on self.tool_call_id != other.tool_call_id, so the error message should reference tool_call_id, not name. Good catch.

  2. Propagating JSONDecodeError into invalid_tool_calls (line 374-381): Previously error=None swallowed the decode error, making it impossible to debug why a tool call was marked invalid. Now error=str(e) preserves the actual parse error message. This is a clear improvement for debuggability.

    One thought: the str(e) representation of json.JSONDecodeError includes the message, document snippet, line number, and column — e.g. "Expecting property name enclosed in double quotes: line 1 column 2 (char 1)". This is informative but could be verbose if the malformed JSON string is large. Since invalid_tool_call stores this as a string field, it should be fine, but worth being aware of.

Both fixes are low-risk and clearly correct. This PR could benefit from a small unit test for the JSONDecodeError propagation — for instance, verifying that when function.arguments contains malformed JSON, the resulting invalid_tool_calls[0].error is a non-empty string. That said, the existing test infrastructure may already cover this indirectly.

Clean PR, LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants