Skip to content

Commit fd2fbd7

Browse files
authored
feat(anthropic): support 1.0 sdk (#39938)
1 parent b0fb33f commit fd2fbd7

11 files changed

Lines changed: 439 additions & 34 deletions

File tree

‎.github/scripts/get_min_versions.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"langchain-text-splitters",
2424
"numpy",
2525
"SQLAlchemy",
26+
"anthropic",
27+
"openai",
2628
]
2729

2830
# some libs only get checked on release because of simultaneous changes in

‎libs/partners/anthropic/Makefile‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ TEST_FILE ?= tests/unit_tests/
1111
PYTEST_EXTRA ?=
1212
integration_test integration_tests: TEST_FILE=tests/integration_tests/
1313

14+
SDK_SMOKE_TEST = tests/integration_tests/test_chat_models.py::test_stream
15+
1416
test tests:
1517
uv run --group test pytest -vvv $(PYTEST_EXTRA) --disable-socket --allow-unix-socket $(TEST_FILE)
1618

19+
# Smoke each supported anthropic major first: 0.x is backed by `httpx`, 1.x by
20+
# `httpx2`, and uv.lock only ever resolves one of them.
1721
integration_test integration_tests:
22+
uv run --with 'anthropic>=0.120.0,<1.0.0' --group test --group test_integration pytest -v --tb=short $(SDK_SMOKE_TEST)
23+
uv run --with 'anthropic>=1.0.0,<2.0.0' --group test --group test_integration pytest -v --tb=short $(SDK_SMOKE_TEST)
1824
uv run --group test --group test_integration pytest -v --tb=short -n auto --benchmark-disable --timeout 30 --retries 3 --retry-delay 1 $(TEST_FILE)
1925

