Skip to content

fix(langchain): trace langchain tool definitions #2978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2025

Conversation

dinmukhamedm
Copy link
Collaborator

@dinmukhamedm dinmukhamedm commented Jun 5, 2025

Fixes #2975

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

Important

Adds tracing for Langchain tool definitions by setting span attributes for tool functions and includes tests to verify this behavior.

  • Behavior:
    • Adds tracing for Langchain tool definitions by setting span attributes for tool functions in _set_request_params() in callback_handler.py.
    • Handles tool function name, description, and parameters.
  • Tests:
    • Adds assertions in test_tool_calls.py to verify tool function attributes are correctly set in spans.
    • Tests include test_tool_calls, test_tool_calls_with_history, test_tool_calls_anthropic_text_block, and test_tool_calls_anthropic_text_block_and_history.

This description was created by Ellipsis for f8d873f. You can customize this summary. It will automatically update as commits are pushed.

Sorry, something went wrong.

Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to f8d873f in 1 minute and 32 seconds. Click for details.
  • Reviewed 156 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py:102
  • Draft comment:
    In the new loop that extracts tool definitions from invocation_params (lines 102-119), consider adding an inline comment explaining that these attributes are meant to trace tool definitions (name, description, parameters). Also, if neither 'parameters' nor 'input_schema' is provided, the code will end up setting a JSON string 'null' – if that isn’t desired, consider adding a check to avoid setting the attribute in such cases.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 10% vs. threshold = 50% The first suggestion about adding a comment is purely informative and doesn't require code changes. The second suggestion about null JSON values is speculative ("if that isn't desired") and doesn't demonstrate a clear problem. The code uses _set_span_attribute which already handles null values appropriately by not setting them. Looking at line 64, there's a check that only sets attributes if value is not None and not empty. The comment about null JSON could be valid if there's a requirement to never have 'null' in span attributes, but I don't have enough context about the requirements. The suggestion for documentation could help future maintainers. While documentation is nice, comments should be reserved for complex logic needing explanation. This code is fairly self-explanatory through good naming. The null handling concern is already addressed by the _set_span_attribute function. The comment should be deleted as it doesn't identify any clear issues requiring code changes. The suggestions are either purely informative or raise speculative concerns that are already handled by existing code.
2. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/callback_handler.py:130
  • Draft comment:
    The change in _set_chat_request (line 130) now passes the original kwargs instead of serialized["kwargs"]. This appears to fix tool definition tracing by accessing the full invocation_params. Ensure that there’s no unintended side-effect if both serialized and direct kwargs are present.
  • Reason this comment was not posted:
    Comment was not on a location in the diff, so it can't be submitted as a review comment.
3. packages/opentelemetry-instrumentation-langchain/tests/test_tool_calls.py:31
  • Draft comment:
    The tests now comprehensively verify that tool definitions (name and parameters) are correctly traced in the span attributes for both ChatOpenAI and ChatAnthropic scenarios. Great job! As a minor suggestion, also consider adding tests for edge cases when tool definitions might be missing to ensure robustness.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None

Workflow ID: wflow_eXwZspOTlOZADcip

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@nirga nirga merged commit bfa7b38 into traceloop:main Jun 9, 2025
10 of 16 checks passed
DrishyaDas pushed a commit to DrishyaDas/openllmetry that referenced this pull request Jun 16, 2025
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.

🐛 Bug Report: LangChain instrumentation does not pick up tool definitions
2 participants