Skip to content

Commit 642c990

Browse files
SongYippeessiq
andauthored
Add support for extra headers in Claude SDK API (#2445)
Third-party model providers offering Claude services require additional dynamic header information. Co-authored-by: ssiq <liweiwuhome@hotmail.com>
1 parent 4955714 commit 642c990

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

opencompass/models/claude_sdk_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class ClaudeSDK(BaseAPIModel):
3939
wrapping of any meta instructions.
4040
base_url (str, optional): Anthropic-compatible base URL. If omitted,
4141
use ANTHROPIC_BASE_URL from environment.
42+
extra_headers (Dict, optional): Extra default HTTP headers passed to
43+
the Anthropic SDK client.
4244
stream (bool): Whether to use streaming response. Defaults to False.
4345
retry (int): Number of retires if the API call fails. Defaults to 2.
4446
think_tag (str): Separator used between thinking content and final
@@ -59,6 +61,7 @@ def __init__(
5961
base_url: Optional[str] = None,
6062
stream: bool = False,
6163
retry: int = 2,
64+
extra_headers: Optional[Dict[str, str]] = None,
6265
think_tag: str = '</think>',
6366
claude_extra_kwargs: Optional[Dict] = None,
6467
):
@@ -83,6 +86,8 @@ def __init__(
8386
client_kwargs = {'api_key': key}
8487
if base_url:
8588
client_kwargs['base_url'] = base_url
89+
if extra_headers is not None:
90+
client_kwargs['default_headers'] = extra_headers
8691

8792
self.anthropic = Anthropic(**client_kwargs)
8893
self.model = path

tests/models/test_claude_sdk_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ def test_anthropic_base_url_env_is_used_as_is(self):
4242
api_key='test-key', base_url='https://proxy.example/v1')
4343
self.assertEqual(model.base_url, 'https://proxy.example/v1')
4444

45+
@patch.dict('os.environ', {}, clear=True)
46+
def test_extra_headers_are_passed_to_client(self):
47+
"""Test extra_headers are passed to the Anthropic SDK client."""
48+
headers = {
49+
'anthropic-beta': 'prompt-caching-2024-07-31',
50+
'x-provider-region': 'test-region',
51+
}
52+
_, anthropic_cls, _ = self.build_model(
53+
base_url='https://proxy.example/v1', extra_headers=headers)
54+
55+
anthropic_cls.assert_called_once_with(
56+
api_key='test-key',
57+
base_url='https://proxy.example/v1',
58+
default_headers=headers)
59+
4560
@patch.dict('os.environ',
4661
{
4762
'ANTHROPIC_API_KEY': 'env-key',

0 commit comments

Comments
 (0)