Skip to content

Commit 084e295

Browse files
authored
fix(vertexai): preserve cache_control during ToolMessage → tool_result conversion (#1369)
1 parent 1f82c4e commit 084e295

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

libs/vertexai/langchain_google_vertexai/_anthropic_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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

libs/vertexai/tests/unit_tests/test_anthropic_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)