Skip to content

[Platform][Anthropic] Allow beta feature flags to be passed into platform invocations #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/platform/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ CHANGELOG
* Add response promises for async operations
* Add InMemoryPlatform and InMemoryRawResult for testing Platform without external Providers calls
* Add tool calling support for Ollama platform
* Allow beta feature flags to be passed into Anthropic model options


19 changes: 15 additions & 4 deletions src/platform/src/Bridge/Anthropic/ModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,26 @@ public function supports(Model $model): bool

public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
{
$headers = [
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
];

if (isset($options['tools'])) {
$options['tool_choice'] = ['type' => 'auto'];
}

if (
isset($options['beta_features'])
&& \is_array($options['beta_features'])
&& !empty($options['beta_features'])
) {
$headers['anthropic-beta'] = implode(',', $options['beta_features']);
unset($options['beta_features']);
}

return new RawHttpResult($this->httpClient->request('POST', 'https://api.anthropic.com/v1/messages', [
'headers' => [
'x-api-key' => $this->apiKey,
'anthropic-version' => $this->version,
],
'headers' => $headers,
'json' => array_merge($options, $payload),
]));
}
Expand Down
114 changes: 114 additions & 0 deletions src/platform/tests/Bridge/Anthropic/ModelClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\Anthropic\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\Anthropic\Claude;
use Symfony\AI\Platform\Bridge\Anthropic\ModelClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;

#[CoversClass(ModelClient::class)]
class ModelClientTest extends TestCase
{
private MockHttpClient $httpClient;
private ModelClient $modelClient;
private Claude $model;

protected function setUp(): void
{
$this->model = new Claude();
}

public function testAnthropicBetaHeaderIsSetWithSingleBetaFeature()
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$this->assertEquals('POST', $method);
$this->assertEquals('https://api.anthropic.com/v1/messages', $url);

$headers = $this->parseHeaders($options['headers']);

$this->assertArrayHasKey('anthropic-beta', $headers);
$this->assertEquals('feature-1', $headers['anthropic-beta']);

return new JsonMockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key');

$options = ['beta_features' => ['feature-1']];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsSetWithMultipleBetaFeatures()
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayHasKey('anthropic-beta', $headers);
$this->assertEquals('feature-1,feature-2,feature-3', $headers['anthropic-beta']);

return new JsonMockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['beta_features' => ['feature-1', 'feature-2', 'feature-3']];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsEmpty()
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayNotHasKey('anthropic-beta', $headers);

return new JsonMockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['beta_features' => []];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsNotProvided()
{
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
$headers = $this->parseHeaders($options['headers']);

$this->assertArrayNotHasKey('anthropic-beta', $headers);

return new JsonMockResponse('{"success": true}');
});

$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');

$options = ['some_other_option' => 'value'];
$this->modelClient->request($this->model, ['message' => 'test'], $options);
}

private function parseHeaders(array $headers): array
{
$parsed = [];
foreach ($headers as $header) {
if (str_contains($header, ':')) {
[$key, $value] = explode(':', $header, 2);
$parsed[trim($key)] = trim($value);
}
}

return $parsed;
}
}