Skip to content

Commit 57afba6

Browse files
chore(doc): small improvement to pagination example
1 parent 6bb945b commit 57afba6

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ $message = $client->messages->create(
4343
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
4444
model: "claude-sonnet-4-20250514",
4545
);
46+
4647
var_dump($message->content);
4748
```
4849

@@ -53,6 +54,60 @@ and named parameters to initialize value objects.
5354

5455
However, builders are also provided `(new Base64ImageSource)->withData("U3RhaW5sZXNzIHJvY2tz")`.
5556

57+
### Streaming
58+
59+
We provide support for streaming responses using Server-Sent Events (SSE).
60+
61+
```php
62+
<?php
63+
64+
use Anthropic\Client;
65+
use Anthropic\Messages\MessageParam;
66+
67+
$client = new Client(
68+
apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
69+
);
70+
71+
$stream = $client->messages->createStream(
72+
maxTokens: 1024,
73+
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],
74+
model: "claude-sonnet-4-20250514",
75+
);
76+
77+
foreach ($stream as $message) {
78+
var_dump($message);
79+
}
80+
```
81+
82+
### Pagination
83+
84+
List methods in the Anthropic API are paginated.
85+
86+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
87+
88+
```php
89+
<?php
90+
91+
use Anthropic\Client;
92+
93+
$client = new Client(
94+
apiKey: getenv("ANTHROPIC_API_KEY") ?: "my-anthropic-api-key"
95+
);
96+
97+
$page = $client->beta->messages->batches->list();
98+
99+
var_dump($page);
100+
101+
// fetch items from the current page
102+
foreach ($page->getItems() as $item) {
103+
var_dump($item->id);
104+
}
105+
// make additional network requests to fetch items from all pages, including and after the current page
106+
foreach ($page->pagingEachItem() as $item) {
107+
var_dump($item->id);
108+
}
109+
```
110+
56111
### Handling errors
57112

58113
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Anthropic\Errors\APIError` will be thrown:
@@ -115,6 +170,7 @@ use Anthropic\Messages\MessageParam;
115170
$client = new Client(maxRetries: 0);
116171

117172
// Or, configure per-request:
173+
118174
$result = $client->messages->create(
119175
maxTokens: 1024,
120176
messages: [MessageParam::with(role: "user", content: "Hello, Claude")],

src/Core/Concerns/SdkPage.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ trait SdkPage
3333
/**
3434
* @return list<Item>
3535
*/
36-
abstract public function getPaginatedItems(): array;
36+
abstract public function getItems(): array;
3737

3838
public function hasNextPage(): bool
3939
{
40-
$items = $this->getPaginatedItems();
40+
$items = $this->getItems();
4141
if (empty($items)) {
4242
return false;
4343
}
@@ -66,7 +66,7 @@ public function getNextPage(): static
6666
[$req, $opts] = $next;
6767

6868
// @phpstan-ignore-next-line
69-
return $this->client->request(...$req, convert: $this->convert, page: $this, options: $opts);
69+
return $this->client->request(...$req, convert: $this->convert, page: $this::class, options: $opts);
7070
}
7171

7272
/**
@@ -94,7 +94,7 @@ public function getIterator(): \Generator
9494
public function pagingEachItem(): \Generator
9595
{
9696
foreach ($this as $page) {
97-
foreach ($page->getPaginatedItems() as $item) {
97+
foreach ($page->getItems() as $item) {
9898
yield $item;
9999
}
100100
}

src/Core/Contracts/BasePage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function hasNextPage(): bool;
4545
/**
4646
* @return list<Item>
4747
*/
48-
public function getPaginatedItems(): array;
48+
public function getItems(): array;
4949

5050
/**
5151
* @return static<Item>

src/Core/Page.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(
7171
}
7272

7373
/** @return list<TItem> */
74-
public function getPaginatedItems(): array
74+
public function getItems(): array
7575
{
7676
// @phpstan-ignore-next-line
7777
return $this->offsetGet('data') ?? [];
@@ -96,8 +96,12 @@ public function nextRequest(): ?array
9696
return null;
9797
}
9898

99-
$nextRequest = $this->request;
99+
$nextRequest = array_merge_recursive(
100+
$this->request,
101+
['query' => ['after_id' => $next]]
102+
);
100103

104+
// @phpstan-ignore-next-line
101105
return [$nextRequest, $this->options];
102106
}
103107
}

0 commit comments

Comments
 (0)