Skip to content

Commit 3345624

Browse files
Add UserAgent header to boto3 client (#558)
1 parent 82bc76e commit 3345624

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

libs/aws/langchain_aws/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ def setup_logging():
3535

3636
setup_logging()
3737

38+
39+
try:
40+
import boto3
41+
from botocore.config import Config
42+
43+
FRAMEWORK_UA = "x-client-framework:langchain-aws"
44+
ORIGINAL_BOTO3_CLIENT = boto3.client
45+
ORIGINAL_SESSION_CLIENT = boto3.session.Session.client
46+
47+
def _ensure_framework_ua(cfg: Config | None) -> Config:
48+
"""Return a Config guaranteed to contain our framework UA tag."""
49+
if cfg is None:
50+
return Config(user_agent_extra=FRAMEWORK_UA)
51+
existing = getattr(cfg, "user_agent_extra", "") or ""
52+
if FRAMEWORK_UA in existing:
53+
return cfg
54+
merged_extra = f"{existing} {FRAMEWORK_UA}".strip()
55+
return cfg.merge(Config(user_agent_extra=merged_extra))
56+
57+
def _patched_boto3_client(*args, **kwargs):
58+
kwargs["config"] = _ensure_framework_ua(kwargs.get("config"))
59+
return ORIGINAL_BOTO3_CLIENT(*args, **kwargs)
60+
61+
def _patched_session_client(self, *args, **kwargs):
62+
kwargs["config"] = _ensure_framework_ua(kwargs.get("config"))
63+
return ORIGINAL_SESSION_CLIENT(self, *args, **kwargs)
64+
65+
boto3.client = _patched_boto3_client
66+
boto3.session.Session.client = _patched_session_client
67+
except Exception:
68+
pass
69+
70+
3871
__all__ = [
3972
"BedrockEmbeddings",
4073
"BedrockLLM",

libs/aws/langchain_aws/agents/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ def get_boto_session(
6464
# Update user agent
6565
existing_user_agent = getattr(config, "user_agent_extra", "") or ""
6666
config.user_agent_extra = (
67-
f"{existing_user_agent} md/sdk_user_agent/{SDK_USER_AGENT}".strip()
67+
f"{existing_user_agent} x-client-framework:langchain-aws "
68+
f"md/sdk_user_agent/{SDK_USER_AGENT}".strip()
6869
)
6970
client_params = {"config": config}
7071

libs/langgraph-checkpoint-aws/langgraph_checkpoint_aws/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ def create_client_config(config: Optional[Config] = None) -> Config:
441441
"""
442442
config_kwargs = {}
443443
existing_user_agent = getattr(config, "user_agent_extra", "") if config else ""
444-
new_user_agent = f"{existing_user_agent} md/sdk_user_agent/{SDK_USER_AGENT}".strip()
444+
new_user_agent = (
445+
f"{existing_user_agent} x-client-framework:langgraph-checkpoint-aws "
446+
f"md/sdk_user_agent/{SDK_USER_AGENT}".strip()
447+
)
445448

446449
return Config(user_agent_extra=new_user_agent, **config_kwargs)

0 commit comments

Comments
 (0)