2026
test_watch:
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
"""Adapt to the installed `anthropic` SDK major version.
2+
3+
`langchain-anthropic` supports `anthropic` 0.x and 1.x. Two differences between
4+
them reach this package, and each is resolved here by reading the answer off the
5+
installed SDK rather than off its version string:
6+
7+
- **Sampling parameters.** `anthropic>=1` dropped `temperature`, `top_p` and
8+
`top_k` from its message-creation methods, since current models do not use
9+
them. The API still honors them for older models, so they are relocated to
10+
`extra_body`, which the SDK merges into the request JSON as-is. The wire
11+
payload is identical on both majors.
12+
13+
- **Raw responses.** `.with_raw_response` on the async client returns
14+
`AsyncAPIResponse`, whose `parse()` is a coroutine; `anthropic<1` returned
15+
`LegacyAPIResponse` with a synchronous `parse()`.
16+
17+
`anthropic>=1` also moved its HTTP layer from `httpx` to
18+
[`httpx2`](https://github.com/pydantic/httpx2), but that does not surface here:
19+
this package hands the SDK only primitives (a `base_url` string, a float
20+
`timeout`, a `proxy` URL string) and subclasses `anthropic.DefaultHttpxClient`,
21+
which already points at the right library, so no `httpx` type crosses the
22+
boundary. Only the unit tests inject `httpx` objects, and they resolve the
23+
matching library themselves in `tests/unit_tests/_httpx_compat.py`.
24+
"""
25+
26+
from __future__ import annotations
27+
28+
import inspect
29+
import logging
30+
import re
31+
from collections.abc import Mapping
32+
from functools import lru_cache
33+
from typing import Any
34+
35+
import anthropic
36+
37+
logger = logging.getLogger(__name__)
38+
39+
# Sampling parameters `anthropic>=1` no longer accepts as named arguments.
40+
_SAMPLING_PARAMS = ("temperature", "top_p", "top_k")
41+
42+
43+
def _sdk_major_version() -> int | None:
44+
"""Major version of the installed `anthropic` SDK, or `None` if unreadable.
45+
46+
Tolerates tag-derived strings such as `v1.0.0`, which build tooling
47+
sometimes leaves unstripped, by reading the first integer run.
48+
"""
49+
match = re.match(r"\D*(\d+)", str(getattr(anthropic, "__version__", "")))
50+
return int(match.group(1)) if match else None
51+
52+
53+
@lru_cache(maxsize=1)
54+
def _unsupported_sampling_params() -> frozenset[str]:
55+
"""Sampling parameters the installed SDK's message methods no longer accept.
56+
57+
Derived from the signature of `Messages.create`, which `beta.messages` and
58+
the async resources track. No client is instantiated, so this is safe to
59+
call during payload construction without resolved credentials.
60+
"""
61+
try:
62+
from anthropic.resources.messages import Messages
63+
64+
accepted = inspect.signature(Messages.create).parameters
65+
except (ImportError, AttributeError, TypeError, ValueError):
66+
# The SDK moved the resource or the signature is unreadable. Guess from
67+
# the major version rather than failing: a wrong guess costs a TypeError
68+
# on the first request, an exception here breaks every import.
69+
major = _sdk_major_version()
70+
logger.warning(
71+
"Could not introspect the anthropic SDK's `Messages.create` "
72+
"signature; falling back to its major version (%r) to decide "
73+
"whether `temperature`/`top_p`/`top_k` are still accepted.",
74+
major,
75+
)
76+
if major is not None and major >= 1:
77+
return frozenset(_SAMPLING_PARAMS)
78+
return frozenset()
79+
80+
return frozenset(p for p in _SAMPLING_PARAMS if p not in accepted)
81+
82+
83+
def _route_unsupported_sampling_params(payload: dict[str, Any]) -> dict[str, Any]:
84+
"""Move sampling params the installed SDK rejects into `extra_body`.
85+
86+
A no-op on `anthropic<1`, where the message methods still accept them as
87+
named arguments. Values the caller already set in `extra_body` win, since
88+
that is the SDK's documented escape hatch and a deliberate choice.
89+
90+
Args:
91+
payload: Request payload, modified in place.
92+
93+
Returns:
94+
The same payload.
95+
"""
96+
unsupported = _unsupported_sampling_params()
97+
if not unsupported:
98+
return payload
99+
100+
extra_body = payload.get("extra_body")
101+
if extra_body is not None and not isinstance(extra_body, Mapping):
102+
# A value the SDK would reject anyway. Leave the payload untouched
103+
# rather than relocating params into something that cannot hold them.
104+
return payload
105+
106+
relocated = {k: payload.pop(k) for k in unsupported if k in payload}
107+
if not relocated:
108+
return payload
109+
110+
payload["extra_body"] = {**relocated, **(extra_body or {})}
111+
return payload
112+
113+
114+
async def _aparse(raw_response: Any) -> Any:
115+
"""Parse a raw SDK response from the async client.
116+
117+
`anthropic>=1` returns `AsyncAPIResponse`, whose `parse()` is a coroutine;
118+
`anthropic<1` returned `LegacyAPIResponse`, whose `parse()` was
119+
synchronous.
120+
"""
121+
parsed = raw_response.parse()
122+
if inspect.isawaitable(parsed):
123+
return await parsed
124+
return parsed

‎libs/partners/anthropic/langchain_anthropic/chat_models.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
_get_default_httpx_client,
8585
)
8686
from langchain_anthropic._compat import _convert_from_v1_to_anthropic
87+
from langchain_anthropic._sdk_compat import (
88+
_aparse,
89+
_route_unsupported_sampling_params,
90+
)
8791
from langchain_anthropic.data._profiles import _PROFILES
8892
from langchain_anthropic.output_parsers import extract_tool_calls
8993

