Skip to content

The agent engineering platform.

fix(langchain): extra arg for anthropic caching, `__end__` -> `end` for `jump_to` (#33097)

Sydney Runkle57d931532fca
diff --git a/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py b/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
index 7187786555..8f993cae06 100644
--- a/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
+++ b/libs/langchain_v1/langchain/agents/middleware/prompt_caching.py
@@ -1,6 +1,6 @@
"""Anthropic prompt caching middleware."""
-from typing import Literal
+from typing import Any, Literal
from warnings import warn
from langchain.agents.middleware.types import AgentMiddleware, ModelRequest
@@ -42,6 +42,7 @@ class AnthropicPromptCachingMiddleware(AgentMiddleware):
def modify_model_request( # type: ignore[override]
self,
request: ModelRequest,
+ state: dict[str, Any], # noqa: ARG002
) -> ModelRequest:
"""Modify the model request to add cache control blocks."""
try:
diff --git a/libs/langchain_v1/langchain/agents/middleware/types.py b/libs/langchain_v1/langchain/agents/middleware/types.py
index cd55681a42..2d4c79e088 100644
--- a/libs/langchain_v1/langchain/agents/middleware/types.py
+++ b/libs/langchain_v1/langchain/agents/middleware/types.py
@@ -45,7 +45,7 @@ __all__ = [
"PublicAgentState",
]
-JumpTo = Literal["tools", "model", "__end__"]
+JumpTo = Literal["tools", "model", "end"]
"""Destination to jump to when a middleware node returns."""
ResponseT = TypeVar("ResponseT")
@@ -237,7 +237,7 @@ def before_model(
AgentState schema.
tools: Optional list of additional tools to register with this middleware.
jump_to: Optional list of valid jump destinations for conditional edges.
- Valid values are: "tools", "model", "__end__"
+ Valid values are: "tools", "model", "end"
name: Optional name for the generated middleware class. If not provided,
uses the decorated function's name.
@@ -260,10 +260,10 @@ def before_model(
Advanced usage with runtime and conditional jumping:
```python
- @before_model(jump_to=["__end__"])
+ @before_model(jump_to=["end"])
def conditional_before_model(state: AgentState, runtime: Runtime) -> dict[str, Any] | None:
if some_condition(state):
- return {"jump_to": "__end__"}
+ return {"jump_to": "end"}
return None
```
@@ -474,7 +474,7 @@ def after_model(
AgentState schema.
tools: Optional list of additional tools to register with this middleware.
jump_to: Optional list of valid jump destinations for conditional edges.
- Valid values are: "tools", "model", "__end__"
+ Valid values are: "tools", "model", "end"
name: Optional name for the generated middleware class. If not provided,
uses the decorated function's name.
diff --git a/libs/langchain_v1/langchain/agents/middleware_agent.py b/libs/langchain_v1/langchain/agents/middleware_agent.py
index 877c0e01f9..60d439eb1a 100644
--- a/libs/langchain_v1/langchain/agents/middleware_agent.py
+++ b/libs/langchain_v1/langchain/agents/middleware_agent.py
@@ -505,8 +505,10 @@ def create_agent( # noqa: PLR0915
def _resolve_jump(jump_to: JumpTo | None, first_node: str) -> str | None:
if jump_to == "model":
return first_node
- if jump_to:
- return jump_to
+ if jump_to == "end":
+ return "__end__"
+ if jump_to == "tools":
+ return "tools"
return None
@@ -602,7 +604,7 @@ def _add_middleware_edge(
destinations = [default_destination]
- if "__end__" in jump_to:
+ if "end" in jump_to:
destinations.append(END)
if "tools" in jump_to:
destinations.append("tools")
diff --git a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
index 73af122293..cd03be7a8d 100644
--- a/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
+++ b/libs/langchain_v1/tests/unit_tests/agents/test_middleware_agent.py
@@ -334,11 +334,11 @@ def test_create_agent_jump(
calls.append("NoopSeven.after_model")
class NoopEight(AgentMiddleware):
- before_model_jump_to = [END]
+ before_model_jump_to = ["end"]
def before_model(self, state) -> dict[str, Any]:
calls.append("NoopEight.before_model")
- return {"jump_to": END}
+ return {"jump_to": "end"}
def modify_model_request(self, request, state) -> ModelRequest:
calls.append("NoopEight.modify_model_request")
@@ -907,7 +907,7 @@ def test_anthropic_prompt_caching_middleware_initialization() -> None:
model_settings={},
)
- assert middleware.modify_model_request(fake_request).model_settings == {
+ assert middleware.modify_model_request(fake_request, {}).model_settings == {
"cache_control": {"type": "ephemeral", "ttl": "5m"}
}
@@ -930,7 +930,7 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
ValueError,
match="AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models. Please install langchain-anthropic.",
):
- middleware.modify_model_request(fake_request)
+ middleware.modify_model_request(fake_request, {})
langchain_anthropic = ModuleType("langchain_anthropic")
@@ -944,12 +944,12 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
ValueError,
match="AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models, not instances of",
):
- middleware.modify_model_request(fake_request)
+ middleware.modify_model_request(fake_request, {})
middleware = AnthropicPromptCachingMiddleware(unsupported_model_behavior="warn")
with warnings.catch_warnings(record=True) as w:
- result = middleware.modify_model_request(fake_request)
+ result = middleware.modify_model_request(fake_request, {})
assert len(w) == 1
assert (
"AnthropicPromptCachingMiddleware caching middleware only supports Anthropic models. Please install langchain-anthropic."
@@ -959,7 +959,7 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
with warnings.catch_warnings(record=True) as w:
with patch.dict("sys.modules", {"langchain_anthropic": langchain_anthropic}):
- result = middleware.modify_model_request(fake_request)
+ result = middleware.modify_model_request(fake_request, {})
assert result is fake_request
assert len(w) == 1
assert (
@@ -969,11 +969,11 @@ def test_anthropic_prompt_caching_middleware_unsupported_model() -> None:
middleware = AnthropicPromptCachingMiddleware(unsupported_model_behavior="ignore")
- result = middleware.modify_model_request(fake_request)
+ result = middleware.modify_model_request(fake_request, {})
assert result is fake_request
with patch.dict("sys.modules", {"langchain_anthropic": {"ChatAnthropic": object()}}):
- result = middleware.modify_model_request(fake_request)
+ result = middleware.modify_model_request(fake_request, {})
assert result is fake_request