AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

AnthropicPromptCachingMiddleware Silently Drops String Content Blocks In System Messages

The middleware replaces a trailing string in system message content with a cache_control-only dict, losing the original text.

highConfidence 95%LangchainEmbeddingsChat ModelsAffected V1.7.1

Origin Analysis

In _tag_system_message of langchain_anthropic/middleware/prompt_caching.py, when the content is a list and the last element is a string, the code uses an empty dict as the base because it only handles dict blocks, resulting in a block with only cache_control and no text.
Run the provided reproduction code: instantiate ChatAnthropic and AnthropicPromptCachingMiddleware, create a ModelRequest with a SystemMessage whose content is a list containing a single string, wrap the call, and inspect the resulting system_message.content. The string will be replaced by a dict with only cache_control.

Fixing Code Block

Edge Case Audit

This change is localized and does not affect other content types. However, if downstream code expects strings to be left as-is (unlikely), it could be impacted. Test thoroughly with create_agent and SystemMessage(content=[...]) scenarios. Rollback: temporarily avoid string lists in system prompts or use dict text blocks instead.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state