Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cd1ffcb

Browse files
committedOct 8, 2024·
feat(api): add message batches api
For more information see https://anthropic.com/news/message-batches-api
1 parent 59f2088 commit cd1ffcb

File tree

73 files changed

+6311
-33
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+6311
-33
lines changed
 

‎.stats.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
configured_endpoints: 3
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-286f00929e2a4d28d991e6a7e660fa801dca7ec91d8ecb2fc17654bb8173eb0d.yml
1+
configured_endpoints: 9
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-aedee7570aa925baba404fc5bd3c8c1fffe8845517e492751db9b175c5cae9da.yml

‎README.md

+115
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,50 @@ message.usage
173173
# Usage(input_tokens=25, output_tokens=13)
174174
```
175175

176+
## Message Batches
177+
178+
This SDK provides beta support for the [Message Batches API](https://docs.anthropic.com/en/docs/build-with-claude/message-batches) under the `client.beta.messages.batches` namespace.
179+
180+
181+
### Creating a batch
182+
183+
Message Batches take the exact same request params as the standard Messages API:
184+
185+
```python
186+
await client.beta.messages.batches.create(
187+
requests=[
188+
{
189+
"custom_id": "my-first-request",
190+
"params": {
191+
"model": "claude-3-5-sonnet-20240620",
192+
"max_tokens": 1024,
193+
"messages": [{"role": "user", "content": "Hello, world"}],
194+
},
195+
},
196+
{
197+
"custom_id": "my-second-request",
198+
"params": {
199+
"model": "claude-3-5-sonnet-20240620",
200+
"max_tokens": 1024,
201+
"messages": [{"role": "user", "content": "Hi again, friend"}],
202+
},
203+
},
204+
]
205+
)
206+
```
207+
208+
209+
### Getting results from a batch
210+
211+
Once a Message Batch has been processed, indicated by `.processing_status === 'ended'`, you can access the results with `.batches.results()`
212+
213+
```python
214+
result_stream = await client.beta.messages.batches.results(batch_id)
215+
async for entry in result_stream:
216+
if entry.result.type == "succeeded":
217+
print(entry.result.message.content)
218+
```
219+
176220
## Tool use
177221

178222
This SDK provides support for tool use, aka function calling. More details can be found in [the documentation](https://docs.anthropic.com/claude/docs/tool-use).
@@ -250,6 +294,77 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
250294

251295
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
252296

297+
## Pagination
298+
299+
List methods in the Anthropic API are paginated.
300+
301+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
302+
303+
```python
304+
from anthropic import Anthropic
305+
306+
client = Anthropic()
307+
308+
all_batches = []
309+
# Automatically fetches more pages as needed.
310+
for batch in client.beta.messages.batches.list(
311+
limit=20,
312+
):
313+
# Do something with batch here
314+
all_batches.append(batch)
315+
print(all_batches)
316+
```
317+
318+
Or, asynchronously:
319+
320+
```python
321+
import asyncio
322+
from anthropic import AsyncAnthropic
323+
324+
client = AsyncAnthropic()
325+
326+
327+
async def main() -> None:
328+
all_batches = []
329+
# Iterate through items across all pages, issuing requests as needed.
330+
async for batch in client.beta.messages.batches.list(
331+
limit=20,
332+
):
333+
all_batches.append(batch)
334+
print(all_batches)
335+
336+
337+
asyncio.run(main())
338+
```
339+
340+
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
341+
342+
```python
343+
first_page = await client.beta.messages.batches.list(
344+
limit=20,
345+
)
346+
if first_page.has_next_page():
347+
print(f"will fetch next page using these details: {first_page.next_page_info()}")
348+
next_page = await first_page.get_next_page()
349+
print(f"number of items we just fetched: {len(next_page.data)}")
350+
351+
# Remove `await` for non-async usage.
352+
```
353+
354+
Or just work directly with the returned data:
355+
356+
```python
357+
first_page = await client.beta.messages.batches.list(
358+
limit=20,
359+
)
360+
361+
print(f"next page cursor: {first_page.last_id}") # => "next page cursor: ..."
362+
for batch in first_page.data:
363+
print(batch.id)
364+
365+
# Remove `await` for non-async usage.
366+
```
367+
253368
## Handling errors
254369

255370
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `anthropic.APIConnectionError` is raised.

0 commit comments

Comments
 (0)
Please sign in to comment.