Skip to content

Commit fd1ac9a

Browse files
authored
feat(openai): Response meta while streaming (#421)
1 parent f510133 commit fd1ac9a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Providers/OpenAI/Handlers/Stream.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Prism\Prism\Text\Request;
2626
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
2727
use Prism\Prism\ValueObjects\Messages\ToolResultMessage;
28+
use Prism\Prism\ValueObjects\Meta;
2829
use Prism\Prism\ValueObjects\ToolCall;
2930
use Psr\Http\Message\StreamInterface;
3031
use Throwable;
@@ -61,6 +62,20 @@ protected function processStream(Response $response, Request $request, int $dept
6162
continue;
6263
}
6364

65+
if ($data['type'] === 'response.created') {
66+
yield new Chunk(
67+
text: '',
68+
finishReason: null,
69+
meta: new Meta(
70+
id: $data['response']['id'] ?? null,
71+
model: $data['response']['model'] ?? null,
72+
),
73+
chunkType: ChunkType::Meta,
74+
);
75+
76+
continue;
77+
}
78+
6479
if ($this->hasReasoningItems($data)) {
6580
$reasoningItems = $this->extractReasoningItems($data, $reasoningItems);
6681

tests/Providers/OpenAI/StreamTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,25 @@
2727
$text = '';
2828
$chunks = [];
2929

30+
$responseId = null;
31+
$model = null;
32+
3033
foreach ($response as $chunk) {
34+
if ($chunk->chunkType === ChunkType::Meta) {
35+
$responseId = $chunk->meta?->id;
36+
$model = $chunk->meta?->model;
37+
}
38+
3139
$chunks[] = $chunk;
3240
$text .= $chunk->text;
3341
}
3442

3543
expect($chunks)->not->toBeEmpty();
3644
expect($text)->not->toBeEmpty();
45+
expect($responseId)
46+
->not->toBeNull()
47+
->toStartWith('resp_');
48+
expect($model)->not->toBeNull();
3749

3850
// Verify the HTTP request
3951
Http::assertSent(function (Request $request): bool {

0 commit comments

Comments
 (0)