Skip to content

Commit c08e8a7

Browse files
Yun-Kimvamseeyarlavamsee_yarlagaddamabdinur
authored
fix(langchain): addresses compatibility with vision API [backport #8681 to 2.7] (#8709)
Backports #8681 to 2.7. Note that in 2.7 the test_langchain_community.py file is not yet present as there is no explicit support yet for langchain_community. The test added by #8681 is based in the now-not-present test file, so we've just deleted it for now but the fix still applies for regular langchain. ## 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) Co-authored-by: Vamsee Yarlagadda <[email protected]> Co-authored-by: vamsee_yarlagadda <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent 2984b59 commit c08e8a7

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

ddtrace/contrib/langchain/patch.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,16 @@ def traced_chat_model_generate(langchain, pin, func, instance, args, kwargs):
283283
for message_set_idx, message_set in enumerate(chat_messages):
284284
for message_idx, message in enumerate(message_set):
285285
if integration.is_pc_sampled_span(span):
286-
span.set_tag_str(
287-
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
288-
integration.trunc(message.content),
289-
)
286+
if isinstance(message, dict):
287+
span.set_tag_str(
288+
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
289+
integration.trunc(str(message.get("content", ""))),
290+
)
291+
else:
292+
span.set_tag_str(
293+
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
294+
integration.trunc(str(getattr(message, "content", ""))),
295+
)
290296
span.set_tag_str(
291297
"langchain.request.messages.%d.%d.message_type" % (message_set_idx, message_idx),
292298
message.__class__.__name__,
@@ -327,10 +333,7 @@ def traced_chat_model_generate(langchain, pin, func, instance, args, kwargs):
327333
else:
328334
log_chat_completions = [
329335
[
330-
{
331-
"content": message.text,
332-
"message_type": message.message.__class__.__name__,
333-
}
336+
{"content": message.text, "message_type": message.message.__class__.__name__}
334337
for message in messages
335338
]
336339
for messages in chat_completions.generations
@@ -343,7 +346,9 @@ def traced_chat_model_generate(langchain, pin, func, instance, args, kwargs):
343346
"messages": [
344347
[
345348
{
346-
"content": message.content,
349+
"content": message.get("content", "")
350+
if isinstance(message, dict)
351+
else str(getattr(message, "content", "")),
347352
"message_type": message.__class__.__name__,
348353
}
349354
for message in messages
@@ -374,10 +379,16 @@ async def traced_chat_model_agenerate(langchain, pin, func, instance, args, kwar
374379
for message_set_idx, message_set in enumerate(chat_messages):
375380
for message_idx, message in enumerate(message_set):
376381
if integration.is_pc_sampled_span(span):
377-
span.set_tag_str(
378-
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
379-
integration.trunc(message.content),
380-
)
382+
if isinstance(message, dict):
383+
span.set_tag_str(
384+
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
385+
integration.trunc(str(message.get("content", ""))),
386+
)
387+
else:
388+
span.set_tag_str(
389+
"langchain.request.messages.%d.%d.content" % (message_set_idx, message_idx),
390+
integration.trunc(str(getattr(message, "content", ""))),
391+
)
381392
span.set_tag_str(
382393
"langchain.request.messages.%d.%d.message_type" % (message_set_idx, message_idx),
383394
message.__class__.__name__,
@@ -418,10 +429,7 @@ async def traced_chat_model_agenerate(langchain, pin, func, instance, args, kwar
418429
else:
419430
log_chat_completions = [
420431
[
421-
{
422-
"content": message.text,
423-
"message_type": message.message.__class__.__name__,
424-
}
432+
{"content": message.text, "message_type": message.message.__class__.__name__}
425433
for message in messages
426434
]
427435
for messages in chat_completions.generations
@@ -434,7 +442,9 @@ async def traced_chat_model_agenerate(langchain, pin, func, instance, args, kwar
434442
"messages": [
435443
[
436444
{
437-
"content": message.content,
445+
"content": message.get("content", "")
446+
if isinstance(message, dict)
447+
else str(getattr(message, "content", "")),
438448
"message_type": message.__class__.__name__,
439449
}
440450
for message in messages
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
langchain: Ensures langchain vision APIs are correctly instrumented

0 commit comments

Comments
 (0)