@@ -41,6 +41,85 @@ def test_transform_usage():
41
41
assert openai_usage ._cache_creation_input_tokens == usage ["cacheWriteInputTokens" ]
42
42
assert openai_usage ._cache_read_input_tokens == usage ["cacheReadInputTokens" ]
43
43
44
+ def test_transform_system_message ():
45
+ config = AmazonConverseConfig ()
46
+
47
+ # Case 1:
48
+ # System message popped
49
+ # User message remains
50
+ messages = [
51
+ {"role" : "system" , "content" : "You are a helpful assistant." },
52
+ {"role" : "user" , "content" : "Hello!" },
53
+ ]
54
+ out_messages , system_blocks = config ._transform_system_message (messages .copy ())
55
+ assert len (out_messages ) == 1
56
+ assert out_messages [0 ]["role" ] == "user"
57
+ assert len (system_blocks ) == 1
58
+ assert system_blocks [0 ]["text" ] == "You are a helpful assistant."
59
+
60
+ # Case 2: System message with list content (type text)
61
+ messages = [
62
+ {
63
+ "role" : "system" ,
64
+ "content" : [
65
+ {"type" : "text" , "text" : "System prompt 1" },
66
+ {"type" : "text" , "text" : "System prompt 2" },
67
+ ],
68
+ },
69
+ {"role" : "user" , "content" : "Hi!" },
70
+ ]
71
+ out_messages , system_blocks = config ._transform_system_message (messages .copy ())
72
+ assert len (out_messages ) == 1
73
+ assert out_messages [0 ]["role" ] == "user"
74
+ assert len (system_blocks ) == 2
75
+ assert system_blocks [0 ]["text" ] == "System prompt 1"
76
+ assert system_blocks [1 ]["text" ] == "System prompt 2"
77
+
78
+ # Case 3: System message with cache_control (should add cachePoint)
79
+ messages = [
80
+ {
81
+ "role" : "system" ,
82
+ "content" : "Cache this!" ,
83
+ "cache_control" : {"type" : "ephemeral" },
84
+ },
85
+ {"role" : "user" , "content" : "Hi!" },
86
+ ]
87
+ out_messages , system_blocks = config ._transform_system_message (messages .copy ())
88
+ assert len (out_messages ) == 1
89
+ assert len (system_blocks ) == 2
90
+ assert system_blocks [0 ]["text" ] == "Cache this!"
91
+ assert "cachePoint" in system_blocks [1 ]
92
+ assert system_blocks [1 ]["cachePoint" ]["type" ] == "default"
93
+
94
+ # Case 3b: System message with two blocks, one with cache_control and one without
95
+ messages = [
96
+ {
97
+ "role" : "system" ,
98
+ "content" : [
99
+ {"type" : "text" , "text" : "Cache this!" , "cache_control" : {"type" : "ephemeral" }},
100
+ {"type" : "text" , "text" : "Don't cache this!" },
101
+ ],
102
+ },
103
+ {"role" : "user" , "content" : "Hi!" },
104
+ ]
105
+ out_messages , system_blocks = config ._transform_system_message (messages .copy ())
106
+ assert len (out_messages ) == 1
107
+ assert len (system_blocks ) == 3
108
+ assert system_blocks [0 ]["text" ] == "Cache this!"
109
+ assert "cachePoint" in system_blocks [1 ]
110
+ assert system_blocks [1 ]["cachePoint" ]["type" ] == "default"
111
+ assert system_blocks [2 ]["text" ] == "Don't cache this!"
112
+
113
+ # Case 4: Non-system messages are not affected
114
+ messages = [
115
+ {"role" : "user" , "content" : "Hello!" },
116
+ {"role" : "assistant" , "content" : "Hi!" },
117
+ ]
118
+ out_messages , system_blocks = config ._transform_system_message (messages .copy ())
119
+ assert len (out_messages ) == 2
120
+ assert out_messages [0 ]["role" ] == "user"
121
+ assert out_messages [1 ]["role" ] == "assistant"
122
+ assert system_blocks == []
44
123
45
124
def test_transform_thinking_blocks_with_redacted_content ():
46
125
thinking_blocks = [
@@ -59,3 +138,4 @@ def test_transform_thinking_blocks_with_redacted_content():
59
138
assert len (transformed_thinking_blocks ) == 2
60
139
assert transformed_thinking_blocks [0 ]["type" ] == "thinking"
61
140
assert transformed_thinking_blocks [1 ]["type" ] == "redacted_thinking"
141
+
0 commit comments