Skip to content

Conversation

@ivancastanop
Copy link

Description

Fixes a bug in AnthropicPromptCachingMiddleware where cache_control parameters remain in the request after ModelFallbackMiddleware switches to non-Anthropic models at runtime, causing TypeError in other providers.

When using AnthropicPromptCachingMiddleware with ModelFallbackMiddleware:

  1. Primary Anthropic model fails
  2. ModelFallbackMiddleware switches request.model to OpenAI (or other provider)
  3. Anthropic-specific cache_control parameters remain in request.model_settings
  4. OpenAI API rejects the request: TypeError: Completions.create() got an unexpected keyword argument 'cache_control'

Solution: Add cleanup logic to remove cache_control parameters when the middleware detects a non-Anthropic model. This enables graceful fallback to any provider without Anthropic-specific settings causing errors. *The error is raised in the Prompt Caching Middleware. The solution should also be made in the same Middleware.

Changes

  • Add _remove_cache_control() method to safely remove cache settings
  • Update wrap_model_call() to clean up for non-Anthropic models
  • Update awrap_model_call() with same logic (async version)
  • Add comprehensive unit tests for cleanup behavior
  • Add integration tests for fallback scenarios (both sync and async)

Example Usage

from langchain.agents import create_agent
from langchain.agents.middleware import ModelFallbackMiddleware
from langchain_anthropic import ChatAnthropic
from langchain_anthropic.middleware import AnthropicPromptCachingMiddleware
from langchain_openai import ChatOpenAI

# Now works correctly with cross-provider fallback
agent = create_agent(
    model="claude-haiku-4-5-20251001",
    middleware=[
        ModelFallbackMiddleware(
            "claude-3-5-haiku-20241022",
            "gpt-4o-mini",  # ✅ No longer causes TypeError!
        ),
        AnthropicPromptCachingMiddleware(ttl="5m"),
    ],
)

Issue

Fixes #33709

Dependencies

None - no new dependencies added.

Testing

All tests pass:

  • make format - passed
  • make lint - passed
  • make test - unit tests pass
  • ✅ Integration tests added (were not present before)

…eware

- Add _remove_cache_control() method to safely remove cache settings
- Update wrap_model_call() to clean up cache_control for non-Anthropic models
- Update awrap_model_call() with same logic (async version)
- Add comprehensive unit tests for cleanup behavior
- Add integration tests for fallback scenarios

Fixes issue where ModelFallbackMiddleware switches to non-Anthropic models
but cache_control parameters remain in request.model_settings, causing
TypeError in OpenAI and other providers.
@github-actions github-actions bot added integration Related to a provider partner package integration fix labels Nov 6, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 6, 2025

CodSpeed Performance Report

Merging #33850 will not alter performance

Comparing ivancastanop:fix/prompt-caching-middleware-runtime-model-fallback (5011713) with master (022fdd5)1

Summary

✅ 3 untouched
⏩ 31 skipped2

Footnotes

  1. No successful run was found on master (9a09ed0) during the generation of this report, so 022fdd5 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 31 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix integration Related to a provider partner package integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AnthropicPromptCachingMiddleware breaks model fallback mechanism with incompatible cache_control parameter

1 participant