File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
langchain_google_vertexai Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -406,6 +406,12 @@ def _merge_messages(
406406 if curr .status == "error" :
407407 tool_result_block ["is_error" ] = True
408408
409+ cache_control = None
410+ if isinstance (curr .additional_kwargs , dict ):
411+ cache_control = curr .additional_kwargs .get ("cache_control" )
412+ if cache_control :
413+ tool_result_block ["cache_control" ] = cache_control
414+
409415 curr = HumanMessage ([tool_result_block ])
410416 elif isinstance (curr , AIMessage ):
411417 # Clean streaming metadata from AIMessage content blocks
Original file line number Diff line number Diff line change @@ -1279,3 +1279,33 @@ def test_format_messages_complex_multiturn_with_tools() -> None:
12791279 # Fourth message (AI response) should have 'index' removed
12801280 assert "index" not in formatted [3 ]["content" ][0 ]
12811281 assert formatted [3 ]["content" ][0 ]["text" ] == "It's sunny and 22°C in Paris!"
1282+
1283+
1284+ def test_tool_message_preserves_cache_control () -> None :
1285+ messages = [
1286+ AIMessage (
1287+ content = "" ,
1288+ tool_calls = [
1289+ create_tool_call (
1290+ name = "get_weather" ,
1291+ args = {"city" : "Paris" },
1292+ id = "call_1" ,
1293+ )
1294+ ],
1295+ ),
1296+ ToolMessage (
1297+ content = "Sunny, 22°C" ,
1298+ tool_call_id = "call_1" ,
1299+ additional_kwargs = {"cache_control" : {"type" : "ephemeral" }},
1300+ ),
1301+ ]
1302+
1303+ _ , formatted = _format_messages_anthropic (messages , project = "test-project" )
1304+ tool_result = formatted [1 ]["content" ][0 ]
1305+
1306+ assert tool_result == {
1307+ "type" : "tool_result" ,
1308+ "content" : "Sunny, 22°C" ,
1309+ "tool_use_id" : "call_1" ,
1310+ "cache_control" : {"type" : "ephemeral" },
1311+ }
You can’t perform that action at this time.
0 commit comments