@@ -1736,7 +1740,9 @@ def _get_request_payload(
17361740
else:
17371741
payload["betas"] = [required_beta]
17381742

1739-
return {k: v for k, v in payload.items() if v is not None}
1743+
return _route_unsupported_sampling_params(
1744+
{k: v for k, v in payload.items() if v is not None}
1745+
)
17401746

17411747
def _create(self, payload: dict) -> Any:
17421748
try:
@@ -1826,7 +1832,7 @@ async def _astream(
18261832
base_generation_info: dict[str, Any] = {}
18271833
if self._uses_gateway:
18281834
_add_gateway_metadata(base_generation_info, raw_response)
1829-
stream = raw_response.parse()
1835+
stream = await _aparse(raw_response)
18301836
coerce_content_to_string = (
18311837
not _tools_in_params(payload)
18321838
and not _documents_in_params(payload)
@@ -2185,7 +2191,7 @@ async def _agenerate(
21852191
if self._uses_gateway:
21862192
_add_gateway_metadata(generation_info, raw_response)
21872193
return self._format_output(
2188-
raw_response.parse(), generation_info=generation_info, **kwargs
2194+
await _aparse(raw_response), generation_info=generation_info, **kwargs
21892195
)
21902196

21912197
def _get_llm_for_structured_output_when_thinking_is_enabled(

‎libs/partners/anthropic/langchain_anthropic/llms.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from pydantic import ConfigDict, Field, SecretStr, model_validator
2222
from typing_extensions import Self
2323

24+
from langchain_anthropic._sdk_compat import _route_unsupported_sampling_params
25+
2426

2527
class _AnthropicCommon(BaseLanguageModel):
2628
client: Any = None
@@ -276,6 +278,7 @@ def _call(
276278

277279
# Remove parameters not supported by Messages API
278280
params = {k: v for k, v in params.items() if k != "max_tokens_to_sample"}
281+
params = _route_unsupported_sampling_params(params)
279282

280283
response = self.client.messages.create(
281284
messages=self._format_messages(prompt),
@@ -312,6 +315,7 @@ async def _acall(
312315

313316
# Remove parameters not supported by Messages API
314317
params = {k: v for k, v in params.items() if k != "max_tokens_to_sample"}
318+
params = _route_unsupported_sampling_params(params)
315319

316320
response = await self.async_client.messages.create(
317321
messages=self._format_messages(prompt),
@@ -352,6 +356,7 @@ def _stream(
352356

353357
# Remove parameters not supported by Messages API
354358
params = {k: v for k, v in params.items() if k != "max_tokens_to_sample"}
359+
params = _route_unsupported_sampling_params(params)
355360

356361
with self.client.messages.stream(
357362
messages=self._format_messages(prompt),
@@ -397,6 +402,7 @@ async def _astream(
397402

398403
# Remove parameters not supported by Messages API
399404
params = {k: v for k, v in params.items() if k != "max_tokens_to_sample"}
405+
params = _route_unsupported_sampling_params(params)
400406

401407
async with self.async_client.messages.stream(
402408
messages=self._format_messages(prompt),

‎libs/partners/anthropic/pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ classifiers = [
2323
version = "1.6.1"
2424
requires-python = ">=3.10.0,<4.0.0"
2525
dependencies = [
26-
"anthropic>=0.120.0,<1.0.0",
26+
"anthropic>=0.120.0,<2.0.0",
2727
"langchain-core>=1.6.0,<2.0.0",
2828
"pydantic>=2.7.4,<3.0.0",
2929
]
@@ -52,7 +52,7 @@ test = [
5252
"pytest-timeout>=2.3.1,<3.0.0",
5353
"pytest-socket>=0.7.0,<1.0.0",
5454
"pytest-xdist>=3.8.0,<4.0.0",
55-
"vcrpy>=8.0.0,<9.0.0",
55+
"vcrpy>=8.2.0,<9.0.0",
5656
"langgraph-prebuilt>=0.7.0a2", # set explicitly until we have a stable version
5757
"langchain-tests>=1.1.9,<2.0.0",
5858
"langchain>=1.0.0,<2.0.0",

‎libs/partners/anthropic/tests/integration_tests/test_chat_models.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def test_astream() -> None:
140140
"model": MODEL_NAME,
141141
"max_tokens": 1024,
142142
"messages": [{"role": "user", "content": "hi"}],
143-
"temperature": 0.0,
143+
"extra_body": {"temperature": 0.0},
144144
}
145145
stream = await async_client.messages.create(**params, stream=True)
146146
async for event in stream:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Resolve the `httpx` library the installed `anthropic` SDK is built on.
2+
3+
Test-only. `langchain_anthropic` hands the SDK nothing but primitives — a
4+
`base_url` string, a float `timeout`, a `proxy` URL string — and gets its client
5+
class by subclassing `anthropic.DefaultHttpxClient`, which already points at the
6+
right library. So no `httpx` type crosses that boundary in library code and
7+
nothing there needs to know which library is in play.
8+
9+
These tests do cross it: they inject mock transports, and the SDK accepts a
10+
built client only from the library it is itself built on (`httpx` on
11+
`anthropic<1`, `httpx2` on `anthropic>=1`), rejecting the other with a
12+
`TypeError`. `anthropic.DefaultHttpxClient` subclasses that library's client
13+
class, so its MRO names the one to use.
14+
"""
15+
16+
from __future__ import annotations
17+
18+
import importlib
19+
20+
import anthropic
21+
22+
_HTTPX_MODULES = ("httpx2", "httpx")
23+
24+
25+
def _resolve_httpx_module() -> str:
26+
"""Name of the HTTP library `anthropic.DefaultHttpxClient` derives from.
27+
28+
Raises:
29+
RuntimeError: If the SDK exposes no recognizable base class. Guessing
30+
from the version string would be wrong as often as it was right;
31+
failing at import makes the mismatch obvious here rather than in a
32+
confusing downstream `TypeError`.
33+
"""
34+
for base in anthropic.DefaultHttpxClient.__mro__:
35+
root = base.__module__.split(".", 1)[0]
36+
if root in _HTTPX_MODULES:
37+
return root
38+
39+
msg = (
40+
f"Cannot tell which httpx library anthropic "
41+
f"{getattr(anthropic, '__version__', 'unknown')} is built on: "
42+
f"DefaultHttpxClient derives from none of {_HTTPX_MODULES}."
43+
)
44+
raise RuntimeError(msg)
45+
46+
47+
httpx = importlib.import_module(_resolve_httpx_module())

‎libs/partners/anthropic/tests/unit_tests/test_chat_models.py‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from unittest.mock import MagicMock, patch
1313

1414
import anthropic
15-
import httpx
1615
import pytest
1716
from anthropic.types import Message, TextBlock, Usage
1817
from blockbuster import blockbuster_ctx
@@ -45,6 +44,7 @@
4544
from pytest import CaptureFixture, MonkeyPatch
4645

4746
from langchain_anthropic import ChatAnthropic
47+
from langchain_anthropic._sdk_compat import _unsupported_sampling_params
4848
from langchain_anthropic._version import __version__
4949
from langchain_anthropic.chat_models import (
5050
_TOOL_CALL_ID_PATTERN,
@@ -58,6 +58,7 @@
5858
_thinking_in_params,
5959
convert_to_anthropic_tool,
6060
)
61+
from tests.unit_tests._httpx_compat import httpx
6162

6263
os.environ["ANTHROPIC_API_KEY"] = "foo"
6364

@@ -118,8 +119,10 @@ def _on_llm_end(self, run: Run) -> None:
118119

119120
def _gateway_handler(
120121
expected_beta: str | None,
121-
) -> Callable[[httpx.Request], httpx.Response]:
122-
def handler(request: httpx.Request) -> httpx.Response:
122+
) -> Callable[[Any], Any]:
123+
# Annotated `Any`: the concrete request/response classes come from `httpx`
124+
# or `httpx2` depending on the installed anthropic SDK.
125+
def handler(request: Any) -> Any:
123126
assert request.headers.get("anthropic-beta") == expected_beta
124127
headers = {"x-langsmith-gateway-metadata": json.dumps(_GATEWAY_METADATA)}
125128
if json.loads(request.content).get("stream"):
@@ -2507,6 +2510,24 @@ def mock_create(*args: Any, **kwargs: Any) -> Message:
25072510
assert payload["mcp_servers"][0]["authorization_token"] == "PLACEHOLDER" # noqa: S105
25082511

25092512

2513+
def test_sampling_params_reach_the_request_payload() -> None:
2514+
"""`temperature`/`top_p`/`top_k` land wherever the installed SDK accepts them.
2515+
2516+
`anthropic>=1` dropped them as named arguments, so they are relocated to
2517+
`extra_body`; the resulting wire payload is the same on both majors.
2518+
"""
2519+
llm = ChatAnthropic(model=MODEL_NAME, temperature=0, top_p=0.9, top_k=5)
2520+
payload = llm._get_request_payload([HumanMessage("foo")])
2521+
2522+
sampling = {"temperature": 0, "top_p": 0.9, "top_k": 5}
2523+
if _unsupported_sampling_params():
2524+
assert payload["extra_body"] == sampling
2525+
assert not sampling.keys() & payload.keys()
2526+
else:
2527+
assert {k: payload[k] for k in sampling} == sampling
2528+
assert "extra_body" not in payload
2529+
2530+
25102531
def test_cache_control_kwarg() -> None:
25112532
llm = ChatAnthropic(model=MODEL_NAME)
25122533

0 commit comments

Comments
 (0)