Skip to content

fix(langchain): Fix missing langchain dependency for LangGraph tracing - #2988

Merged
nirga merged 4 commits into
traceloop:mainfrom
ronensc:fix-langchain-deps
Jun 9, 2025
Merged

nirga merged 4 commits into
traceloop:mainfrom
ronensc:fix-langchain-deps

Conversation

@ronensc

@ronensc ronensc commented Jun 9, 2025 •

Copy link
Copy Markdown
Collaborator

Fixes #2973

  • 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

Fixes missing LangGraph dependency by checking for langchain or langgraph and conditionally wrapping functions based on package availability.

  • Behavior:
    • Fixes missing dependency issue by checking for langchain or langgraph in init_langchain_instrumentor() in tracing.py.
    • Updates _wrap_openai_functions_for_tracing() in __init__.py to conditionally wrap functions based on langchain_community and langchain_openai package availability.
  • Utilities:
    • Adds is_package_available() function in utils.py to check for package presence.
  • Misc:
    • Removes langchain from _instruments in __init__.py.

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

@ellipsis-dev ellipsis-dev Bot left a comment

Copy link
Copy Markdown
Contributor

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 c30b2fd in 1 minute and 10 seconds. Click for details.
  • Reviewed 241 lines of code in 3 files
  • Skipped 0 files when reviewing.
  • Skipped posting 4 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/__init__.py:31
  • Draft comment:
    Changed dependency tuple now only requires 'langchain-core'. This is appropriate for LangGraph clients, but consider adding a comment or changelog note explaining why the 'langchain' dependency was dropped to avoid confusion.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 0% vs. threshold = 50% The comment is about a real change in the code - removing a dependency requirement. However, the comment is not suggesting a code change, but rather documentation. Per the rules, we should not make purely informative comments or ask for PR description updates. The code change itself appears intentional and well-structured with the new is_package_available checks. The dependency change could have significant impact on users, and documenting breaking changes is generally good practice. Maybe this deserves an exception to the documentation rule? No - the rules are clear that we should not make purely informative comments or request documentation updates. The code changes themselves properly handle the new optional dependency structure. Delete the comment since it only requests documentation and doesn't suggest any actual code changes.
2. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/__init__.py:82
  • Draft comment:
    Good use of conditional wrapping: checking with is_package_available for 'langchain_community' and 'langchain_openai' ensures that instrumentation is only applied when those packages are installed.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
3. packages/opentelemetry-instrumentation-langchain/opentelemetry/instrumentation/langchain/utils.py:72
  • Draft comment:
    The utility function 'is_package_available' is concise and effective. Consider adding a brief docstring to enhance maintainability.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 50% None
4. packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py:649
  • Draft comment:
    Updated init_langchain_instrumentor to check for 'langchain' OR 'langgraph' is a proper fix to address LangGraph-only projects. Ensure corresponding tests cover this new condition.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment suggests ensuring that tests cover a new condition, which violates the rule against asking the PR author to ensure their change is tested. It doesn't provide a specific suggestion or point out a specific issue with the code.

Workflow ID: wflow_p4acY8W1ZAyxuvd6

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

@nirga nirga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @ronensc! Do you happen to know what's the latency impact of find_spec?

@nirga
nirga merged commit d4cc327 into traceloop:main Jun 9, 2025
@ronensc
ronensc deleted the fix-langchain-deps branch June 10, 2025 07:36
@ronensc

ronensc commented Jun 10, 2025

Copy link
Copy Markdown
Collaborator Author

@nirga I ran a quick benchmark on importlib.util.find_spec (Python 3.10.12, 144 installed packages).
The average latency was ~0.07 ms per call, both for existing and non-existent modules.
So the impact is negligible in practice.

Full details in the collapsed section below

Latency measurement details
import importlib.util
import time
import statistics

def measure_find_spec_latency(module_name, runs=100):
    times = []
    found = 0

    for _ in range(runs):
        start = time.perf_counter()
        spec = importlib.util.find_spec(module_name)
        end = time.perf_counter()
        times.append((end - start) * 1000)  # milliseconds
        if spec is not None:
            found += 1

    print(f"find_spec('{module_name}') latency over {runs} runs:")
    print(f"  Avg:   {statistics.mean(times):.3f} ms")
    print(f"  Min:   {min(times):.3f} ms")
    print(f"  Max:   {max(times):.3f} ms")
    print(f"  Stdev: {statistics.stdev(times):.3f} ms")
    print(f"  Found: {found}/{runs} times ({(found/runs)*100:.1f}%)")

if __name__ == "__main__":
    # Try an available and a non-available module
    measure_find_spec_latency("langgraph")
    print()
    measure_find_spec_latency("non_existent_package_123")
$ python -V

Python 3.10.12
$ pip freeze | wc -l

144
$ python measure_find_spec.py


find_spec('langgraph') latency over 100 runs:
  Avg:   0.070 ms
  Min:   0.064 ms
  Max:   0.128 ms
  Stdev: 0.011 ms
  Found: 100/100 times (100.0%)

find_spec('non_existent_package_123') latency over 100 runs:
  Avg:   0.067 ms
  Min:   0.050 ms
  Max:   0.183 ms
  Stdev: 0.027 ms
  Found: 0/100 times (0.0%)

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: LangGraph traces not emitted unless langchain package is installed

2 participants