anthropic 0.68.2__py3-none-any.whl → 0.70.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. anthropic/_version.py +1 -1
  2. anthropic/lib/streaming/_beta_messages.py +1 -0
  3. anthropic/lib/tools/__init__.py +7 -0
  4. anthropic/lib/tools/_beta_builtin_memory_tool.py +245 -0
  5. anthropic/lib/tools/_beta_functions.py +30 -0
  6. anthropic/lib/tools/_beta_runner.py +18 -6
  7. anthropic/resources/beta/messages/messages.py +55 -11
  8. anthropic/types/anthropic_beta_param.py +2 -0
  9. anthropic/types/beta/__init__.py +35 -0
  10. anthropic/types/beta/beta_clear_tool_uses_20250919_edit_param.py +38 -0
  11. anthropic/types/beta/beta_clear_tool_uses_20250919_edit_response.py +18 -0
  12. anthropic/types/beta/beta_context_management_config_param.py +15 -0
  13. anthropic/types/beta/beta_context_management_response.py +13 -0
  14. anthropic/types/beta/beta_count_tokens_context_management_response.py +10 -0
  15. anthropic/types/beta/beta_input_tokens_clear_at_least_param.py +13 -0
  16. anthropic/types/beta/beta_input_tokens_trigger_param.py +13 -0
  17. anthropic/types/beta/beta_memory_tool_20250818_command.py +26 -0
  18. anthropic/types/beta/beta_memory_tool_20250818_create_command.py +18 -0
  19. anthropic/types/beta/beta_memory_tool_20250818_delete_command.py +15 -0
  20. anthropic/types/beta/beta_memory_tool_20250818_insert_command.py +21 -0
  21. anthropic/types/beta/beta_memory_tool_20250818_param.py +23 -0
  22. anthropic/types/beta/beta_memory_tool_20250818_rename_command.py +18 -0
  23. anthropic/types/beta/beta_memory_tool_20250818_str_replace_command.py +21 -0
  24. anthropic/types/beta/beta_memory_tool_20250818_view_command.py +19 -0
  25. anthropic/types/beta/beta_message.py +4 -0
  26. anthropic/types/beta/beta_message_tokens_count.py +6 -0
  27. anthropic/types/beta/beta_raw_message_delta_event.py +4 -0
  28. anthropic/types/beta/beta_stop_reason.py +3 -1
  29. anthropic/types/beta/beta_tool_union_param.py +2 -0
  30. anthropic/types/beta/beta_tool_uses_keep_param.py +13 -0
  31. anthropic/types/beta/beta_tool_uses_trigger_param.py +13 -0
  32. anthropic/types/beta/message_count_tokens_params.py +7 -1
  33. anthropic/types/beta/message_create_params.py +4 -0
  34. anthropic/types/model.py +4 -0
  35. anthropic/types/model_param.py +4 -0
  36. anthropic/types/stop_reason.py +3 -1
  37. {anthropic-0.68.2.dist-info → anthropic-0.70.0.dist-info}/METADATA +19 -19
  38. {anthropic-0.68.2.dist-info → anthropic-0.70.0.dist-info}/RECORD +40 -22
  39. {anthropic-0.68.2.dist-info → anthropic-0.70.0.dist-info}/WHEEL +0 -0
  40. {anthropic-0.68.2.dist-info → anthropic-0.70.0.dist-info}/licenses/LICENSE +0 -0
anthropic/_version.py CHANGED
@@ -1,4 +1,4 @@
1
1
  # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
3
  __title__ = "anthropic"
