Skip to content

AWSBedrockInstrumentation: patch clients initialized with boto3.Session #458

@codefromthecrypt

Description

@codefromthecrypt

Currently, AWSBedrockInstrumentation patches this properly

client = boto3.client("bedrock-runtime")

However, LangChain initializes it a little different, like this:

session = boto3.Session()
client = session.client("bedrock-runtime")

https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/llms/bedrock.py#L403-L426

I think this change will fix it in manual testing, but need second eyes in case I goofed

--- a/src/langtrace_python_sdk/instrumentation/aws_bedrock/instrumentation.py
+++ b/src/langtrace_python_sdk/instrumentation/aws_bedrock/instrumentation.py
@@ -42,6 +42,11 @@ class AWSBedrockInstrumentation(BaseInstrumentor):
             name="client",
             wrapper=patch_aws_bedrock(tracer, version),
         )
+        _W(
+            module="boto3.session",
+            name="Session.client",
+            wrapper=patch_aws_bedrock(tracer, version),
+        )
 
     def _uninstrument(self, **kwargs):
         pass

Once this works, we can resolve langtrace usage inside Elastic chatbot-rag-app here, by removing a workaround:

--- a/example-apps/chatbot-rag-app/api/llm_integrations.py
+++ b/example-apps/chatbot-rag-app/api/llm_integrations.py
@@ -1,6 +1,5 @@
 import os
 
-import boto3
 from langchain_aws import ChatBedrock
 from langchain_cohere import ChatCohere
 from langchain_google_vertexai import ChatVertexAI
@@ -42,7 +41,6 @@ def init_bedrock(temperature):
 
     AWSBedrockInstrumentation().instrument()
     return ChatBedrock(
-        client=boto3.client("bedrock-runtime"),
         model_id=os.getenv("CHAT_MODEL"),
         streaming=True,
         model_kwargs={"temperature": temperature},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions