348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 | @dataclass(init=False)
class BedrockConverseModel(Model):
"""A model that uses the Bedrock Converse API."""
client: BedrockRuntimeClient
_model_name: BedrockModelName = field(repr=False)
_provider: Provider[BaseClient] = field(repr=False)
def __init__(
self,
model_name: BedrockModelName,
*,
provider: Literal['bedrock', 'gateway'] | Provider[BaseClient] = 'bedrock',
profile: ModelProfileSpec | None = None,
settings: ModelSettings | None = None,
):
"""Initialize a Bedrock model.
Args:
model_name: The name of the model to use.
model_name: The name of the Bedrock model to use. List of model names available
[here](https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html).
provider: The provider to use for authentication and API access. Can be either the string
'bedrock' or an instance of `Provider[BaseClient]`. If not provided, a new provider will be
created using the other parameters.
profile: The model profile to use. Defaults to a profile picked by the provider based on the model name.
settings: Model-specific settings that will be used as defaults for this model.
"""
self._model_name = model_name
if isinstance(provider, str):
provider = infer_provider('gateway/bedrock' if provider == 'gateway' else provider)
self._provider = provider
self.client = cast('BedrockRuntimeClient', provider.client)
super().__init__(settings=settings, profile=profile or provider.model_profile)
@property
def base_url(self) -> str:
return str(self.client.meta.endpoint_url)
@property
def model_name(self) -> str:
"""The model name."""
return self._model_name
@property
def system(self) -> str:
"""The model provider."""
return self._provider.name
@classmethod
def supported_builtin_tools(cls) -> frozenset[type[AbstractBuiltinTool]]:
"""The set of builtin tool types this model can handle."""
return frozenset({CodeExecutionTool})
def _get_tools(self, model_request_parameters: ModelRequestParameters) -> list[ToolTypeDef]:
return [self._map_tool_definition(r) for r in model_request_parameters.tool_defs.values()]
@staticmethod
def _map_tool_definition(f: ToolDefinition) -> ToolTypeDef:
tool_spec: ToolSpecificationTypeDef = {'name': f.name, 'inputSchema': {'json': f.parameters_json_schema}}
if f.description: # pragma: no branch
tool_spec['description'] = f.description
return {'toolSpec': tool_spec}
async def request(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> ModelResponse:
model_settings, model_request_parameters = self.prepare_request(
model_settings,
model_request_parameters,
)
settings = cast(BedrockModelSettings, model_settings or {})
response = await self._messages_create(messages, False, settings, model_request_parameters)
model_response = await self._process_response(response)
return model_response
async def count_tokens(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> usage.RequestUsage:
"""Count the number of tokens, works with limited models.
Check the actual supported models on <https://docs.aws.amazon.com/bedrock/latest/userguide/count-tokens.html>
"""
model_settings, model_request_parameters = self.prepare_request(model_settings, model_request_parameters)
settings = cast(BedrockModelSettings, model_settings or {})
system_prompt, bedrock_messages = await self._map_messages(messages, model_request_parameters, settings)
params: CountTokensRequestTypeDef = {
'modelId': remove_bedrock_geo_prefix(self.model_name),
'input': {
'converse': {
'messages': bedrock_messages,
'system': system_prompt,
},
},
}
try:
response = await anyio.to_thread.run_sync(functools.partial(self.client.count_tokens, **params))
except ClientError as e:
status_code = e.response.get('ResponseMetadata', {}).get('HTTPStatusCode')
if isinstance(status_code, int):
raise ModelHTTPError(status_code=status_code, model_name=self.model_name, body=e.response) from e
raise ModelAPIError(model_name=self.model_name, message=str(e)) from e
return usage.RequestUsage(input_tokens=response['inputTokens'])
@asynccontextmanager
async def request_stream(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
run_context: RunContext[Any] | None = None,
) -> AsyncIterator[StreamedResponse]:
model_settings, model_request_parameters = self.prepare_request(
model_settings,
model_request_parameters,
)
settings = cast(BedrockModelSettings, model_settings or {})
response = await self._messages_create(messages, True, settings, model_request_parameters)
yield BedrockStreamedResponse(
model_request_parameters=model_request_parameters,
_model_name=self.model_name,
_event_stream=response['stream'],
_provider_name=self._provider.name,
_provider_url=self.base_url,
_provider_response_id=response.get('ResponseMetadata', {}).get('RequestId', None),
)
async def _process_response(self, response: ConverseResponseTypeDef) -> ModelResponse:
items: list[ModelResponsePart] = []
if message := response['output'].get('message'): # pragma: no branch
for item in message['content']:
if reasoning_content := item.get('reasoningContent'):
if redacted_content := reasoning_content.get('redactedContent'):
items.append(
ThinkingPart(
id='redacted_content',
content='',
signature=redacted_content.decode('utf-8'),
provider_name=self.system,
)
)
elif reasoning_text := reasoning_content.get('reasoningText'): # pragma: no branch
signature = reasoning_text.get('signature')
items.append(
ThinkingPart(
content=reasoning_text['text'],
signature=signature,
provider_name=self.system if signature else None,
)
)
if text := item.get('text'):
items.append(TextPart(content=text))
elif tool_use := item.get('toolUse'):
if tool_use.get('type') == 'server_tool_use':
if tool_use['name'] == 'nova_code_interpreter': # pragma: no branch
items.append(
BuiltinToolCallPart(
provider_name=self.system,
tool_name=CodeExecutionTool.kind,
args=tool_use['input'],
tool_call_id=tool_use['toolUseId'],
)
)
else:
items.append(
ToolCallPart(
tool_name=tool_use['name'],
args=tool_use['input'],
tool_call_id=tool_use['toolUseId'],
),
)
elif tool_result := item.get('toolResult'):
if tool_result.get('type') == 'nova_code_interpreter_result': # pragma: no branch
items.append(
BuiltinToolReturnPart(
provider_name=self.system,
tool_name=CodeExecutionTool.kind,
content=tool_result['content'][0].get('json') if tool_result['content'] else None,
tool_call_id=tool_result.get('toolUseId'),
provider_details={'status': tool_result['status']} if 'status' in tool_result else {},
)
)
input_tokens = response['usage']['inputTokens']
output_tokens = response['usage']['outputTokens']
cache_read_tokens = response['usage'].get('cacheReadInputTokens', 0)
cache_write_tokens = response['usage'].get('cacheWriteInputTokens', 0)
u = usage.RequestUsage(
input_tokens=input_tokens + cache_write_tokens + cache_read_tokens,
output_tokens=output_tokens,
cache_read_tokens=cache_read_tokens,
cache_write_tokens=cache_write_tokens,
)
response_id = response.get('ResponseMetadata', {}).get('RequestId', None)
raw_finish_reason = response['stopReason']
provider_details = {'finish_reason': raw_finish_reason}
finish_reason = _FINISH_REASON_MAP.get(raw_finish_reason)
return ModelResponse(
parts=items,
usage=u,
model_name=self.model_name,
provider_response_id=response_id,
provider_name=self._provider.name,
provider_url=self.base_url,
finish_reason=finish_reason,
provider_details=provider_details,
)
def _get_thinking_fields(
self,
model_settings: BedrockModelSettings,
model_request_parameters: ModelRequestParameters,
) -> dict[str, Any] | None:
"""Build thinking-related additionalModelRequestFields, using unified thinking as fallback."""
existing = dict(model_settings.get('bedrock_additional_model_requests_fields') or {})
thinking = model_request_parameters.thinking
if thinking is None:
return existing or None
profile = BedrockModelProfile.from_profile(self.profile)
variant = profile.bedrock_thinking_variant
if variant == 'anthropic' and 'thinking' not in existing:
if thinking is False:
existing['thinking'] = {'type': 'disabled'}
else:
existing['thinking'] = {'type': 'enabled', 'budget_tokens': ANTHROPIC_THINKING_BUDGET_MAP[thinking]}
elif variant == 'openai' and 'reasoning_effort' not in existing:
if thinking is not False: # Bedrock doesn't accept reasoning_effort='none'
existing['reasoning_effort'] = OPENAI_REASONING_EFFORT_MAP[thinking]
elif variant == 'qwen' and 'reasoning_config' not in existing:
if thinking is not False:
# Qwen only supports low/high; map others to closest
level_map: dict[ThinkingLevel, str] = {
True: 'high',
'minimal': 'low',
'low': 'low',
'medium': 'high',
'high': 'high',
'xhigh': 'high',
}
existing['reasoning_config'] = level_map[thinking]
return existing or None
@overload
async def _messages_create(
self,
messages: list[ModelMessage],
stream: Literal[True],
model_settings: BedrockModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> ConverseStreamResponseTypeDef:
pass
@overload
async def _messages_create(
self,
messages: list[ModelMessage],
stream: Literal[False],
model_settings: BedrockModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> ConverseResponseTypeDef:
pass
async def _messages_create(
self,
messages: list[ModelMessage],
stream: bool,
model_settings: BedrockModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> ConverseResponseTypeDef | ConverseStreamResponseTypeDef:
settings = model_settings or BedrockModelSettings()
system_prompt, bedrock_messages = await self._map_messages(messages, model_request_parameters, settings)
inference_config = self._map_inference_config(settings)
params: ConverseRequestTypeDef = {
'modelId': settings.get('bedrock_inference_profile') or self.model_name,
'messages': bedrock_messages,
'system': system_prompt,
'inferenceConfig': inference_config,
}
tool_config = self._map_tool_config(model_request_parameters, settings)
if tool_config:
params['toolConfig'] = tool_config
tools: list[ToolTypeDef] = list(tool_config['tools']) if tool_config else []
self._limit_cache_points(system_prompt, bedrock_messages, tools)
# Bedrock supports a set of specific extra parameters
if model_settings:
if guardrail_config := model_settings.get('bedrock_guardrail_config', None):
params['guardrailConfig'] = guardrail_config
if performance_configuration := model_settings.get('bedrock_performance_configuration', None):
params['performanceConfig'] = performance_configuration
if request_metadata := model_settings.get('bedrock_request_metadata', None):
params['requestMetadata'] = request_metadata
if additional_model_response_fields_paths := model_settings.get(
'bedrock_additional_model_response_fields_paths', None
):
params['additionalModelResponseFieldPaths'] = additional_model_response_fields_paths
if additional_model_requests_fields := self._get_thinking_fields(model_settings, model_request_parameters):
params['additionalModelRequestFields'] = additional_model_requests_fields
if prompt_variables := model_settings.get('bedrock_prompt_variables', None):
params['promptVariables'] = prompt_variables
if service_tier := model_settings.get('bedrock_service_tier', None):
params['serviceTier'] = service_tier
try:
if stream:
model_response = await anyio.to_thread.run_sync(
functools.partial(self.client.converse_stream, **params)
)
else:
model_response = await anyio.to_thread.run_sync(functools.partial(self.client.converse, **params))
except ClientError as e:
status_code = e.response.get('ResponseMetadata', {}).get('HTTPStatusCode')
if isinstance(status_code, int):
raise ModelHTTPError(status_code=status_code, model_name=self.model_name, body=e.response) from e
raise ModelAPIError(model_name=self.model_name, message=str(e)) from e
return model_response
@staticmethod
def _map_inference_config(
model_settings: ModelSettings | None,
) -> InferenceConfigurationTypeDef:
model_settings = model_settings or {}
inference_config: InferenceConfigurationTypeDef = {}
if max_tokens := model_settings.get('max_tokens'):
inference_config['maxTokens'] = max_tokens
if (temperature := model_settings.get('temperature')) is not None:
inference_config['temperature'] = temperature
if top_p := model_settings.get('top_p'):
inference_config['topP'] = top_p
if stop_sequences := model_settings.get('stop_sequences'):
inference_config['stopSequences'] = stop_sequences
return inference_config
def _map_tool_config(
self,
model_request_parameters: ModelRequestParameters,
model_settings: BedrockModelSettings | None,
) -> ToolConfigurationTypeDef | None:
tools = self._get_tools(model_request_parameters)
for tool in model_request_parameters.builtin_tools:
if tool.kind == CodeExecutionTool.kind:
tools.append({'systemTool': {'name': 'nova_code_interpreter'}})
else:
raise NotImplementedError(
f"Builtin tool '{tool.kind}' is not supported yet. If it should be, please file an issue."
)
if not tools:
return None
profile = BedrockModelProfile.from_profile(self.profile)
if (
model_settings
and model_settings.get('bedrock_cache_tool_definitions')
and profile.bedrock_supports_tool_caching
):
tools.append({'cachePoint': {'type': 'default'}})
tool_choice: ToolChoiceTypeDef
if not model_request_parameters.allow_text_output:
tool_choice = {'any': {}}
else:
tool_choice = {'auto': {}}
tool_config: ToolConfigurationTypeDef = {'tools': tools}
if tool_choice and BedrockModelProfile.from_profile(self.profile).bedrock_supports_tool_choice:
tool_config['toolChoice'] = tool_choice
return tool_config
async def _map_messages( # noqa: C901
self,
messages: Sequence[ModelMessage],
model_request_parameters: ModelRequestParameters,
model_settings: BedrockModelSettings | None,
) -> tuple[list[SystemContentBlockTypeDef], list[MessageUnionTypeDef]]:
"""Maps a `pydantic_ai.Message` to the Bedrock `MessageUnionTypeDef`.
Groups consecutive ToolReturnPart objects into a single user message as required by Bedrock Claude/Nova models.
"""
settings = model_settings or BedrockModelSettings()
profile = BedrockModelProfile.from_profile(self.profile)
system_prompt: list[SystemContentBlockTypeDef] = []
bedrock_messages: list[MessageUnionTypeDef] = []
document_count: Iterator[int] = count(1)
for message in messages:
if isinstance(message, ModelRequest):
for part in message.parts:
if isinstance(part, SystemPromptPart):
if part.content: # pragma: no branch
system_prompt.append({'text': part.content})
elif isinstance(part, UserPromptPart):
bedrock_messages.extend(
await self._map_user_prompt(part, document_count, profile.bedrock_supports_prompt_caching)
)
elif isinstance(part, ToolReturnPart):
assert part.tool_call_id is not None
tool_result_content: list[Any] = []
sibling_content: list[ContentBlockUnionTypeDef] = []
content_mode: Literal['str', 'jsonable'] = (
'str' if profile.bedrock_tool_result_format == 'text' else 'jsonable'
)
for item in part.content_items(mode=content_mode):
if isinstance(item, UploadedFile):
if item.provider_name != self.system:
raise UserError(
f'UploadedFile with `provider_name={item.provider_name!r}` cannot be used with BedrockConverseModel. '
f'Expected `provider_name` to be `{self.system!r}`.'
)
if not item.file_id.startswith('s3://'):
raise UserError(
f'UploadedFile for Bedrock must use an S3 URL (s3://bucket/key), got: {item.file_id}'
)
uf_source = _parse_s3_source(item.file_id)
try:
uf_format = item.format
except ValueError as e:
raise UserError(
f'Unsupported media type for Bedrock UploadedFile: {item.media_type}'
) from e
if item.media_type.startswith('image/'):
tool_result_content.append(_make_image_block(uf_format, uf_source))
elif item.media_type.startswith('video/'):
tool_result_content.append(_make_video_block(uf_format, uf_source))
elif item.media_type.startswith('audio/'):
raise UserError('Audio files are not supported for Bedrock UploadedFile')
else:
tool_result_content.append(
_make_document_block(f'Document {next(document_count)}', uf_format, uf_source)
)
elif is_multi_modal_content(item):
if isinstance(item, AudioUrl):
raise NotImplementedError('AudioUrl is not supported in Bedrock tool returns')
file_block = await self._map_file_to_content_block(item, document_count) # pyright: ignore[reportArgumentType]
kind = next((k for k in ('image', 'document', 'video') if k in file_block), None)
if kind in profile.bedrock_supported_media_kinds_in_tool_returns:
tool_result_content.append(file_block)
else:
tool_result_content.append({'text': f'See file {item.identifier}.'})
sibling_content.append({'text': f'This is file {item.identifier}:'})
sibling_content.append(file_block)
elif isinstance(item, str):
tool_result_content.append({'text': item})
else:
tool_result_content.append({'json': item})
user_content: list[ContentBlockUnionTypeDef] = [
{
'toolResult': {
'toolUseId': part.tool_call_id,
'content': tool_result_content,
'status': 'success',
}
}
]
user_content.extend(sibling_content)
bedrock_messages.append({'role': 'user', 'content': user_content})
elif isinstance(part, RetryPromptPart):
if part.tool_name is None:
bedrock_messages.append({'role': 'user', 'content': [{'text': part.model_response()}]})
else:
assert part.tool_call_id is not None
bedrock_messages.append(
{
'role': 'user',
'content': [
{
'toolResult': {
'toolUseId': part.tool_call_id,
'content': [{'text': part.model_response()}],
'status': 'error',
}
}
],
}
)
else:
assert_never(part)
elif isinstance(message, ModelResponse):
content: list[ContentBlockOutputTypeDef] = []
for item in message.parts:
if isinstance(item, TextPart):
content.append({'text': item.content})
elif isinstance(item, ThinkingPart):
if (
item.provider_name == self.system
and item.signature
and BedrockModelProfile.from_profile(self.profile).bedrock_send_back_thinking_parts
):
reasoning_content: ReasoningContentBlockOutputTypeDef
if item.id == 'redacted_content':
reasoning_content = {
'redactedContent': item.signature.encode('utf-8'),
}
else:
reasoning_content = {
'reasoningText': {
'text': item.content,
'signature': item.signature,
}
}
content.append({'reasoningContent': reasoning_content})
else:
start_tag, end_tag = self.profile.thinking_tags
content.append({'text': '\n'.join([start_tag, item.content, end_tag])})
elif isinstance(item, BuiltinToolCallPart):
if item.provider_name == self.system:
if item.tool_name == CodeExecutionTool.kind:
server_tool_use_block_param: ToolUseBlockOutputTypeDef = {
'toolUseId': _utils.guard_tool_call_id(t=item),
'name': 'nova_code_interpreter',
'input': item.args_as_dict(),
'type': 'server_tool_use',
}
content.append({'toolUse': server_tool_use_block_param})
elif isinstance(item, BuiltinToolReturnPart):
if item.provider_name == self.system:
if item.tool_name == CodeExecutionTool.kind:
result_content: list[ToolResultContentBlockOutputTypeDef] = [
{'json': cast(dict[str, Any], item.content)}
]
tool_result: ToolResultBlockOutputTypeDef = {
'toolUseId': _utils.guard_tool_call_id(t=item),
'content': result_content,
'type': 'nova_code_interpreter_result',
}
if item.provider_details and 'status' in item.provider_details:
tool_result['status'] = item.provider_details['status']
content.append({'toolResult': tool_result})
else:
assert isinstance(item, ToolCallPart)
content.append(self._map_tool_call(item))
if content:
bedrock_messages.append({'role': 'assistant', 'content': content})
else:
assert_never(message)
# Merge together sequential user messages.
processed_messages: list[MessageUnionTypeDef] = []
last_message: dict[str, Any] | None = None
for current_message in bedrock_messages:
if (
last_message is not None
and current_message['role'] == last_message['role']
and current_message['role'] == 'user'
):
# Add the new user content onto the existing user message.
last_content = list(last_message['content'])
last_content.extend(current_message['content'])
last_message['content'] = last_content
continue
# Add the entire message to the list of messages.
processed_messages.append(current_message)
last_message = cast(dict[str, Any], current_message)
if instructions := self._get_instructions(messages, model_request_parameters):
system_prompt.append({'text': instructions})
if system_prompt and settings.get('bedrock_cache_instructions') and profile.bedrock_supports_prompt_caching:
system_prompt.append({'cachePoint': {'type': 'default'}})
if processed_messages and settings.get('bedrock_cache_messages') and profile.bedrock_supports_prompt_caching:
last_user_content = self._get_last_user_message_content(processed_messages)
if last_user_content is not None:
# Note: _get_last_user_message_content ensures content doesn't already end with a cachePoint.
_insert_cache_point_before_trailing_documents(last_user_content)
# Bedrock requires conversations to start with a user message.
# This can happen when there are no messages at all (only system prompt/instructions),
# or when message_history starts with an assistant response (e.g. from a previous
# system-prompt-only run). Prepend a synthetic user message in either case.
# Note: Anthropic models on Bedrock reject whitespace-only text, so we use a period.
if not processed_messages or processed_messages[0]['role'] != 'user':
processed_messages.insert(0, {'role': 'user', 'content': [{'text': '.'}]})
return system_prompt, processed_messages
@staticmethod
def _get_last_user_message_content(messages: list[MessageUnionTypeDef]) -> list[Any] | None:
"""Get the content list from the last user message that can receive a cache point.
Returns the content list if:
- A user message exists
- It has a non-empty content list
- The last content block doesn't already have a cache point
Returns None otherwise.
"""
user_messages = [msg for msg in messages if msg.get('role') == 'user']
if not user_messages:
return None
content = user_messages[-1].get('content') # Last user message
if not content or not isinstance(content, list) or len(content) == 0:
return None
last_block = content[-1]
if not isinstance(last_block, dict):
return None
if 'cachePoint' in last_block: # Skip if already has a cache point
return None
return content
@staticmethod
async def _map_file_to_content_block(
file: ImageUrl | DocumentUrl | VideoUrl | BinaryContent,
document_count: Iterator[int],
) -> ContentBlockUnionTypeDef:
"""Map a multimodal file directly to a Bedrock content block."""
source: DocumentSourceTypeDef
if isinstance(file, BinaryContent):
source = {'bytes': file.data}
if file.is_image:
return _make_image_block(file.format, source)
elif file.is_document:
return _make_document_block(f'Document {next(document_count)}', file.format, source)
elif file.is_video:
return _make_video_block(file.format, source)
else:
raise NotImplementedError(f'Unsupported binary content type for Bedrock: {file.media_type}')
else:
if file.url.startswith('s3://'):
source = _parse_s3_source(file.url)
else:
downloaded = await download_item(file, data_format='bytes', type_format='extension')
source = {'bytes': downloaded['data']}
try:
format = file.format
except (KeyError, ValueError):
format = file.media_type.split('/', 1)[1]
if isinstance(file, ImageUrl):
return _make_image_block(format, source)
elif isinstance(file, DocumentUrl):
return _make_document_block(f'Document {next(document_count)}', format, source)
else:
return _make_video_block(format, source)
async def _map_user_prompt( # noqa: C901
self,
part: UserPromptPart,
document_count: Iterator[int],
supports_prompt_caching: bool,
) -> list[MessageUnionTypeDef]:
content: list[ContentBlockUnionTypeDef] = []
if isinstance(part.content, str):
content.append({'text': part.content})
else:
for item in part.content:
if isinstance(item, str):
content.append({'text': item})
elif isinstance(item, (BinaryContent, ImageUrl, DocumentUrl, VideoUrl)):
content.append(await BedrockConverseModel._map_file_to_content_block(item, document_count))
elif isinstance(item, AudioUrl):
raise NotImplementedError('AudioUrl is not supported in Bedrock user prompts')
elif isinstance(item, UploadedFile):
if item.provider_name != self.system:
raise UserError(
f'UploadedFile with `provider_name={item.provider_name!r}` cannot be used with BedrockConverseModel. '
f'Expected `provider_name` to be `{self.system!r}`.'
)
if not item.file_id.startswith('s3://'):
raise UserError(
f'UploadedFile for Bedrock must use an S3 URL (s3://bucket/key), got: {item.file_id}'
)
source: DocumentSourceTypeDef = _parse_s3_source(item.file_id)
try:
format = item.format
except ValueError as e:
raise UserError(f'Unsupported media type for Bedrock UploadedFile: {item.media_type}') from e
if item.media_type.startswith('image/'):
content.append(_make_image_block(format, source))
elif item.media_type.startswith('video/'):
content.append(_make_video_block(format, source))
elif item.media_type.startswith('audio/'):
raise UserError('Audio files are not supported for Bedrock UploadedFile')
else:
content.append(_make_document_block(f'Document {next(document_count)}', format, source))
elif isinstance(item, CachePoint):
if not supports_prompt_caching:
# Silently skip CachePoint for models that don't support prompt caching
continue
if not content or 'cachePoint' in content[-1]:
raise UserError(
'CachePoint cannot be the first content in a user message - there must be previous content to cache when using Bedrock. '
'To cache system instructions or tool definitions, use the `bedrock_cache_instructions` or `bedrock_cache_tool_definitions` settings instead.'
)
_insert_cache_point_before_trailing_documents(content, raise_if_cannot_insert=True)
else:
assert_never(item)
# https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_Message.html
# "If you include a ContentBlock with a document field, you must also include a ContentBlock with a text field."
has_document = any('document' in block for block in content)
has_text = any('text' in block for block in content)
if has_document and not has_text:
content.insert(0, {'text': 'See attached document(s).'})
return [{'role': 'user', 'content': content}]
@staticmethod
def _map_tool_call(t: ToolCallPart) -> ContentBlockOutputTypeDef:
return {
'toolUse': {
'toolUseId': _utils.guard_tool_call_id(t=t),
'name': _utils.sanitize_tool_name(t.tool_name),
'input': t.args_as_dict(),
}
}
@staticmethod
def _limit_cache_points(
system_prompt: list[SystemContentBlockTypeDef],
bedrock_messages: list[MessageUnionTypeDef],
tools: list[ToolTypeDef],
) -> None:
"""Limit the number of cache points in the request to Bedrock's maximum.
Bedrock enforces a maximum of 4 cache points per request. This method ensures
compliance by counting existing cache points and removing excess ones from messages.
Strategy:
1. Count cache points in system_prompt
2. Count cache points in tools
3. Raise UserError if system + tools already exceed MAX_CACHE_POINTS
4. Calculate remaining budget for message cache points
5. Traverse messages from newest to oldest, keeping the most recent cache points
within the remaining budget
6. Remove excess cache points from older messages to stay within limit
Cache point priority (always preserved):
- System prompt cache points
- Tool definition cache points
- Message cache points (newest first, oldest removed if needed)
Raises:
UserError: If system_prompt and tools combined already exceed MAX_CACHE_POINTS (4).
This indicates a configuration error that cannot be auto-fixed.
"""
MAX_CACHE_POINTS = 4
# Count existing cache points in system prompt
used_cache_points = sum(1 for block in system_prompt if 'cachePoint' in block)
# Count existing cache points in tools
for tool in tools:
if 'cachePoint' in tool:
used_cache_points += 1
# Calculate remaining cache points budget for messages
remaining_budget = MAX_CACHE_POINTS - used_cache_points
if remaining_budget < 0: # pragma: no cover
raise UserError(
f'Too many cache points for Bedrock request. '
f'System prompt and tool definitions already use {used_cache_points} cache points, '
f'which exceeds the maximum of {MAX_CACHE_POINTS}.'
)
# Remove excess cache points from messages (newest to oldest)
for message in reversed(bedrock_messages):
content = message.get('content')
if not content or not isinstance(content, list): # pragma: no cover
continue
# Build a new content list, keeping only cache points within budget
new_content: list[Any] = []
for block in reversed(content): # Process newest first
is_cache_point = isinstance(block, dict) and 'cachePoint' in block
if is_cache_point:
if remaining_budget > 0:
remaining_budget -= 1
new_content.append(block)
else:
new_content.append(block)
message['content'] = list(reversed(new_content)) # Restore original order
|