Skip to content

fix: resolve TypeError when passing non-iterable ToolNode objects to langchain instrumentation tools extraction#4049

Open
manojmanivannan wants to merge 1 commit intotraceloop:mainfrom
manojmanivannan:fix-langgraph-supervisor-toolnode
Open

fix: resolve TypeError when passing non-iterable ToolNode objects to langchain instrumentation tools extraction#4049
manojmanivannan wants to merge 1 commit intotraceloop:mainfrom
manojmanivannan:fix-langgraph-supervisor-toolnode

Conversation

@manojmanivannan
Copy link
Copy Markdown

@manojmanivannan manojmanivannan commented Apr 22, 2026

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Enhanced tool definition extraction in LangChain instrumentation to support alternative tool configurations.
  • Tests

    • Added test coverage for ToolNode-based tool handling in agent creation.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 22, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 22, 2026

📝 Walkthrough

Walkthrough

The changes extend the agent wrapper's tool-definition extraction to conditionally iterate over a tools_by_name attribute when present, falling back to direct iteration otherwise, and introduce a test verifying this behavior with ToolNode objects passed to create_react_agent.

Changes

Cohort / File(s) Summary
Tool Definition Extraction Enhancement
packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/patch.py
Modified create_agent_wrapper to detect and iterate over tools_by_name.values() when available on the tools object, with fallback to direct tools iteration.
Agent Creation Test Addition
packages/opentelemetry-instrumentation-langchain/tests/test_langgraph.py
Added test_create_react_agent_with_toolnode to verify GenAI semantic convention attributes (operation name, agent name, tool definitions) when ToolNode is passed to create_react_agent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A tool by any name shall still be found,
Whether in a list or nested deep,
Our agent now checks all around,
Tool definitions we shall keep! 🔧

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 55.56% 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 accurately describes the main fix: handling non-iterable ToolNode objects in langchain tools extraction by detecting and iterating over the tools_by_name attribute.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/opentelemetry-instrumentation-langchain/tests/test_langgraph.py (1)

558-591: Test adequately covers the fix; consider strengthening the assertion.

The test validates that the create_agent span is produced with GEN_AI_TOOL_DEFINITIONS when a ToolNode is passed — which is exactly the regression case. To make the test guard against future regressions where tools_by_name iteration silently yields zero definitions, consider additionally asserting the tool name is present in the serialized definitions:

✅ Optional stronger assertion
     assert create_span.attributes[GenAIAttributes.GEN_AI_AGENT_NAME] == "ToolNodeAgent"
     assert GenAIAttributes.GEN_AI_TOOL_DEFINITIONS in create_span.attributes
+    import json as _json
+    tool_defs = _json.loads(create_span.attributes[GenAIAttributes.GEN_AI_TOOL_DEFINITIONS])
+    assert any(t.get("name") == "get_weather" for t in tool_defs)
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/opentelemetry-instrumentation-langchain/tests/test_langgraph.py`
around lines 558 - 591, The test should additionally assert that the serialized
tool definitions actually include the tool's name to prevent silent regressions;
in test_create_react_agent_with_toolnode after locating create_span (from
create_react_agent with ToolNode) deserialize or inspect
create_span.attributes[GenAIAttributes.GEN_AI_TOOL_DEFINITIONS] and assert it
contains "get_weather" (the tool defined via `@tool`) so the ToolNode ->
tools_by_name serialization is validated.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/opentelemetry-instrumentation-langchain/tests/test_langgraph.py`:
- Around line 558-591: The test should additionally assert that the serialized
tool definitions actually include the tool's name to prevent silent regressions;
in test_create_react_agent_with_toolnode after locating create_span (from
create_react_agent with ToolNode) deserialize or inspect
create_span.attributes[GenAIAttributes.GEN_AI_TOOL_DEFINITIONS] and assert it
contains "get_weather" (the tool defined via `@tool`) so the ToolNode ->
tools_by_name serialization is validated.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 43da2013-62c3-4925-9846-f1bb140d44fe

📥 Commits

Reviewing files that changed from the base of the PR and between 3efa0eb and 4f12775.

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

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.

2 participants