Skip to content

Commit b690887

Browse files
authored
fix(OpenAI): add streaming Ratelimit event (#761)
* fix(OpenAI): add no-op rate limit stream class * test(OpenAI): add no-op rate limit stream class
1 parent 34fda39 commit b690887

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

src/Responses/Responses/CreateStreamedResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use OpenAI\Responses\Responses\Streaming\OutputTextAnnotationAdded;
2727
use OpenAI\Responses\Responses\Streaming\OutputTextDelta;
2828
use OpenAI\Responses\Responses\Streaming\OutputTextDone;
29+
use OpenAI\Responses\Responses\Streaming\RateLimits;
2930
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryPart;
3031
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryTextDelta;
3132
use OpenAI\Responses\Responses\Streaming\ReasoningSummaryTextDone;
@@ -53,7 +54,7 @@ final class CreateStreamedResponse implements ResponseContract
5354

5455
private function __construct(
5556
public readonly string $event,
56-
public readonly Response|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|Error $response,
57+
public readonly Response|OutputItem|ContentPart|OutputTextDelta|OutputTextAnnotationAdded|OutputTextDone|RefusalDelta|RefusalDone|FunctionCallArgumentsDelta|FunctionCallArgumentsDone|FileSearchCall|WebSearchCall|CodeInterpreterCall|CodeInterpreterCodeDelta|CodeInterpreterCodeDone|ReasoningSummaryPart|ReasoningSummaryTextDelta|ReasoningSummaryTextDone|ReasoningTextDelta|ReasoningTextDone|McpListTools|McpListToolsInProgress|McpCall|McpCallArgumentsDelta|McpCallArgumentsDone|ImageGenerationPart|ImageGenerationPartialImage|RateLimits|Error $response,
5758
) {}
5859

5960
/**
@@ -115,6 +116,7 @@ public static function from(array $attributes): self
115116
'response.image_generation_call.generating',
116117
'response.image_generation_call.in_progress' => ImageGenerationPart::from($attributes, $meta), // @phpstan-ignore-line
117118
'response.image_generation_call.partial_image' => ImageGenerationPartialImage::from($attributes, $meta), // @phpstan-ignore-line
119+
'response.rate_limits.updated' => RateLimits::from($attributes, $meta), // @phpstan-ignore-line
118120
'error' => Error::from($attributes, $meta), // @phpstan-ignore-line
119121
default => throw new UnknownEventException('Unknown Responses streaming event: '.$event),
120122
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Responses\Responses\Streaming;
6+
7+
use OpenAI\Contracts\ResponseContract;
8+
use OpenAI\Contracts\ResponseHasMetaInformationContract;
9+
use OpenAI\Responses\Concerns\ArrayAccessible;
10+
use OpenAI\Responses\Concerns\HasMetaInformation;
11+
use OpenAI\Responses\Meta\MetaInformation;
12+
use OpenAI\Testing\Responses\Concerns\Fakeable;
13+
14+
/**
15+
* @phpstan-type RateLimitsType array{type: string}
16+
*
17+
* @implements ResponseContract<RateLimitsType>
18+
*/
19+
final class RateLimits implements ResponseContract, ResponseHasMetaInformationContract
20+
{
21+
/**
22+
* @use ArrayAccessible<RateLimitsType>
23+
*/
24+
use ArrayAccessible;
25+
26+
use Fakeable;
27+
use HasMetaInformation;
28+
29+
private function __construct(
30+
private readonly MetaInformation $meta,
31+
) {}
32+
33+
/**
34+
* @param RateLimitsType $attributes
35+
*/
36+
public static function from(array $attributes, MetaInformation $meta): self
37+
{
38+
return new self(
39+
meta: $meta,
40+
);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function toArray(): array
47+
{
48+
return [
49+
'type' => 'response.rate_limits.updated',
50+
];
51+
}
52+
}

tests/Fixtures/Responses.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,3 +944,8 @@ function responseReasoningTextDoneEvent()
944944
{
945945
return fopen(__DIR__.'/Streams/ResponseReasoningTextDone.txt', 'r');
946946
}
947+
948+
function responseRateLimitsUpdatedEvent()
949+
{
950+
return fopen(__DIR__.'/Streams/ResponseRateLimitsUpdated.txt', 'r');
951+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data: {"type":"response.rate_limits.updated"}

tests/Responses/Responses/CreateStreamedResponse.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use OpenAI\Responses\Responses\CreateResponse;
44
use OpenAI\Responses\Responses\CreateStreamedResponse;
5+
use OpenAI\Responses\Responses\Streaming\RateLimits;
56
use OpenAI\Responses\Responses\Streaming\ReasoningTextDelta;
67
use OpenAI\Responses\Responses\Streaming\ReasoningTextDone;
78
use OpenAI\Responses\Responses\Streaming\Response;
@@ -55,3 +56,15 @@
5556
->response->contentIndex->toBe(0)
5657
->response->sequenceNumber->toBe(10);
5758
});
59+
60+
test('rate limits updated event', function () {
61+
$response = CreateStreamedResponse::fake(responseRateLimitsUpdatedEvent());
62+
63+
expect($response->getIterator()->current())
64+
->toBeInstanceOf(CreateStreamedResponse::class)
65+
->event->toBe('response.rate_limits.updated')
66+
->response->toBeInstanceOf(RateLimits::class)
67+
->response->toArray()->toBe([
68+
'type' => 'response.rate_limits.updated',
69+
]);
70+
});

0 commit comments

Comments
 (0)