Skip to content

Commit 04ab137

Browse files
committed
Change response_format string literal to const
In `PlatformSubscriber`, a hardcoded `response_format` option is used, but it's overridden by many bridges and probably more to come. This changes it to a public const to avoid it becoming a PITA to replace all the occurrences if we ever have to change it
1 parent 2ddd2b3 commit 04ab137

File tree

5 files changed

+17
-12
lines changed

5 files changed

+17
-12
lines changed

src/platform/src/Bridge/Gemini/Gemini/ModelClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\AI\Platform\Model;
1616
use Symfony\AI\Platform\ModelClientInterface;
1717
use Symfony\AI\Platform\Result\RawHttpResult;
18+
use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber;
1819
use Symfony\Component\HttpClient\EventSourceHttpClient;
1920
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
2021
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -49,10 +50,10 @@ public function request(Model $model, array|string $payload, array $options = []
4950
$options['stream'] ?? false ? 'streamGenerateContent' : 'generateContent',
5051
);
5152

52-
if (isset($options['response_format']['json_schema']['schema'])) {
53+
if (isset($options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]['json_schema']['schema'])) {
5354
$options['responseMimeType'] = 'application/json';
54-
$options['responseJsonSchema'] = $options['response_format']['json_schema']['schema'];
55-
unset($options['response_format']);
55+
$options['responseJsonSchema'] = $options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]['json_schema']['schema'];
56+
unset($options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]);
5657
}
5758

5859
$generationConfig = ['generationConfig' => $options];

src/platform/src/Bridge/Ollama/OllamaClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\AI\Platform\Model;
1616
use Symfony\AI\Platform\ModelClientInterface;
1717
use Symfony\AI\Platform\Result\RawHttpResult;
18+
use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
1920

2021
/**
@@ -63,9 +64,9 @@ private function doCompletionRequest(array|string $payload, array $options = [])
6364
// Revert Ollama's default streaming behavior
6465
$options['stream'] ??= false;
6566

66-
if (\array_key_exists('response_format', $options) && \array_key_exists('json_schema', $options['response_format'])) {
67-
$options['format'] = $options['response_format']['json_schema']['schema'];
68-
unset($options['response_format']);
67+
if (\array_key_exists(PlatformSubscriber::KEY_OUTPUT_STRUCTURE, $options) && \array_key_exists('json_schema', $options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE])) {
68+
$options['format'] = $options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]['json_schema']['schema'];
69+
unset($options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]);
6970
}
7071

7172
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/api/chat', $this->hostUrl), [

src/platform/src/Bridge/OpenAi/DallE/ResultConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\AI\Platform\Result\RawResultInterface;
1818
use Symfony\AI\Platform\Result\ResultInterface;
1919
use Symfony\AI\Platform\ResultConverterInterface;
20+
use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber;
2021

2122
/**
2223
* @see https://platform.openai.com/docs/api-reference/images/create
@@ -40,7 +41,7 @@ public function convert(RawResultInterface $result, array $options = []): Result
4041

4142
$images = [];
4243
foreach ($result['data'] as $image) {
43-
if ('url' === $options['response_format']) {
44+
if ('url' === $options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]) {
4445
$images[] = new UrlImage($image['url']);
4546

4647
continue;

src/platform/src/Bridge/VertexAi/Gemini/ModelClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\AI\Platform\Model as BaseModel;
1515
use Symfony\AI\Platform\ModelClientInterface;
1616
use Symfony\AI\Platform\Result\RawHttpResult;
17+
use Symfony\AI\Platform\StructuredOutput\PlatformSubscriber;
1718
use Symfony\Component\HttpClient\EventSourceHttpClient;
1819
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -51,11 +52,11 @@ public function request(BaseModel $model, array|string $payload, array $options
5152
$options['stream'] ?? false ? 'streamGenerateContent' : 'generateContent',
5253
);
5354

54-
if (isset($options['response_format']['json_schema']['schema'])) {
55+
if (isset($options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]['json_schema']['schema'])) {
5556
$options['generationConfig']['responseMimeType'] = 'application/json';
56-
$options['generationConfig']['responseSchema'] = $options['response_format']['json_schema']['schema'];
57+
$options['generationConfig']['responseSchema'] = $options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]['json_schema']['schema'];
5758

58-
unset($options['response_format']);
59+
unset($options[PlatformSubscriber::KEY_OUTPUT_STRUCTURE]);
5960
}
6061

6162
if (isset($options['generationConfig'])) {

src/platform/src/StructuredOutput/PlatformSubscriber.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
*/
3737
final class PlatformSubscriber implements EventSubscriberInterface
3838
{
39+
public const KEY_OUTPUT_STRUCTURE = 'response_format';
3940
private string $outputStructure;
4041

4142
public function __construct(
@@ -91,7 +92,7 @@ public function processInput(InvocationEvent $event): void
9192
throw new InvalidArgumentException('Streamed responses are not supported for structured output.');
9293
}
9394

94-
$options['response_format'] = $this->responseFormatFactory->create($options['output_structure']);
95+
$options[self::KEY_OUTPUT_STRUCTURE] = $this->responseFormatFactory->create($options['output_structure']);
9596

9697
$this->outputStructure = $options['output_structure'];
9798
unset($options['output_structure']);
@@ -103,7 +104,7 @@ public function processResult(ResultEvent $event): void
103104
{
104105
$options = $event->getOptions();
105106

106-
if (!isset($options['response_format'])) {
107+
if (!isset($options[self::KEY_OUTPUT_STRUCTURE])) {
107108
return;
108109
}
109110

0 commit comments

Comments
 (0)