88from datetime import datetime , timezone
99from typing import Any , Literal , Union , cast , overload
1010
11- from anthropic .types .beta import (
12- BetaCitationsDelta ,
13- BetaCodeExecutionToolResultBlock ,
14- BetaCodeExecutionToolResultBlockParam ,
15- BetaInputJSONDelta ,
16- BetaServerToolUseBlockParam ,
17- BetaWebSearchToolResultBlockParam ,
18- )
1911from typing_extensions import assert_never
2012
2113from pydantic_ai .builtin_tools import CodeExecutionTool , WebSearchTool
4739from ..providers import Provider , infer_provider
4840from ..settings import ModelSettings
4941from ..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
5843
5944try :
6045 from anthropic import NOT_GIVEN , APIStatusError , AsyncAnthropic , AsyncStream
6146 from anthropic .types .beta import (
6247 BetaBase64PDFBlockParam ,
6348 BetaBase64PDFSourceParam ,
49+ BetaCitationsDelta ,
6450 BetaCodeExecutionTool20250522Param ,
51+ BetaCodeExecutionToolResultBlock ,
52+ BetaCodeExecutionToolResultBlockParam ,
6553 BetaContentBlock ,
6654 BetaContentBlockParam ,
6755 BetaImageBlockParam ,
56+ BetaInputJSONDelta ,
6857 BetaMessage ,
6958 BetaMessageParam ,
7059 BetaMetadataParam ,
7867 BetaRawMessageStreamEvent ,
7968 BetaRedactedThinkingBlock ,
8069 BetaServerToolUseBlock ,
70+ BetaServerToolUseBlockParam ,
8171 BetaSignatureDelta ,
8272 BetaTextBlock ,
8373 BetaTextBlockParam ,
9484 BetaToolUseBlockParam ,
9585 BetaWebSearchTool20250305Param ,
9686 BetaWebSearchToolResultBlock ,
87+ BetaWebSearchToolResultBlockParam ,
9788 )
9889 from anthropic .types .beta .beta_web_search_tool_20250305_param import UserLocation
9990 from anthropic .types .model_param import ModelParam
@@ -246,7 +237,9 @@ async def _messages_create(
246237 ) -> BetaMessage | AsyncStream [BetaRawMessageStreamEvent ]:
247238 # standalone function to make it easier to override
248239 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+
250243 tool_choice : BetaToolChoiceParam | None
251244
252245 if not tools :
@@ -264,8 +257,10 @@ async def _messages_create(
264257
265258 try :
266259 extra_headers = model_settings .get ('extra_headers' , {})
260+ for k , v in tool_headers .items ():
261+ extra_headers .setdefault (k , v )
267262 extra_headers .setdefault ('User-Agent' , get_user_agent ())
268- extra_headers . setdefault ( 'anthropic-beta' , 'code-execution-2025-05-22' )
263+
269264 return await self .client .beta .messages .create (
270265 max_tokens = model_settings .get ('max_tokens' , 4096 ),
271266 system = system_prompt or NOT_GIVEN ,
@@ -352,8 +347,11 @@ async def _process_streamed_response(
352347 def _get_tools (self , model_request_parameters : ModelRequestParameters ) -> list [BetaToolParam ]:
353348 return [self ._map_tool_definition (r ) for r in model_request_parameters .tool_defs .values ()]
354349
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 ]]:
356353 tools : list [BetaToolUnionParam ] = []
354+ extra_headers : dict [str , str ] = {}
357355 for tool in model_request_parameters .builtin_tools :
358356 if isinstance (tool , WebSearchTool ):
359357 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) -
367365 )
368366 )
369367 elif isinstance (tool , CodeExecutionTool ): # pragma: no branch
368+ extra_headers ['anthropic-beta' ] = 'code-execution-2025-05-22'
370369 tools .append (BetaCodeExecutionTool20250522Param (name = 'code_execution' , type = 'code_execution_20250522' ))
371370 else : # pragma: no cover
372371 raise UserError (
373372 f'`{ tool .__class__ .__name__ } ` is not supported by `AnthropicModel`. If it should be, please file an issue.'
374373 )
375- return tools
374+ return tools , extra_headers
376375
377376 async def _map_message (self , messages : list [ModelMessage ]) -> tuple [str , list [BetaMessageParam ]]: # noqa: C901
378377 """Just maps a `pydantic_ai.Message` to a `anthropic.types.MessageParam`."""
0 commit comments