The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
feat(anthropic): add `user_profile_id` convenience attribute (#39148)
ccurmea1a1ad3bb3eb
diff --git a/libs/partners/anthropic/langchain_anthropic/chat_models.py b/libs/partners/anthropic/langchain_anthropic/chat_models.pyindex 370171472e..998338c418 100644--- a/libs/partners/anthropic/langchain_anthropic/chat_models.py+++ b/libs/partners/anthropic/langchain_anthropic/chat_models.py@@ -1126,6 +1126,17 @@ class ChatAnthropic(BaseChatModel):docs for more information."""+ user_profile_id: str | None = None+ """User profile ID to attribute the request to.++ Use when acting on behalf of a party other than your organization. Setting this+ automatically enables the required `user-profiles` beta, routing the request+ through `client.beta.messages.create`.++ Can also be passed at call time, which overrides the value set here (for example,+ `model.invoke(..., user_profile_id="uprof_...")`).+ """+@propertydef effort(self) -> Literal["max", "xhigh", "high", "medium", "low"] | None:"""Alias for `reasoning_effort`."""@@ -1421,6 +1432,7 @@ class ChatAnthropic(BaseChatModel):"betas": self.betas,"context_management": self.context_management,"mcp_servers": self.mcp_servers,+ "user_profile_id": self.user_profile_id,"system": system,**self.model_kwargs,**kwargs,@@ -1569,6 +1581,15 @@ class ChatAnthropic(BaseChatModel):else:payload["betas"] = [required_beta]+ # Auto-append required beta for user_profile_id+ if payload.get("user_profile_id"):+ required_beta = "user-profiles-2026-03-24"+ if payload.get("betas"):+ if required_beta not in payload["betas"]:+ payload["betas"] = [*payload["betas"], required_beta]+ else:+ payload["betas"] = [required_beta]+return {k: v for k, v in payload.items() if v is not None}def _create(self, payload: dict) -> Any:diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.pyindex 2ee0b58ff2..6915a65bbf 100644--- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py+++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py@@ -2159,6 +2159,41 @@ def test_inference_geo_in_payload() -> None:assert payload["inference_geo"] == "us"+def test_user_profile_id_init_param() -> None:+ """`user_profile_id` set at construction is included in the payload."""+ llm = ChatAnthropic(model=MODEL_NAME, user_profile_id="uprof_init")+ input_message = HumanMessage("Hello, world!")+ payload = llm._get_request_payload([input_message])+ assert payload["user_profile_id"] == "uprof_init"+ # Setting it auto-enables the required beta, routing through beta.messages.create.+ assert "user-profiles-2026-03-24" in payload["betas"]+++def test_user_profile_id_runtime_param() -> None:+ """`user_profile_id` passed at call time is included in the payload."""+ llm = ChatAnthropic(model=MODEL_NAME)+ input_message = HumanMessage("Hello, world!")+ payload = llm._get_request_payload([input_message], user_profile_id="uprof_runtime")+ assert payload["user_profile_id"] == "uprof_runtime"+ assert "user-profiles-2026-03-24" in payload["betas"]+++def test_user_profile_id_runtime_overrides_init() -> None:+ """A call-time `user_profile_id` takes precedence over the init value."""+ llm = ChatAnthropic(model=MODEL_NAME, user_profile_id="uprof_init")+ input_message = HumanMessage("Hello, world!")+ payload = llm._get_request_payload([input_message], user_profile_id="uprof_runtime")+ assert payload["user_profile_id"] == "uprof_runtime"+++def test_user_profile_id_absent_by_default() -> None:+ """When unset, `user_profile_id` is stripped from the payload."""+ llm = ChatAnthropic(model=MODEL_NAME)+ input_message = HumanMessage("Hello, world!")+ payload = llm._get_request_payload([input_message])+ assert "user_profile_id" not in payload++def test_anthropic_model_params() -> None:llm = ChatAnthropic(model=MODEL_NAME)