Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions libs/deepagents/deepagents/middleware/_prompt_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ def _create_bedrock_prompt_caching_middleware() -> AgentMiddleware[Any, Any, Any
return cast("AgentMiddleware[Any, Any, Any]", middleware_cls(unsupported_model_behavior="ignore"))


def _create_anthropic_vertex_prompt_caching_middleware() -> AgentMiddleware[Any, Any, Any] | None:
"""Create Anthropic Vertex caching middleware when available."""
module_name = "langchain_google_vertexai.middleware.prompt_caching"
try:
module = import_module(module_name)
except ImportError as exc:
if exc.name not in {
"langchain_google_vertexai",
"langchain_google_vertexai.middleware",
module_name,
}:
raise
logger.debug("Anthropic Vertex caching middleware is unavailable.", exc_info=exc)
return None
middleware_cls = module.AnthropicVertexPromptCachingMiddleware
return cast(
"AgentMiddleware[Any, Any, Any]",
middleware_cls(unsupported_model_behavior="ignore"),
)


def _create_fireworks_prompt_caching_middleware() -> AgentMiddleware[Any, Any, Any] | None:
"""Create Fireworks prompt caching middleware when `langchain-fireworks` is installed."""
module_name = "langchain_fireworks.middleware.prompt_caching"
Expand All @@ -41,6 +62,9 @@ def _create_fireworks_prompt_caching_middleware() -> AgentMiddleware[Any, Any, A
def append_prompt_caching_middleware(middleware: list[AgentMiddleware[Any, Any, Any]]) -> None:
"""Append provider-specific prompt caching middleware."""
middleware.append(AnthropicPromptCachingMiddleware(unsupported_model_behavior="ignore"))
vertex_middleware = _create_anthropic_vertex_prompt_caching_middleware()
if vertex_middleware is not None:
middleware.append(vertex_middleware)
bedrock_middleware = _create_bedrock_prompt_caching_middleware()
if bedrock_middleware is not None:
middleware.append(bedrock_middleware)
Expand Down
37 changes: 37 additions & 0 deletions libs/deepagents/tests/unit_tests/middleware/test_prompt_caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from unittest.mock import MagicMock, patch

import pytest

from deepagents.middleware._prompt_caching import (
_create_anthropic_vertex_prompt_caching_middleware,
)


def test_creates_anthropic_vertex_prompt_caching_middleware() -> None:
middleware = MagicMock()
middleware_cls = MagicMock(return_value=middleware)
module = MagicMock(AnthropicVertexPromptCachingMiddleware=middleware_cls)

with patch("deepagents.middleware._prompt_caching.import_module", return_value=module):
result = _create_anthropic_vertex_prompt_caching_middleware()

assert result is middleware
middleware_cls.assert_called_once_with(unsupported_model_behavior="ignore")


def test_anthropic_vertex_prompt_caching_is_optional() -> None:
error = ModuleNotFoundError(name="langchain_google_vertexai.middleware")

with patch("deepagents.middleware._prompt_caching.import_module", side_effect=error):
assert _create_anthropic_vertex_prompt_caching_middleware() is None


def test_anthropic_vertex_prompt_caching_preserves_unrelated_import_errors() -> None:
with (
patch(
"deepagents.middleware._prompt_caching.import_module",
side_effect=ImportError(name="missing_transitive"),
),
pytest.raises(ImportError),
):
_create_anthropic_vertex_prompt_caching_middleware()
2 changes: 2 additions & 0 deletions libs/deepagents/tests/unit_tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ def test_bedrock_prompt_caching_is_optional_when_middleware_unavailable(self) ->

with (
patch("deepagents.middleware._prompt_caching._create_fireworks_prompt_caching_middleware", return_value=None),
patch("deepagents.middleware._prompt_caching._create_anthropic_vertex_prompt_caching_middleware", return_value=None),
patch(
"deepagents.middleware._prompt_caching.import_module",
side_effect=ModuleNotFoundError(name="langchain_aws.middleware.prompt_caching"),
Expand Down Expand Up @@ -523,6 +524,7 @@ def test_fireworks_prompt_caching_is_optional_when_middleware_unavailable(self)

with (
patch("deepagents.middleware._prompt_caching._create_bedrock_prompt_caching_middleware", return_value=None),
patch("deepagents.middleware._prompt_caching._create_anthropic_vertex_prompt_caching_middleware", return_value=None),
patch(
"deepagents.middleware._prompt_caching.import_module",
side_effect=ModuleNotFoundError(name="langchain_fireworks.middleware.prompt_caching"),
Expand Down