8000 add logic for deprecated models and test · anthropics/anthropic-sdk-python@358670d · GitHub
[go: up one dir, main page]

Skip to content

Commit 358670d

Browse files
dtmeadowsstainless-app[bot]
authored andcommitted
add logic for deprecated models and test
1 parent 9b6515a commit 358670d

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

src/anthropic/resources/beta/messages/messages.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,13 @@ def stream(
10591059
extra_body: Body | None = None,
10601060
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
10611061
) -> BetaMessageStreamManager:
1062+
if model in DEPRECATED_MODELS:
1063+
warnings.warn(
1064+
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
1065+
DeprecationWarning,
1066+
stacklevel=3,
1067+
)
1068+
10621069
"""Create a Message stream"""
10631070
extra_headers = {
10641071
"X-Stainless-Stream-Helper": "beta.messages",
@@ -2361,6 +2368,13 @@ def stream(
23612368
extra_body: Body | None = None,
23622369
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
23632370
) -> BetaAsyncMessageStreamManager:
2371+
if model in DEPRECATED_MODELS:
2372+
warnings.warn(
2373+
f"The model '{model}' is deprecated and will reach end-of-life on {DEPRECATED_MODELS[model]}.\nPlease migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.",
2374+
DeprecationWarning,
2375+
stacklevel=3,
2376+
)
2377+
23642378
extra_headers = {
23652379
"X-Stainless-Stream-Helper": "beta.messages",
23662380
**strip_not_given({"anthropic-beta": ",".join(str(e) for e in betas) if is_given(betas) else NOT_GIVEN}),

tests/lib/streaming/test_beta_messages.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010
from respx import MockRouter
1111

12-
from anthropic import Stream, Anthropic, AsyncStream, AsyncAnthropic
12+
from anthropic import Anthropic, AsyncAnthropic
1313
from anthropic._compat import PYDANTIC_V2
1414
from anthropic.types.beta.beta_message import BetaMessage
1515
from anthropic.lib.streaming._beta_types import BetaMessageStreamEvent
@@ -203,9 +203,6 @@ def assert_tool_use_response(events: list[BetaMessageStreamEvent], message: Beta
203203
class TestSyncMessages:
204204
@pytest.mark.respx(base_url=base_url)
205205
def test_basic_response(self, respx_mock: MockRouter) -> None:
206-
# skip for now... this isn't in `stream`?
207-
pytest.skip("Deprecated model warnings are not currently implemented for streaming...")
208-
209206
respx_mock.post("/v1/messages").mock(return_value=httpx.Response(200, content=basic_response()))
210207

211208
with sync_client.beta.messages.stream(
@@ -218,8 +215,7 @@ def test_basic_response(self, respx_mock: MockRouter) -> None:
218215
],
219216
model="claude-3-opus-20240229",
220217
) as stream:
221-
with pytest.warns(DeprecationWarning):
222-
assert isinstance(cast(Any, stream), Stream)
218+
assert isinstance(cast(Any, stream), BetaMessageStream)
223219

224220
assert_basic_response([event for event in stream], stream.get_final_message())
225221

@@ -264,8 +260,6 @@ def test_context_manager(self, respx_mock: MockRouter) -> None:
264260

265261
@pytest.mark.respx(base_url=base_url)
266262
def test_deprecated_model_warning_stream(self, respx_mock: MockRouter) -> None:
267-
# skip for now... this isn't in `stream`?
268-
pytest.skip("Deprecated model warnings are not currently implemented for streaming...")
269263
for deprecated_model in DEPRECATED_MODELS:
270264
respx_mock.post("/v1/messages").mock(return_value=httpx.Response(200, content=basic_response()))
271265

@@ -283,8 +277,6 @@ class TestAsyncMessages:
283277
@pytest.mark.asyncio
284278
@pytest.mark.respx(base_url=base_url)
285279
async def test_basic_response(self, respx_mock: MockRouter) -> None:
286-
# skip for now... this isn't in `stream`?
287-
pytest.skip("Deprecated model warnings are not currently implemented for streaming...")
288280
respx_mock.post("/v1/messages").mock(return_value=httpx.Response(200, content=to_async_iter(basic_response())))
289281

290282
async with async_client.beta.messages.stream(
@@ -295,10 +287,9 @@ async def test_basic_response(self, respx_mock: MockRouter) -> None:
295287
"content": "Say hello there!",
296288
}
297289
],
298-
model="claude-3-opus-20240229",
290+
model="claude-opus-4-0",
299291
) as stream:
300-
with pytest.warns(DeprecationWarning):
301-
assert isinstance(cast(Any, stream), AsyncStream)
292+
assert isinstance(cast(Any, stream), BetaAsyncMessageStream)
302293

303294
assert_basic_response([event async for event in stream], await stream.get_final_message())
304295

@@ -325,8 +316,6 @@ async def test_context_manager(self, respx_mock: MockRouter) -> None:
325316
@pytest.mark.asyncio
326317
@pytest.mark.respx(base_url=base_url)
327318
async def test_deprecated_model_warning_stream(self, respx_mock: MockRouter) -> None:
328-
# skip for now... this isn't in `stream`?
329-
pytest.skip("Deprecated model warnings are not currently implemented for streaming...")
330319
for deprecated_model in DEPRECATED_MODELS:
331320
respx_mock.post("/v1/messages").mock(
332321
return_value=httpx.Response(200, content=to_async_iter(basic_response()))
@@ -394,6 +383,7 @@ def test_stream_method_definition_in_sync(sync: bool) -> None:
394383
+ "\n\n".join(errors)
395384
)
396385

386+
397387
# go through all the ContentBlock types to make sure the type alias is up to date
398388
# with any type that has an input property of type object
399389
def test_tracks_tool_input_type_alias_is_up_to_date() -> None:

tests/lib/streaming/test_messages.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from anthropic import Stream, Anthropic, AsyncStream, AsyncAnthropic
1313
from anthropic._compat import PYDANTIC_V2
1414
from anthropic.lib.streaming import MessageStreamEvent
15-
from anthropic.lib.streaming._messages import TRACKS_TOOL_INPUT
1615
from anthropic.types.message import Message
1716
from anthropic.resources.messages import DEPRECATED_MODELS
17+
from anthropic.lib.streaming._messages import TRACKS_TOOL_INPUT
1818

1919
base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
2020
api_key = "my-anthropic-api-key"
@@ -144,6 +144,7 @@ def assert_basic_response(events: list[MessageStreamEvent], message: Message) ->
144144
"message_delta",
145145
]
146146

147+
147148
def assert_tool_use_response(events: list[MessageStreamEvent], message: Message) -> None:
148149
assert message.id == "msg_019Q1hrJbZG26Fb9BQhrkHEr"
149150
assert message.model == "claude-sonnet-4-20250514"
@@ -197,6 +198,7 @@ def assert_tool_use_response(events: list[MessageStreamEvent], message: Message)
197198
"message_delta",
198199
]
199200

201+
200202
class TestSyncMessages:
201203
@pytest.mark.respx(base_url=base_url)
202204
def test_basic_response(self, respx_mock: MockRouter) -> None:
@@ -352,6 +354,7 @@ async def test_tool_use(self, respx_mock: MockRouter) -> None:
352354

353355
assert_tool_use_response([event async for event in stream], await stream.get_final_message())
354356

357+
355358
@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"])
356359
def test_stream_method_definition_in_sync(sync: bool) -> None:
357360
client: Anthropic | AsyncAnthropic = sync_client if sync else async_client

0 commit comments

Comments
 (0)
0