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%LangchainAffected 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