litellm/tests/unit/proxy/anthropic_endpoints
joshua-berri 272ee12967
fix(proxy): return cache keys on non-streaming Anthropic cache hits (#54217)
* fix(proxy): send x-litellm-cache-key on non-streaming /v1/messages cache hits

A non-streaming /v1/messages response cache hit comes back as a plain dict
with no hidden params, so the cache key never reached the response headers.
When a confirmed hit leaves the key out, the proxy now sends the exact key
the lookup recorded on the logging object. A key the response already
carries still wins, and other endpoints are unchanged.

Fixes #52585

* fix(proxy): read the recorded preset cache key through a typed Logging accessor

Reading the untyped Logging.litellm_params directly added unknown-type
diagnostics. Logging.get_preset_cache_key returns the recorded key as
str | None from the typed params view, with the same behavior.

The cache header test router now sets cache_responses=True like the proxy
does once its cache exists. A default Router passes caching=False on every
call, so repeats never read the cache.

* test(proxy): send the /v1/messages cache header tests through an injected client

The native Rust Anthropic transport bypasses respx, so the cache header
tests reached the real API. A proxy pre-call hook now hands each request
an AsyncHTTPHandler over httpx.MockTransport, which transport_handler
sends through, and the scripted upstream records what the proxy sent.
The fixture closes the client and awaits pending cache writes and logging.

* test(proxy): count provider dispatches in the /v1/messages cache header tests

Native Anthropic Messages is Rust-required, so no injected client or respx
route could keep those calls off the network. The fixture now uses a
deployment whose /v1/messages target runs on Python and replaces
anthropic_messages_handler with a counting stand-in, as
test_response_cache.py does. The proxy, Router, response cache and header
and body processing still run for real, and the dispatch count shows
whether a request was served from the cache.

* test(proxy): keep the cache header tests' fake client connected until teardown

The fake ASGI receive returned the same http.request on every call, so the
first-chunk disconnect watcher on a streamed reply never yielded and the
streaming cases spun. The cache tests now pass an asyncio.Event. Their
receive hands over the body once, then waits on that event and reports
http.disconnect. The fixture sets it in a finally. Existing callers of
_messages_request that pass no event behave as before.

---------

Co-authored-by: Joshua Valluru <326636767+joshua-berri@users.noreply.github.com>
2026-11-24 11:54:57 -08:00
..
__init__.py
test_answered_status.py fix(anthropic): answer an upstream Anthropic 529 as 529 overloaded_error on /v1/messages (#46063) 2026-11-03 17:37:09 -08:00
test_claude_code_marketplace.py refactor(auth_cache): type auth cache keys and keep the cache always native (#51130) 2026-11-21 17:20:30 +09:00
test_claude_code_skill_access.py feat(auth): read projects, tags, access groups and object permissions through rust repositories (#52178) 2026-11-18 22:32:06 +00:00
test_endpoints.py fix(proxy): return cache keys on non-streaming Anthropic cache hits (#54217) 2026-11-24 11:54:57 -08:00
test_gateway_endpoints.py feat(auth): decide key and JWT authentication in a Rust core (#51374) 2026-11-17 14:07:47 +09:00
test_skill_upload_archive.py test(skills): drop the archive rejection test whose zip-byte ids differ across xdist workers (#50118) 2026-11-04 22:12:55 +00:00
test_skills_endpoints.py
test_streaming_model_restamp.py fix(streaming): keep merged think tags balanced and split SSE lines only on CR/LF (#48755) 2026-10-28 17:00:29 -07:00