Skip to content
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

fix(langchain): type check chain.invoke() input argument name #8835

Merged
merged 1 commit into from
Apr 2, 2024

Conversation

Yun-Kim
Copy link
Contributor

@Yun-Kim Yun-Kim commented Apr 2, 2024

This PR fixes the langchain integration's patched chain method to check for the correct input argument name.

In LangChain<0.1, we patch langchain.Chain.__call__(), which uses inputs: Union[Dict[str, Any], str] as the argument to the chain invocation. However in LangChain>=0.1, we patch langchain.Chain.invoke(), which uses input: Dict[str, Any] as the argument to the chain invocation. We use the same traced function to patch both methods, but this subtle change broke our argument parsing, which expected the name inputs instead of input.

Checklist

  • Change(s) are motivated and described in the PR description
  • Testing strategy is described if automated tests are not included in the PR
  • Risks are described (performance impact, potential for breakage, maintainability)
  • Change is maintainable (easy to change, telemetry, documentation)
  • Library release note guidelines are followed or label changelog/no-changelog is set
  • Documentation is included (in-code, generated user docs, public corp docs)
  • Backport labels are set (if applicable)
  • If this PR changes the public interface, I've notified @DataDog/apm-tees.
  • If change touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.

Reviewer Checklist

  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Description motivates each change
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Change is maintainable (easy to change, telemetry, documentation)
  • Release note makes sense to a user of the library
  • Author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

@Yun-Kim Yun-Kim added the changelog/no-changelog A changelog entry is not required for this PR. label Apr 2, 2024
@Yun-Kim Yun-Kim requested review from a team as code owners April 2, 2024 20:09
@Yun-Kim Yun-Kim force-pushed the yunkim/fix-langchain-chain-inputs branch 2 times, most recently from f9b9d6a to 6f8d9c7 Compare April 2, 2024 20:19
@Yun-Kim Yun-Kim removed the changelog/no-changelog A changelog entry is not required for this PR. label Apr 2, 2024
@datadog-dd-trace-py-rkomorn
Copy link

datadog-dd-trace-py-rkomorn bot commented Apr 2, 2024

Datadog Report

Branch report: yunkim/fix-langchain-chain-inputs
Commit report: 37b90ae
Test service: dd-trace-py

✅ 0 Failed, 272 Passed, 133 Skipped, 0s Wall Time

@Yun-Kim Yun-Kim requested a review from erikayasuda April 2, 2024 20:34
Copy link
Contributor

@sabrenner sabrenner left a comment

Choose a reason for hiding this comment

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

lgtm, one nonblocking question

ddtrace/contrib/langchain/patch.py Outdated Show resolved Hide resolved
@Yun-Kim Yun-Kim force-pushed the yunkim/fix-langchain-chain-inputs branch from 6f8d9c7 to 37b90ae Compare April 2, 2024 21:09
@pr-commenter
Copy link

pr-commenter bot commented Apr 2, 2024

Benchmarks

Benchmark execution time: 2024-04-02 22:13:13

Comparing candidate commit 37b90ae in PR branch yunkim/fix-langchain-chain-inputs with baseline commit 1c1f54a in branch main.

Found 7 performance improvements and 4 performance regressions! Performance is the same for 190 metrics, 9 unstable metrics.

scenario:flasksimple-appsec-telemetry

  • 🟩 execution_time [-256.159µs; -210.144µs] or [-3.933%; -3.226%]

scenario:flasksimple-tracer

  • 🟥 execution_time [+202.964µs; +252.187µs] or [+3.228%; +4.010%]

scenario:httppropagationextract-datadog_tracecontext_tracestate_not_propagated_on_trace_id_no_match

  • 🟩 max_rss_usage [-693.461KB; -469.393KB] or [-3.177%; -2.150%]

scenario:httppropagationextract-invalid_span_id_header

  • 🟩 max_rss_usage [-767.353KB; -618.323KB] or [-3.509%; -2.827%]

scenario:httppropagationextract-medium_valid_headers_all

  • 🟩 max_rss_usage [-719.715KB; -501.303KB] or [-3.293%; -2.293%]

scenario:httppropagationextract-valid_headers_all

  • 🟥 max_rss_usage [+727.535KB; +808.055KB] or [+3.442%; +3.823%]

scenario:httppropagationextract-wsgi_invalid_trace_id_header

  • 🟥 max_rss_usage [+664.764KB; +746.718KB] or [+3.144%; +3.531%]

scenario:sethttpmeta-all-disabled

  • 🟩 max_rss_usage [-733.373KB; -475.357KB] or [-3.314%; -2.148%]

scenario:sethttpmeta-obfuscation-send-querystring-disabled

  • 🟥 max_rss_usage [+480.507KB; +756.895KB] or [+2.212%; +3.484%]

scenario:sethttpmeta-useragentvariant_exists_2

  • 🟩 max_rss_usage [-827.870KB; -597.538KB] or [-3.733%; -2.695%]

scenario:sethttpmeta-useragentvariant_exists_3

  • 🟩 max_rss_usage [-712.610KB; -451.474KB] or [-3.224%; -2.042%]

@Yun-Kim Yun-Kim merged commit d16df44 into main Apr 2, 2024
81 of 87 checks passed
@Yun-Kim Yun-Kim deleted the yunkim/fix-langchain-chain-inputs branch April 2, 2024 22:53
Yun-Kim added a commit that referenced this pull request Apr 15, 2024
This PR fixes the langchain integration's patched chain method to check
for the correct input argument name.

In `LangChain<0.1`, we patch `langchain.Chain.__call__()`, which uses
`inputs: Union[Dict[str, Any], str]` as the argument to the chain
invocation. However in `LangChain>=0.1`, we patch
`langchain.Chain.invoke()`, which uses `input: Dict[str, Any]` as the
argument to the chain invocation. We use the same traced function to
patch both methods, but this subtle change broke our argument parsing,
which expected the name `inputs` instead of `input`.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.
- [x] If change touches code that signs or publishes builds or packages,
or handles credentials of any kind, I've requested a review from
`@DataDog/security-design-and-guidance`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [X] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
Yun-Kim added a commit that referenced this pull request Apr 16, 2024
… 2.8] (#8987)

Backport #8835 to 2.8.

This PR fixes the langchain integration's patched chain method to check
for the correct input argument name.

In `LangChain<0.1`, we patch `langchain.Chain.__call__()`, which uses
`inputs: Union[Dict[str, Any], str]` as the argument to the chain
invocation. However in `LangChain>=0.1`, we patch
`langchain.Chain.invoke()`, which uses `input: Dict[str, Any]` as the
argument to the chain invocation. We use the same traced function to
patch both methods, but this subtle change broke our argument parsing,
which expected the name `inputs` instead of `input`.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.
- [x] If change touches code that signs or publishes builds or packages,
or handles credentials of any kind, I've requested a review from
`@DataDog/security-design-and-guidance`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [X] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance

policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
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.

3 participants