4
- __version__ = "0.68.2" # x-release-please-version
4
+ __version__ = "0.70.0" # x-release-please-version
@@ -486,6 +486,7 @@ def accumulate_event(
486
486
  current_snapshot.stop_reason = event.delta.stop_reason
487
487
  current_snapshot.stop_sequence = event.delta.stop_sequence
488
488
  current_snapshot.usage.output_tokens = event.usage.output_tokens
489
+ current_snapshot.context_management = event.context_management
489
490
 
490
491
  # Update other usage fields if they exist in the event
491
492
  if event.usage.input_tokens is not None:
@@ -2,19 +2,26 @@ from ._beta_runner import BetaToolRunner, BetaAsyncToolRunner, BetaStreamingTool
2
2
  from ._beta_functions import (
3
3
  BetaFunctionTool,
4
4
  BetaAsyncFunctionTool,
5
+ BetaBuiltinFunctionTool,
5
6
  BetaFunctionToolResultType,
7
+ BetaAsyncBuiltinFunctionTool,
6
8
  beta_tool,
7
9
  beta_async_tool,
8
10
  )
11
+ from ._beta_builtin_memory_tool import BetaAbstractMemoryTool, BetaAsyncAbstractMemoryTool
9
12
 
10
13
  __all__ = [
11
14
  "beta_tool",
12
15
  "beta_async_tool",
13
16
  "BetaFunctionTool",
14
17
  "BetaAsyncFunctionTool",
18
+ "BetaBuiltinFunctionTool",
19
+ "BetaAsyncBuiltinFunctionTool",
15
20
  "BetaToolRunner",
16
21
  "BetaAsyncStreamingToolRunner",
17
22
  "BetaStreamingToolRunner",
18
23
  "BetaAsyncToolRunner",
19
24
  "BetaFunctionToolResultType",
25
+ "BetaAbstractMemoryTool",
26
+ "BetaAsyncAbstractMemoryTool",
20
27
  ]
@@ -0,0 +1,245 @@
1
+ from __future__ import annotations
2
+
3
+ from abc import abstractmethod
4
+ from typing import TYPE_CHECKING, Any, cast
5
+ from typing_extensions import override, assert_never
6
+
7
+ from ..._models import construct_type_unchecked
8
+ from ...types.beta import (
9
+ BetaMemoryTool20250818Param,
10
+ BetaMemoryTool20250818Command,
11
+ BetaCacheControlEphemeralParam,
12
+ BetaMemoryTool20250818ViewCommand,
13
+ BetaMemoryTool20250818CreateCommand,
14
+ BetaMemoryTool20250818DeleteCommand,
15
+ BetaMemoryTool20250818InsertCommand,
16
+ BetaMemoryTool20250818RenameCommand,
17
+ BetaMemoryTool20250818StrReplaceCommand,
18
+ )
19
+ from ._beta_functions import BetaBuiltinFunctionTool, BetaFunctionToolResultType, BetaAsyncBuiltinFunctionTool
20
+
21
+
22
+ class BetaAbstractMemoryTool(BetaBuiltinFunctionTool):
23
+ """Abstract base class for memory tool implementations.
24
+
25
+ This class provides the interface for implementing a custom memory backend for Claude.
26
+
27
+ Subclass this to create your own memory storage solution (e.g., database, cloud storage, encrypted files, etc.).
28
+
29
+ Example usage:
30
+
31
+ ```py
32
+ class MyMemoryTool(BetaAbstractMemoryTool):
33
+ def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
34
+ ...
35
+ return "view result"
36
+
37
+ def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
38
+ ...
39
+ return "created successfully"
40
+
41
+ # ... implement other abstract methods
42
+
43
+
44
+ client = Anthropic()
45
+ memory_tool = MyMemoryTool()
46
+ message = client.beta.messages.run_tools(
47
+ model="claude-sonnet-4-5",
48
+ messages=[{"role": "user", "content": "Remember that I like coffee"}],
49
+ tools=[memory_tool],
50
+ ).until_done()
51
+ ```
52
+ """
53
+
54
+ def __init__(self, *, cache_control: BetaCacheControlEphemeralParam | None = None) -> None:
55
+ super().__init__()
56
+ self._cache_control = cache_control
57
+
58
+ @override
59
+ def to_dict(self) -> BetaMemoryTool20250818Param:
60
+ param: BetaMemoryTool20250818Param = {"type": "memory_20250818", "name": "memory"}
61
+
62
+ if self._cache_control is not None:
63
+ param["cache_control"] = self._cache_control
64
+
65
+ return param
66
+
67
+ @override
68
+ def call(self, input: object) -> BetaFunctionToolResultType:
69
+ command = cast(
70
+ BetaMemoryTool20250818Command,
71
+ construct_type_unchecked(value=input, type_=cast(Any, BetaMemoryTool20250818Command)),
72
+ )
73
+ return self.execute(command)
74
+
75
+ def execute(self, command: BetaMemoryTool20250818Command) -> BetaFunctionToolResultType:
76
+ """Execute a memory command and return the result.
77
+
78
+ This method dispatches to the appropriate handler method based on the
79
+ command type (view, create, str_replace, insert, delete, rename).
80
+
81
+ You typically don't need to override this method.
82
+ """
83
+ if command.command == "view":
84
+ return self.view(command)
85
+ elif command.command == "create":
86
+ return self.create(command)
87
+ elif command.command == "str_replace":
88
+ return self.str_replace(command)
89
+ elif command.command == "insert":
90
+ return self.insert(command)
91
+ elif command.command == "delete":
92
+ return self.delete(command)
93
+ elif command.command == "rename":
94
+ return self.rename(command)
95
+ elif TYPE_CHECKING: # type: ignore[unreachable]
96
+ assert_never(command)
97
+ else:
98
+ raise NotImplementedError(f"Unknown command: {command.command}")
99
+
100
+ @abstractmethod
101
+ def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
102
+ """View the contents of a memory path."""
103
+ pass
104
+
105
+ @abstractmethod
106
+ def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
107
+ """Create a new memory file with the specified content."""
108
+ pass
109
+
110
+ @abstractmethod
111
+ def str_replace(self, command: BetaMemoryTool20250818StrReplaceCommand) -> BetaFunctionToolResultType:
112
+ """Replace text in a memory file."""
113
+ pass
114
+
115
+ @abstractmethod
116
+ def insert(self, command: BetaMemoryTool20250818InsertCommand) -> BetaFunctionToolResultType:
117
+ """Insert text at a specific line number in a memory file."""
118
+ pass
119
+
120
+ @abstractmethod
121
+ def delete(self, command: BetaMemoryTool20250818DeleteCommand) -> BetaFunctionToolResultType:
122
+ """Delete a memory file or directory."""
123
+ pass
124
+
125
+ @abstractmethod
126
+ def rename(self, command: BetaMemoryTool20250818RenameCommand) -> BetaFunctionToolResultType:
127
+ """Rename or move a memory file or directory."""
128
+ pass
129
+
130
+ def clear_all_memory(self) -> BetaFunctionToolResultType:
131
+ """Clear all memory data."""
132
+ raise NotImplementedError("clear_all_memory not implemented")
133
+
134
+
135
+ class BetaAsyncAbstractMemoryTool(BetaAsyncBuiltinFunctionTool):
136
+ """Abstract base class for memory tool implementations.
137
+
138
+ This class provides the interface for implementing a custom memory backend for Claude.
139
+
140
+ Subclass this to create your own memory storage solution (e.g., database, cloud storage, encrypted files, etc.).
141
+
142
+ Example usage:
143
+
144
+ ```py
145
+ class MyMemoryTool(BetaAbstractMemoryTool):
146
+ def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
147
+ ...
148
+ return "view result"
149
+
150
+ def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
151
+ ...
152
+ return "created successfully"
153
+
154
+ # ... implement other abstract methods
155
+
156
+
157
+ client = Anthropic()
158
+ memory_tool = MyMemoryTool()
159
+ message = client.beta.messages.run_tools(
160
+ model="claude-3-5-sonnet-20241022",
161
+ messages=[{"role": "user", "content": "Remember that I like coffee"}],
162
+ tools=[memory_tool],
163
+ ).until_done()
164
+ ```
165
+ """
166
+
167
+ def __init__(self, *, cache_control: BetaCacheControlEphemeralParam | None = None) -> None:
168
+ super().__init__()
169
+ self._cache_control = cache_control
170
+
171
+ @override
172
+ def to_dict(self) -> BetaMemoryTool20250818Param:
173
+ param: BetaMemoryTool20250818Param = {"type": "memory_20250818", "name": "memory"}
174
+
175
+ if self._cache_control is not None:
176
+ param["cache_control"] = self._cache_control
177
+
178
+ return param
179
+
180
+ @override
181
+ async def call(self, input: object) -> BetaFunctionToolResultType:
182
+ command = cast(
183
+ BetaMemoryTool20250818Command,
184
+ construct_type_unchecked(value=input, type_=cast(Any, BetaMemoryTool20250818Command)),
185
+ )
186
+ return await self.execute(command)
187
+
188
+ async def execute(self, command: BetaMemoryTool20250818Command) -> BetaFunctionToolResultType:
189
+ """Execute a memory command and return the result.
190
+
191
+ This method dispatches to the appropriate handler method based on the
192
+ command type (view, create, str_replace, insert, delete, rename).
193
+
194
+ You typically don't need to override this method.
195
+ """
196
+ if command.command == "view":
197
+ return await self.view(command)
198
+ elif command.command == "create":
199
+ return await self.create(command)
200
+ elif command.command == "str_replace":
201
+ return await self.str_replace(command)
202
+ elif command.command == "insert":
203
+ return await self.insert(command)
204
+ elif command.command == "delete":
205
+ return await self.delete(command)
206
+ elif command.command == "rename":
207
+ return await self.rename(command)
208
+ elif TYPE_CHECKING: # type: ignore[unreachable]
209
+ assert_never(command)
210
+ else:
211
+ raise NotImplementedError(f"Unknown command: {command.command}")
212
+
213
+ @abstractmethod
214
+ async def view(self, command: BetaMemoryTool20250818ViewCommand) -> BetaFunctionToolResultType:
215
+ """View the contents of a memory path."""
216
+ pass
217
+
218
+ @abstractmethod
219
+ async def create(self, command: BetaMemoryTool20250818CreateCommand) -> BetaFunctionToolResultType:
220
+ """Create a new memory file with the specified content."""
221
+ pass
222
+
223
+ @abstractmethod
224
+ async def str_replace(self, command: BetaMemoryTool20250818StrReplaceCommand) -> BetaFunctionToolResultType:
225
+ """Replace text in a memory file."""
226
+ pass
227
+
228
+ @abstractmethod
229
+ async def insert(self, command: BetaMemoryTool20250818InsertCommand) -> BetaFunctionToolResultType:
230
+ """Insert text at a specific line number in a memory file."""
231
+ pass
232
+
233
+ @abstractmethod
234
+ async def delete(self, command: BetaMemoryTool20250818DeleteCommand) -> BetaFunctionToolResultType:
235
+ """Delete a memory file or directory."""
236
+ pass
237
+
238
+ @abstractmethod
239
+ async def rename(self, command: BetaMemoryTool20250818RenameCommand) -> BetaFunctionToolResultType:
240
+ """Rename or move a memory file or directory."""
241
+ pass
242
+
243
+ async def clear_all_memory(self) -> BetaFunctionToolResultType:
244
+ """Clear all memory data."""
245
+ raise NotImplementedError("clear_all_memory not implemented")
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import logging
4
+ from abc import ABC, abstractmethod
4
5
  from typing import Any, Union, Generic, TypeVar, Callable, Iterable, Coroutine, cast, overload
5
6
  from inspect import iscoroutinefunction
6
7
  from typing_extensions import TypeAlias, override
@@ -13,6 +14,7 @@ from ... import _compat
13
14
  from ..._utils import is_dict
14
15
  from ..._compat import cached_property
15
16
  from ..._models import TypeAdapter
17
+ from ...types.beta import BetaToolUnionParam
16
18
  from ..._utils._utils import CallableT
17
19
  from ...types.tool_param import ToolParam, InputSchema
18
20
  from ...types.beta.beta_tool_result_block_param import Content as BetaContent
@@ -28,6 +30,30 @@ AsyncFunction = Callable[..., Coroutine[Any, Any, BetaFunctionToolResultType]]
28
30
  AsyncFunctionT = TypeVar("AsyncFunctionT", bound=AsyncFunction)
29
31
 
30
32
 
33
+ class BetaBuiltinFunctionTool(ABC):
34
+ @abstractmethod
35
+ def to_dict(self) -> BetaToolUnionParam: ...
36
+
37
+ @abstractmethod
38
+ def call(self, input: object) -> BetaFunctionToolResultType: ...
39
+
40
+ @property
41
+ def name(self) -> str:
42
+ return self.to_dict()["name"]
43
+
44
+
45
+ class BetaAsyncBuiltinFunctionTool(ABC):
46
+ @abstractmethod
47
+ def to_dict(self) -> BetaToolUnionParam: ...
48
+
49
+ @abstractmethod
50
+ async def call(self, input: object) -> BetaFunctionToolResultType: ...
51
+
52
+ @property
53
+ def name(self) -> str:
54
+ return self.to_dict()["name"]
55
+
56
+
31
57
  class BaseFunctionTool(Generic[CallableT]):
32
58
  func: CallableT
33
59
  """The function this tool is wrapping"""
@@ -287,3 +313,7 @@ def beta_async_tool(
287
313
  )
288
314
 
289
315
  return decorator
316
+
317
+
318
+ BetaRunnableTool = Union[BetaFunctionTool[Any], BetaBuiltinFunctionTool]
319
+ BetaAsyncRunnableTool = Union[BetaAsyncFunctionTool[Any], BetaAsyncBuiltinFunctionTool]
@@ -22,7 +22,14 @@ import httpx
22
22
  from ..._types import Body, Query, Headers, NotGiven
23
23
  from ..._utils import consume_sync_iterator, consume_async_iterator
24
24
  from ...types.beta import BetaMessage, BetaContentBlock, BetaMessageParam
25
- from ._beta_functions import BetaFunctionTool, BetaAsyncFunctionTool
25
+ from ._beta_functions import (
26
+ BetaFunctionTool,
27
+ BetaRunnableTool,
28
+ BetaAsyncFunctionTool,
29
+ BetaAsyncRunnableTool,
30
+ BetaBuiltinFunctionTool,
31
+ BetaAsyncBuiltinFunctionTool,
32
+ )
26
33
  from ..streaming._beta_messages import BetaMessageStream, BetaAsyncMessageStream
27
34
  from ...types.beta.message_create_params import MessageCreateParamsBase
28
35
  from ...types.beta.beta_tool_result_block_param import BetaToolResultBlockParam
@@ -31,7 +38,12 @@ if TYPE_CHECKING:
31
38
  from ..._client import Anthropic, AsyncAnthropic
32
39
 
33
40
 
34
- AnyFunctionToolT = TypeVar("AnyFunctionToolT", bound=Union[BetaFunctionTool[Any], BetaAsyncFunctionTool[Any]])
41
+ AnyFunctionToolT = TypeVar(
42
+ "AnyFunctionToolT",
43
+ bound=Union[
44
+ BetaFunctionTool[Any], BetaAsyncFunctionTool[Any], BetaBuiltinFunctionTool, BetaAsyncBuiltinFunctionTool
45
+ ],
46
+ )
35
47
  RunnerItemT = TypeVar("RunnerItemT")
36
48
 
37
49
  log = logging.getLogger(__name__)
@@ -97,13 +109,13 @@ class BaseToolRunner(Generic[AnyFunctionToolT]):
97
109
  return False
98
110
 
99
111
 
100
- class BaseSyncToolRunner(BaseToolRunner[BetaFunctionTool[Any]], Generic[RunnerItemT], ABC):
112
+ class BaseSyncToolRunner(BaseToolRunner[BetaRunnableTool], Generic[RunnerItemT], ABC):
101
113
  def __init__(
102
114
  self,
103
115
  *,
104
116
  params: MessageCreateParamsBase,
105
117
  options: RequestOptions,
106
- tools: Iterable[BetaFunctionTool[Any]],
118
+ tools: Iterable[BetaRunnableTool],
107
119
  client: Anthropic,
108
120
  max_iterations: int | None = None,
109
121
  ) -> None:
@@ -250,13 +262,13 @@ class BetaStreamingToolRunner(BaseSyncToolRunner[BetaMessageStream]):
250
262
  message = stream.get_final_message()
251
263
 
252
264
 
253
- class BaseAsyncToolRunner(BaseToolRunner[BetaAsyncFunctionTool[Any]], Generic[RunnerItemT], ABC):
265
+ class BaseAsyncToolRunner(BaseToolRunner[BetaAsyncRunnableTool], Generic[RunnerItemT], ABC):
254
266
  def __init__(
255
267
  self,
256
268
  *,
257
269
  params: MessageCreateParamsBase,
258
270
  options: RequestOptions,
259
- tools: Iterable[BetaAsyncFunctionTool[Any]],
271
+ tools: Iterable[BetaAsyncRunnableTool],
260
272
  client: AsyncAnthropic,
261
273
  max_iterations: int | None = None,
262
274
  ) -> None: