Skip to content

Commit fec764f

Browse files
committed
Add revised_prompt property to Image/CreateResponseData
1 parent 18d84cf commit fec764f

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

src/Resources/Images.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function create(array $parameters): CreateResponse
2626
{
2727
$payload = Payload::create('images/generations', $parameters);
2828

29-
/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}> $response */
29+
/** @var Response<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}> $response */
3030
$response = $this->transporter->requestObject($payload);
3131

3232
return CreateResponse::from($response->data(), $response->meta());

src/Responses/Images/CreateResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use OpenAI\Testing\Responses\Concerns\Fakeable;
1313

1414
/**
15-
* @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}>
15+
* @implements ResponseContract<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
1616
*/
1717
final class CreateResponse implements ResponseContract, ResponseHasMetaInformationContract
1818
{
1919
/**
20-
* @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string}>}>
20+
* @use ArrayAccessible<array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>}>
2121
*/
2222
use ArrayAccessible;
2323

@@ -37,7 +37,7 @@ private function __construct(
3737
/**
3838
* Acts as static factory, and returns a new Response instance.
3939
*
40-
* @param array{created: int, data: array<int, array{url?: string, b64_json?: string}>} $attributes
40+
* @param array{created: int, data: array<int, array{url?: string, b64_json?: string, revised_prompt?: string}>} $attributes
4141
*/
4242
public static function from(array $attributes, MetaInformation $meta): self
4343
{

src/Responses/Images/CreateResponseData.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,33 @@
88
use OpenAI\Responses\Concerns\ArrayAccessible;
99

1010
/**
11-
* @implements ResponseContract<array{url: string}|array{b64_json: string}>
11+
* @implements ResponseContract<array{url: string, revised_prompt?: string}|array{b64_json: string, revised_prompt?: string}>
1212
*/
1313
final class CreateResponseData implements ResponseContract
1414
{
1515
/**
16-
* @use ArrayAccessible<array{url: string}|array{b64_json: string}>
16+
* @use ArrayAccessible<array{url: string, revised_prompt?: string}|array{b64_json: string, revised_prompt?: string}>
1717
*/
1818
use ArrayAccessible;
1919

2020
private function __construct(
21-
public readonly string $url = '',
22-
public readonly string $b64_json = '',
21+
public readonly string $url,
22+
public readonly string $b64_json,
23+
public readonly ?string $revisedPrompt,
2324
) {
2425
}
2526

2627
/**
2728
* Acts as static factory, and returns a new Response instance.
2829
*
29-
* @param array{url?: string, b64_json?: string} $attributes
30+
* @param array{url?: string, b64_json?: string, revised_prompt?: string} $attributes
3031
*/
3132
public static function from(array $attributes): self
3233
{
3334
return new self(
3435
$attributes['url'] ?? '',
3536
$attributes['b64_json'] ?? '',
37+
$attributes['revised_prompt'] ?? null,
3638
);
3739
}
3840

@@ -41,8 +43,14 @@ public static function from(array $attributes): self
4143
*/
4244
public function toArray(): array
4345
{
44-
return $this->url !== '' && $this->url !== '0' ?
46+
$data = $this->url !== '' && $this->url !== '0' ?
4547
['url' => $this->url] :
4648
['b64_json' => $this->b64_json];
49+
50+
if ($this->revisedPrompt !== null) {
51+
$data['revised_prompt'] = $this->revisedPrompt;
52+
}
53+
54+
return $data;
4755
}
4856
}

tests/Fixtures/Image.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ function imageCreateWithUrl(): array
1515
];
1616
}
1717

18+
/**
19+
* @return array<string, mixed>
20+
*/
21+
function imageCreateWithUrlDallE3(): array
22+
{
23+
return [
24+
'created' => 1664136088,
25+
'data' => [
26+
[
27+
'url' => 'https://openai.com/image.png',
28+
'revised_prompt' => 'This is a revised prompt.',
29+
],
30+
],
31+
];
32+
}
33+
1834
/**
1935
* @return array<string, mixed>
2036
*/

tests/Responses/Images/CreateResponseData.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
->toBe(imageCreateWithUrl()['data'][0]);
1818
});
1919

20+
test('from with url from dall-e 3', function () {
21+
$response = CreateResponseData::from(imageCreateWithUrlDallE3()['data'][0]);
22+
23+
expect($response)
24+
->url->toBe('https://openai.com/image.png')
25+
->revisedPrompt->toBe('This is a revised prompt.')
26+
->b64_json->toBeEmpty();
27+
});
28+
29+
test('to array with url from dall-e 3', function () {
30+
$result = CreateResponseData::from(imageCreateWithUrlDallE3()['data'][0]);
31+
32+
expect($result->toArray())
33+
->toBe(imageCreateWithUrlDallE3()['data'][0]);
34+
});
35+
2036
test('from with b64_json', function () {
2137
$response = CreateResponseData::from(imageCreateWithB46Json()['data'][0]);
2238

0 commit comments

Comments
 (0)