Skip to content

fix(langchain): handle ToolNode in create_agent_wrapper tool extraction#3973

Open
RRocaP wants to merge 1 commit intotraceloop:mainfrom
RRocaP:fix/langchain-toolnode-not-iterable
Open

fix(langchain): handle ToolNode in create_agent_wrapper tool extraction#3973
RRocaP wants to merge 1 commit intotraceloop:mainfrom
RRocaP:fix/langchain-toolnode-not-iterable

Conversation

@RRocaP
Copy link
Copy Markdown

@RRocaP RRocaP commented Apr 11, 2026

Summary

Fixes #3921

Problem

When langgraph-supervisor's create_supervisor() passes a ToolNode
object to create_react_agent, the instrumentation crashes:

File "patch.py", line 489, in wrapper
    for tool in tools:
TypeError: 'ToolNode' object is not iterable

ToolNode wraps tools but does not implement __iter__. It exposes
tools via tools_by_name (a dict[str, BaseTool]).

Fix

Before iterating, check if the tools object has a tools_by_name
attribute. If so, iterate over its .values(). Otherwise, iterate
normally (preserving existing behavior for lists/tuples).

Files changed

  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/patch.py

Test plan

  • create_supervisor() with ToolNode no longer crashes
  • Existing tests pass (list-based tools still work)

Summary by CodeRabbit

  • Bug Fixes
    • Improved tool extraction logic in agent instrumentation to properly handle different types of tool wrapper implementations, ensuring consistent behavior across various agent configurations.

When langgraph-supervisor passes a ToolNode object to
create_react_agent, the instrumentation crashes with:
  TypeError: 'ToolNode' object is not iterable

ToolNode wraps tools but does not implement __iter__. It exposes
individual tools via the tools_by_name attribute (dict of name->tool).

This fix checks for tools_by_name before iterating, falling back to
the original iterable behavior for lists, tuples, and other
iterables.

Fixes traceloop#3921
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b9c226e8-a32c-4aa0-b9b6-0bfd6a4f4f9a

📥 Commits

Reviewing files that changed from the base of the PR and between 786d49f and 2a4413f.

📒 Files selected for processing (1)
  • packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/patch.py

📝 Walkthrough

Walkthrough

The patch updates the agent wrapper's tool extraction logic to handle ToolNode objects, which lack direct iterability. The code now checks for a tools_by_name attribute and iterates over its values when present, otherwise falling back to the original iteration behavior.

Changes

Cohort / File(s) Summary
Tool Iteration Fix
packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/patch.py
Added conditional logic to detect and iterate over tools.tools_by_name.values() for ToolNode-like objects that expose tools via this attribute, preventing TypeError on non-iterable tool wrappers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 A ToolNode came hopping around,
Not iterable, causing a sound,
But a clever refactor set it all right,
Now tools_by_name shines so bright! ✨
thump-thump goes the rabbit's delight! 🐇

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: handling ToolNode in the tool extraction logic of create_agent_wrapper.
Linked Issues check ✅ Passed The code changes fully implement the fix requirements from issue #3921: checking for tools_by_name attribute and iterating over its values for ToolNode, while preserving existing behavior for other iterables.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the ToolNode handling issue described in issue #3921 with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: 'ToolNode' object is not iterable in patch.py when using langgraph-supervisor with create_react_agent

2 participants