8
8
from datetime import datetime , timezone
9
9
from typing import Any , Literal , Union , cast , overload
10
10
11
- from anthropic .types .beta import (
12
- BetaCitationsDelta ,
13
- BetaCodeExecutionToolResultBlock ,
14
- BetaCodeExecutionToolResultBlockParam ,
15
- BetaInputJSONDelta ,
16
- BetaServerToolUseBlockParam ,
17
- BetaWebSearchToolResultBlockParam ,
18
- )
19
11
from typing_extensions import assert_never
20
12
21
13
from pydantic_ai .builtin_tools import CodeExecutionTool , WebSearchTool
47
39
from ..providers import Provider , infer_provider
48
40
from ..settings import ModelSettings
49
41
from ..tools import ToolDefinition
50
- from . import (
51
- Model ,
52
- ModelRequestParameters ,
53
- StreamedResponse ,
54
- check_allow_model_requests ,
55
- download_item ,
56
- get_user_agent ,
57
- )
42
+ from . import Model , ModelRequestParameters , StreamedResponse , check_allow_model_requests , download_item , get_user_agent
58
43
59
44
try :
60
45
from anthropic import NOT_GIVEN , APIStatusError , AsyncAnthropic , AsyncStream
61
46
from anthropic .types .beta import (
62
47
BetaBase64PDFBlockParam ,
63
48
BetaBase64PDFSourceParam ,
49
+ BetaCitationsDelta ,
64
50
BetaCodeExecutionTool20250522Param ,
51
+ BetaCodeExecutionToolResultBlock ,
52
+ BetaCodeExecutionToolResultBlockParam ,
65
53
BetaContentBlock ,
66
54
BetaContentBlockParam ,
67
55
BetaImageBlockParam ,
56
+ BetaInputJSONDelta ,
68
57
BetaMessage ,
69
58
BetaMessageParam ,
70
59
BetaMetadataParam ,
78
67
BetaRawMessageStreamEvent ,
79
68
BetaRedactedThinkingBlock ,
80
69
BetaServerToolUseBlock ,
70
+ BetaServerToolUseBlockParam ,
81
71
BetaSignatureDelta ,
82
72
BetaTextBlock ,
83
73
BetaTextBlockParam ,
94
84
BetaToolUseBlockParam ,
95
85
BetaWebSearchTool20250305Param ,
96
86
BetaWebSearchToolResultBlock ,
87
+ BetaWebSearchToolResultBlockParam ,
97
88
)
98
89
from anthropic .types .beta .beta_web_search_tool_20250305_param import UserLocation
99
90
from anthropic .types .model_param import ModelParam
@@ -246,7 +237,9 @@ async def _messages_create(
246
237
) -> BetaMessage | AsyncStream [BetaRawMessageStreamEvent ]:
247
238
# standalone function to make it easier to override
248
239
tools = self ._get_tools (model_request_parameters )
249
- tools += self ._get_builtin_tools (model_request_parameters )
240
+ builtin_tools , tool_headers = self ._get_builtin_tools (model_request_parameters )
241
+ tools += builtin_tools
242
+
250
243
tool_choice : BetaToolChoiceParam | None
251
244
252
245
if not tools :
@@ -264,8 +257,10 @@ async def _messages_create(
264
257
265
258
try :
266
259
extra_headers = model_settings .get ('extra_headers' , {})
260
+ for k , v in tool_headers .items ():
261
+ extra_headers .setdefault (k , v )
267
262
extra_headers .setdefault ('User-Agent' , get_user_agent ())
268
- extra_headers . setdefault ( 'anthropic-beta' , 'code-execution-2025-05-22' )
263
+
269
264
return await self .client .beta .messages .create (
270
265
max_tokens = model_settings .get ('max_tokens' , 4096 ),
271
266
system = system_prompt or NOT_GIVEN ,
@@ -352,8 +347,11 @@ async def _process_streamed_response(
352
347
def _get_tools (self , model_request_parameters : ModelRequestParameters ) -> list [BetaToolParam ]:
353
348
return [self ._map_tool_definition (r ) for r in model_request_parameters .tool_defs .values ()]
354
349
355
- def _get_builtin_tools (self , model_request_parameters : ModelRequestParameters ) -> list [BetaToolUnionParam ]:
350
+ def _get_builtin_tools (
351
+ self , model_request_parameters : ModelRequestParameters
352
+ ) -> tuple [list [BetaToolUnionParam ], dict [str , str ]]:
356
353
tools : list [BetaToolUnionParam ] = []
354
+ extra_headers : dict [str , str ] = {}
357
355
for tool in model_request_parameters .builtin_tools :
358
356
if isinstance (tool , WebSearchTool ):
359
357
user_location = UserLocation (type = 'approximate' , ** tool .user_location ) if tool .user_location else None
@@ -367,12 +365,13 @@ def _get_builtin_tools(self, model_request_parameters: ModelRequestParameters) -
367
365
)
368
366
)
369
367
elif isinstance (tool , CodeExecutionTool ): # pragma: no branch
368
+ extra_headers ['anthropic-beta' ] = 'code-execution-2025-05-22'
370
369
tools .append (BetaCodeExecutionTool20250522Param (name = 'code_execution' , type = 'code_execution_20250522' ))
371
370
else : # pragma: no cover
372
371
raise UserError (
373
372
f'`{ tool .__class__ .__name__ } ` is not supported by `AnthropicModel`. If it should be, please file an issue.'
374
373
)
375
- return tools
374
+ return tools , extra_headers
376
375
377
376
async def _map_message (self , messages : list [ModelMessage ]) -> tuple [str , list [BetaMessageParam ]]: # noqa: C901
378
377
"""Just maps a `pydantic_ai.Message` to a `anthropic.types.MessageParam`."""
0 commit comments