16
16
from litellm .llms .custom_httpx .http_handler import HTTPHandler , get_async_httpx_client
17
17
from litellm .types .files import get_file_extension_from_mime_type
18
18
from litellm .types .llms .anthropic import *
19
- from litellm .types .llms .bedrock import MessageBlock as BedrockMessageBlock
20
19
from litellm .types .llms .bedrock import CachePointBlock
20
+ from litellm .types .llms .bedrock import MessageBlock as BedrockMessageBlock
21
21
from litellm .types .llms .custom_http import httpxSpecialProvider
22
22
from litellm .types .llms .ollama import OllamaVisionModelObject
23
23
from litellm .types .llms .openai import (
@@ -1067,10 +1067,10 @@ def convert_to_gemini_tool_call_invoke(
1067
1067
if tool_calls is not None :
1068
1068
for tool in tool_calls :
1069
1069
if "function" in tool :
1070
- gemini_function_call : Optional [VertexFunctionCall ] = (
1071
- _gemini_tool_call_invoke_helper (
1072
- function_call_params = tool [ "function" ]
1073
- )
1070
+ gemini_function_call : Optional [
1071
+ VertexFunctionCall
1072
+ ] = _gemini_tool_call_invoke_helper (
1073
+ function_call_params = tool [ "function" ]
1074
1074
)
1075
1075
if gemini_function_call is not None :
1076
1076
_parts_list .append (
@@ -1589,9 +1589,9 @@ def anthropic_messages_pt( # noqa: PLR0915
1589
1589
)
1590
1590
1591
1591
if "cache_control" in _content_element :
1592
- _anthropic_content_element ["cache_control" ] = (
1593
- _content_element [ "cache_control" ]
1594
- )
1592
+ _anthropic_content_element [
1593
+ "cache_control"
1594
+ ] = _content_element [ "cache_control" ]
1595
1595
user_content .append (_anthropic_content_element )
1596
1596
elif m .get ("type" , "" ) == "text" :
1597
1597
m = cast (ChatCompletionTextObject , m )
@@ -1629,9 +1629,9 @@ def anthropic_messages_pt( # noqa: PLR0915
1629
1629
)
1630
1630
1631
1631
if "cache_control" in _content_element :
1632
- _anthropic_content_text_element ["cache_control" ] = (
1633
- _content_element [ "cache_control" ]
1634
- )
1632
+ _anthropic_content_text_element [
1633
+ "cache_control"
1634
+ ] = _content_element [ "cache_control" ]
1635
1635
1636
1636
user_content .append (_anthropic_content_text_element )
1637
1637
@@ -2482,8 +2482,7 @@ def _validate_format(mime_type: str, image_format: str) -> str:
2482
2482
2483
2483
if is_document :
2484
2484
return BedrockImageProcessor ._get_document_format (
2485
- mime_type = mime_type ,
2486
- supported_doc_formats = supported_doc_formats
2485
+ mime_type = mime_type , supported_doc_formats = supported_doc_formats
2487
2486
)
2488
2487
2489
2488
else :
@@ -2495,12 +2494,9 @@ def _validate_format(mime_type: str, image_format: str) -> str:
2495
2494
f"Unsupported image format: { image_format } . Supported formats: { supported_image_and_video_formats } "
2496
2495
)
2497
2496
return image_format
2498
-
2497
+
2499
2498
@staticmethod
2500
- def _get_document_format (
2501
- mime_type : str ,
2502
- supported_doc_formats : List [str ]
2503
- ) -> str :
2499
+ def _get_document_format (mime_type : str , supported_doc_formats : List [str ]) -> str :
2504
2500
"""
2505
2501
Get the document format from the mime type
2506
2502
@@ -2519,13 +2515,9 @@ def _get_document_format(
2519
2515
The document format
2520
2516
"""
2521
2517
valid_extensions : Optional [List [str ]] = None
2522
- potential_extensions = mimetypes .guess_all_extensions (
2523
- mime_type , strict = False
2524
- )
2518
+ potential_extensions = mimetypes .guess_all_extensions (mime_type , strict = False )
2525
2519
valid_extensions = [
2526
- ext [1 :]
2527
- for ext in potential_extensions
2528
- if ext [1 :] in supported_doc_formats
2520
+ ext [1 :] for ext in potential_extensions if ext [1 :] in supported_doc_formats
2529
2521
]
2530
2522
2531
2523
# Fallback to types/files.py if mimetypes doesn't return valid extensions
@@ -2689,10 +2681,12 @@ def _convert_to_bedrock_tool_call_invoke(
2689
2681
)
2690
2682
bedrock_content_block = BedrockContentBlock (toolUse = bedrock_tool )
2691
2683
_parts_list .append (bedrock_content_block )
2692
-
2684
+
2693
2685
# Check for cache_control and add a separate cachePoint block
2694
2686
if tool .get ("cache_control" , None ) is not None :
2695
- cache_point_block = BedrockContentBlock (cachePoint = CachePointBlock (type = "default" ))
2687
+ cache_point_block = BedrockContentBlock (
2688
+ cachePoint = CachePointBlock (type = "default" )
2689
+ )
2696
2690
_parts_list .append (cache_point_block )
2697
2691
return _parts_list
2698
2692
except Exception as e :
@@ -2754,7 +2748,7 @@ def _convert_to_bedrock_tool_call_result(
2754
2748
for content in content_list :
2755
2749
if content ["type" ] == "text" :
2756
2750
content_str += content ["text" ]
2757
-
2751
+
2758
2752
message .get ("name" , "" )
2759
2753
id = str (message .get ("tool_call_id" , str (uuid .uuid4 ())))
2760
2754
@@ -2763,7 +2757,7 @@ def _convert_to_bedrock_tool_call_result(
2763
2757
content = [tool_result_content_block ],
2764
2758
toolUseId = id ,
2765
2759
)
2766
-
2760
+
2767
2761
content_block = BedrockContentBlock (toolResult = tool_result )
2768
2762
2769
2763
return content_block
@@ -3085,6 +3079,7 @@ def _initial_message_setup(
3085
3079
messages .append (DEFAULT_USER_CONTINUE_MESSAGE )
3086
3080
return messages
3087
3081
3082
+
3088
3083
@staticmethod
3089
3084
async def _bedrock_converse_messages_pt_async ( # noqa: PLR0915
3090
3085
messages : List ,
@@ -3128,6 +3123,12 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
3128
3123
if element ["type" ] == "text" :
3129
3124
_part = BedrockContentBlock (text = element ["text" ])
3130
3125
_parts .append (_part )
3126
+ elif element ["type" ] == "guarded_text" :
3127
+ # Wrap guarded_text in guardrailConverseContent block
3128
+ _part = BedrockContentBlock (
3129
+ guardrailConverseContent = {"text" : element ["text" ]}
3130
+ )
3131
+ _parts .append (_part )
3131
3132
elif element ["type" ] == "image_url" :
3132
3133
format : Optional [str ] = None
3133
3134
if isinstance (element ["image_url" ], dict ):
@@ -3170,6 +3171,7 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
3170
3171
3171
3172
msg_i += 1
3172
3173
if user_content :
3174
+
3173
3175
if len (contents ) > 0 and contents [- 1 ]["role" ] == "user" :
3174
3176
if (
3175
3177
assistant_continue_message is not None
@@ -3199,26 +3201,29 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
3199
3201
current_message = messages [msg_i ]
3200
3202
tool_call_result = _convert_to_bedrock_tool_call_result (current_message )
3201
3203
tool_content .append (tool_call_result )
3202
-
3204
+
3203
3205
# Check if we need to add a separate cachePoint block
3204
3206
has_cache_control = False
3205
-
3207
+
3206
3208
# Check for message-level cache_control
3207
3209
if current_message .get ("cache_control" , None ) is not None :
3208
3210
has_cache_control = True
3209
3211
# Check for content-level cache_control in list content
3210
3212
elif isinstance (current_message .get ("content" ), list ):
3211
3213
for content_element in current_message ["content" ]:
3212
- if (isinstance (content_element , dict ) and
3213
- content_element .get ("cache_control" , None ) is not None ):
3214
+ if (
3215
+ isinstance (content_element , dict )
3216
+ and content_element .get ("cache_control" , None ) is not None
3217
+ ):
3214
3218
has_cache_control = True
3215
3219
break
3216
-
3220
+
3217
3221
# Add a separate cachePoint block if cache_control is present
3218
3222
if has_cache_control :
3219
- cache_point_block = BedrockContentBlock (cachePoint = CachePointBlock (type = "default" ))
3223
+ cache_point_block = BedrockContentBlock (
3224
+ cachePoint = CachePointBlock (type = "default" )
3225
+ )
3220
3226
tool_content .append (cache_point_block )
3221
-
3222
3227
3223
3228
msg_i += 1
3224
3229
if tool_content :
@@ -3299,7 +3304,7 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
3299
3304
image_url = image_url
3300
3305
)
3301
3306
assistants_parts .append (assistants_part )
3302
- # Add cache point block for assistant content elements
3307
+ # Add cache point block for assistant content elements
3303
3308
_cache_point_block = (
3304
3309
litellm .AmazonConverseConfig ()._get_cache_point_block (
3305
3310
message_block = cast (
@@ -3311,8 +3316,12 @@ async def _bedrock_converse_messages_pt_async( # noqa: PLR0915
3311
3316
if _cache_point_block is not None :
3312
3317
assistants_parts .append (_cache_point_block )
3313
3318
assistant_content .extend (assistants_parts )
3314
- elif _assistant_content is not None and isinstance (_assistant_content , str ):
3315
- assistant_content .append (BedrockContentBlock (text = _assistant_content ))
3319
+ elif _assistant_content is not None and isinstance (
3320
+ _assistant_content , str
3321
+ ):
3322
+ assistant_content .append (
3323
+ BedrockContentBlock (text = _assistant_content )
3324
+ )
3316
3325
# Add cache point block for assistant string content
3317
3326
_cache_point_block = (
3318
3327
litellm .AmazonConverseConfig ()._get_cache_point_block (
@@ -3496,6 +3505,12 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915
3496
3505
if element ["type" ] == "text" :
3497
3506
_part = BedrockContentBlock (text = element ["text" ])
3498
3507
_parts .append (_part )
3508
+ elif element ["type" ] == "guarded_text" :
3509
+ # Wrap guarded_text in guardrailConverseContent block
3510
+ _part = BedrockContentBlock (
3511
+ guardrailConverseContent = {"text" : element ["text" ]}
3512
+ )
3513
+ _parts .append (_part )
3499
3514
elif element ["type" ] == "image_url" :
3500
3515
format : Optional [str ] = None
3501
3516
if isinstance (element ["image_url" ], dict ):
@@ -3539,6 +3554,7 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915
3539
3554
3540
3555
msg_i += 1
3541
3556
if user_content :
3557
+
3542
3558
if len (contents ) > 0 and contents [- 1 ]["role" ] == "user" :
3543
3559
if (
3544
3560
assistant_continue_message is not None
@@ -3565,29 +3581,33 @@ def _bedrock_converse_messages_pt( # noqa: PLR0915
3565
3581
while msg_i < len (messages ) and messages [msg_i ]["role" ] == "tool" :
3566
3582
tool_call_result = _convert_to_bedrock_tool_call_result (messages [msg_i ])
3567
3583
current_message = messages [msg_i ]
3568
-
3584
+
3569
3585
# Add the tool result first
3570
3586
tool_content .append (tool_call_result )
3571
-
3587
+
3572
3588
# Check if we need to add a separate cachePoint block
3573
3589
has_cache_control = False
3574
-
3590
+
3575
3591
# Check for message-level cache_control
3576
3592
if current_message .get ("cache_control" , None ) is not None :
3577
3593
has_cache_control = True
3578
3594
# Check for content-level cache_control in list content
3579
3595
elif isinstance (current_message .get ("content" ), list ):
3580
3596
for content_element in current_message ["content" ]:
3581
- if (isinstance (content_element , dict ) and
3582
- content_element .get ("cache_control" , None ) is not None ):
3597
+ if (
3598
+ isinstance (content_element , dict )
3599
+ and content_element .get ("cache_control" , None ) is not None
3600
+ ):
3583
3601
has_cache_control = True
3584
3602
break
3585
-
3603
+
3586
3604
# Add a separate cachePoint block if cache_control is present
3587
3605
if has_cache_control :
3588
- cache_point_block = BedrockContentBlock (cachePoint = CachePointBlock (type = "default" ))
3606
+ cache_point_block = BedrockContentBlock (
3607
+ cachePoint = CachePointBlock (type = "default" )
3608
+ )
3589
3609
tool_content .append (cache_point_block )
3590
-
3610
+
3591
3611
msg_i += 1
3592
3612
if tool_content :
3593
3613
# if last message was a 'user' message, then add a blank assistant message (bedrock requires alternating roles)
@@ -3852,10 +3872,9 @@ def function_call_prompt(messages: list, functions: list):
3852
3872
if isinstance (message ["content" ], str ):
3853
3873
message ["content" ] += f""" { function_prompt } """
3854
3874
else :
3855
- message ["content" ].append ({
3856
- "type" : "text" ,
3857
- "text" : f""" { function_prompt } """
3858
- })
3875
+ message ["content" ].append (
3876
+ {"type" : "text" , "text" : f""" { function_prompt } """ }
3877
+ )
3859
3878
function_added_to_prompt = True
3860
3879
3861
3880
if function_added_to_prompt is False :
0 commit comments