litellm/tests/llm_translation
Krrish Dholakia 50b936c75e
feat(guardrails/headroom): add CCR (compress-cache-retrieve) via agentic loop (#31681)
* feat(guardrails/headroom): add CCR (compress-cache-retrieve) support via agentic loop

When Headroom's /v1/compress returns messages containing hash markers
(hash=[a-f0-9]{24}), inject a headroom_retrieve tool into the request.
When the LLM calls that tool, intercept via async_should_run_agentic_loop
and async_build_agentic_loop_plan, call GET /v1/retrieve/{hash} on the
Headroom sidecar, and replay the LLM with the original content as a tool
result -- all transparent to the caller.

* style: run ruff format on headroom guardrail and tests

* fix(guardrails/headroom): detect headroom_retrieve calls in both OpenAI and Anthropic response formats

* test(guardrails/headroom): add test for Anthropic content block format detection in CCR loop

* ci: trigger CI checks

* fix(guardrails/headroom): replace List/Dict with list/dict to fix UP006 ruff violations

* fix(guardrails/headroom): replace except Exception with except ValueError to fix BLE001

* fix(guardrails/headroom): add Responses API output format detection for CCR tool calls

* refactor(guardrails/headroom): extract format-specific helpers to fix C901 complexity

* fix(guardrails/headroom): scope CCR retrieval to hashes produced by current request

Previously any LLM-supplied hash in a headroom_retrieve tool call was
forwarded to the Headroom retrieve API, letting a crafted tool call
fetch arbitrary cached content. Validate the hash against the set
produced by compressing the current request's messages before calling
retrieve.

* fix(guardrails/headroom): track issued hashes server-side, fix Responses API replay shape

Hash validation now also checks an in-memory cache of hashes actually
returned by /v1/compress, not just whether the hash text appears
somewhere in the request's messages. The message-text check alone is
forgeable: an attacker can plant a hash-shaped string in their own
prompt and have it treated as valid.

Responses API follow-up now emits function_call/function_call_output
items keyed by call_id instead of chat-style assistant/tool messages,
since the Responses API does not accept the latter as input. Also
fixes call_id/id field priority when extracting tool calls from
Responses API output, since call_id (not id) is what must match
between the function_call and its output.

* fix(guardrails/headroom): drop redundant quoted type annotations

UP037 flags quotes on annotations that are already lazily evaluated
via `from __future__ import annotations`.

* test(guardrails/headroom): add missing pytest.mark.asyncio decorators

Functional under asyncio_mode=auto, but every other async test in the
file has the decorator for consistency.

* fix(guardrails/headroom): scope CCR hashes per call_id, fix Anthropic replay shape

Two real gaps found in review:

1. The instance-wide issued-hash cache combined with a message-text
   check did not actually scope retrieval to the request that produced
   the hash. A hash issued for request A stays in the shared cache
   until TTL expiry, and the message-text check is satisfied by any
   request whose own messages happen to echo that hash string. Request
   B could plant A's hash in its own prompt and retrieve A's content.

   Fixed by keying the issued-hash cache by litellm_call_id, matching
   the pattern already used in compression_interception: a hash is
   only honored when it was issued under the exact call_id resolving
   for the current request.

2. The Anthropic Messages replay path fell through to the chat-style
   assistant/tool-message builder, which Anthropic does not accept.
   Anthropic requires the tool_use block echoed in an assistant message
   paired with a tool_result block in a user message, keyed by
   tool_use_id. Added a dedicated branch for this shape.

* docs: note proactive API-fragmentation helper convention

Add a bullet to the coding-conventions list: look for or add a shared
helper when logic branches on API surface (chat completions vs
Anthropic Messages vs Responses API), instead of duplicating
format-detection per module.

* fix(guardrails/headroom): fix Anthropic tool-shape detection, extract shared cross-API tool util

Live e2e testing against the real Anthropic API surfaced two bugs the
mocked unit tests couldn't catch because they used MagicMock responses
instead of realistic response shapes:

1. has_headroom_retrieve_tool only recognized OpenAI-shaped function
   tools. By the time an Anthropic Messages response reaches the
   agentic-loop gate, the tool this guardrail injected has already been
   transformed into Anthropic's native shape (type: "custom", top-level
   "name"), so the gate never fired for real Anthropic requests.

2. AnthropicMessagesResponse is a TypedDict, so real responses are
   plain dicts at runtime, not objects with attribute access. The
   extractors and format detectors used bare getattr(), which silently
   returns nothing for dict responses instead of reading the actual
   key.

Extracted the cross-API-surface tool-call extraction and tool-presence
check into litellm/litellm_core_utils/prompt_templates/factory.py
(get_tool_calls_from_response, has_tool_with_name) so this format
fragmentation is handled in one place instead of being duplicated
per-guardrail, and reused the existing repair-aware
parse_tool_call_arguments from common_utils instead of a naive
json.loads. headroom.py now delegates to these shared helpers.

Confirmed live against the real Anthropic API: the retrieve loop now
fires and successfully retrieves the correct hash's content through
the full compress -> tool-call -> retrieve -> replay round-trip.

* fix(guardrails/headroom): fix ruff-strict UP006/I001 budget violations

Use lowercase list/dict generics in the new factory.py tool-call
helpers instead of typing.List/Dict, drop the now-unused Tuple import
in headroom.py, and reorder the new factory import ahead of the
llms.custom_httpx import to satisfy import sorting.

* fix(guardrails/headroom): match Anthropic tools without a type field

Anthropic's documented client tool format is just name + input_schema;
type: "custom" is only one possible value, not a requirement. Match
any non-OpenAI-shaped tool on its top-level name instead of requiring
type == "custom".
2026-06-30 19:19:34 -07:00
..
fixtures test fix 2025-10-16 18:00:46 -07:00
realtime feat: litellm oss 110626 (#30202) 2026-06-11 22:30:26 -07:00
reasoning_effort_grid test(reasoning-effort-grid): bump cell-count assertion for claude-sonnet-5 2026-06-30 19:04:19 +00:00
test-skill [Feat] New API - Claude Skills API (Anthropic) (#17042) 2025-11-24 15:01:40 -08:00
test_llm_response_utils chore: litellm oss staging (#31185) 2026-06-26 09:17:44 -07:00
test_skills_data Remove Apache 2 license from SKILL.md (#22322) 2026-02-27 19:33:55 -08:00
base_audio_transcription_unit_tests.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
base_embedding_unit_tests.py Litellm dev 12 25 2025 p2 (#7420) 2024-12-25 18:35:34 -08:00
base_llm_unit_tests.py fix(bedrock): normalize Messages system role and adaptive-thinking for Claude Invoke (#31364) 2026-06-27 11:35:36 -07:00
base_rerank_unit_tests.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
conftest.py test: point router/completion/triton tests at the local fake OpenAI endpoint (#30900) 2026-06-20 16:20:35 -07:00
dog.wav (feat) Support audio param in responses streaming (#6312) 2024-10-18 19:16:14 +05:30
duck.png fix vertex ai multimodal embedding translation (#9471) 2025-03-24 23:23:28 -07:00
gettysburg.wav Litellm dev 12 25 2025 p2 (#7420) 2024-12-25 18:35:34 -08:00
guinea.png fix vertex ai multimodal embedding translation (#9471) 2025-03-24 23:23:28 -07:00
log.xt Litellm dev 04 05 2025 p2 (#9774) 2025-04-07 21:02:52 -07:00
Readme.md test: add 24hr Redis-backed VCR cache to additional test suites (#27159) 2026-05-05 15:13:31 -07:00
test_a2a.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_anthropic_completion.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
test_aws_base_llm.py Add support for AWS assume_role with a session token 2025-08-23 22:37:21 -07:00
test_azure_agents.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_azure_ai.py test_completion_azure 2026-03-30 21:54:27 -07:00
test_azure_o_series.py [Fix] CI: Enable VCR replay for test_azure_o_series 2026-05-04 20:48:26 -07:00
test_azure_openai.py Litellm ishaan april15 2 (#25828) 2026-04-15 18:42:23 -07:00
test_bedrock_agentcore.py Revert "chore(tests): migrate Bedrock CI to AWS account 941277531214 (#28728)" (#29326) 2026-05-30 11:26:24 -07:00
test_bedrock_agents.py test: skip test with invalid arn 2025-09-09 20:35:44 -07:00
test_bedrock_anthropic_regression.py fix(tests): replace deprecated Bedrock Claude 3.7 Sonnet model ID 2026-04-28 14:24:19 -07:00
test_bedrock_common_utils.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_bedrock_completion.py chore: litellm oss staging (#30968) 2026-06-23 07:31:44 -07:00
test_bedrock_dynamic_auth_params_unit_tests.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_bedrock_embedding.py chore: litellm oss staging (#31185) 2026-06-26 09:17:44 -07:00
test_bedrock_embedding_pricing.py chore: litellm oss staging (#30968) 2026-06-23 07:31:44 -07:00
test_bedrock_govcloud.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_bedrock_gpt_oss.py [Test] add request-body mock test for bedrock gpt-oss tool schema 2026-04-14 19:36:57 -07:00
test_bedrock_invoke_tests.py test(vcr): close out the remaining VCR live-call leaks (#29603) 2026-06-03 13:46:43 -07:00
test_bedrock_llama.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_bedrock_mantle.py fix(bedrock-mantle): use /anthropic/v1/messages path for Mantle endpo… (#27976) 2026-05-15 13:31:59 -07:00
test_bedrock_moonshot.py [Test] Mock remaining live Bedrock Moonshot tests 2026-04-16 17:43:43 -07:00
test_bedrock_nova_embedding.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_bedrock_nova_json.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_cloudflare.py fix(cloudflare): route native Workers AI provider through OpenAI-compatible endpoint (#31053) 2026-06-23 12:09:11 -07:00
test_cohere.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_containers_api.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_convert_dict_to_image.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_crusoe.py fix(crusoe): remove trailing slashes from API base URLs and fix list indentation 2026-05-01 17:27:52 +05:30
test_databricks.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_deepgram.py Litellm dev 12 28 2024 p3 (#7464) 2024-12-28 19:18:58 -08:00
test_deepseek_completion.py Litellm oss staging (#28161) 2026-05-18 16:27:44 -07:00
test_elevenlabs.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_evals_api.py [Fix] Tests: Reduce VCR cassette bloat and fix multipart caching 2026-05-07 11:54:19 -07:00
test_fireworks_ai_translation.py chore: litellm oss staging (#30968) 2026-06-23 07:31:44 -07:00
test_gemini.py Litellm oss staging 030626 (#29578) 2026-06-03 11:01:51 -07:00
test_gemini_image_usage.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_gigachat.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_gpt4o_audio.py fix(tests): replace shut-down gpt-4o-audio-preview with gpt-audio-1.5 (#28281) 2026-05-19 14:48:30 -07:00
test_groq.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_hosted_vllm_embedding_e2e.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_huggingface_chat_completion.py Revert "Revert "fix tests (#12286)"" 2025-07-03 12:08:27 -07:00
test_hyperbolic.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_infinity.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_jina_ai.py Litellm 12 02 2024 (#6994) 2024-12-02 22:00:01 -08:00
test_lambda_ai.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_langgraph.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_litellm_proxy_provider.py fix(tests): replace deprecated Bedrock Claude 3.7 Sonnet model ID 2026-04-28 14:24:19 -07:00
test_minimax_tts.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_mistral_api.py test: reduce mistral direct tests b/c of rate limit errors 2025-08-23 11:15:03 -07:00
test_model_cost_map_resilience.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_morph.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_nvidia_nim.py fix(tests): migrate realtime + rerank tests off shut-down upstream models (#28191) 2026-05-18 15:41:51 -07:00
test_openai.py test(vcr): drop dead 'from respx import MockRouter' imports 2026-05-13 00:32:03 +00:00
test_openai_o1.py test(vcr): drop dead 'from respx import MockRouter' imports 2026-05-13 00:32:03 +00:00
test_openai_record_replay_proxy.py Extend the record/replay proxy to chat, embeddings, moderations, rerank, and Anthropic (#29847) 2026-06-06 14:33:42 -07:00
test_openrouter.py Fix deprecated model test 2026-05-11 09:49:47 +05:30
test_optional_params.py fix(bedrock): normalize Messages system role and adaptive-thinking for Claude Invoke (#31364) 2026-06-27 11:35:36 -07:00
test_perplexity_reasoning.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_prompt_caching.py test(vcr): drop dead 'from respx import MockRouter' imports 2026-05-13 00:32:03 +00:00
test_prompt_factory.py feat(guardrails/headroom): add CCR (compress-cache-retrieve) via agentic loop (#31681) 2026-06-30 19:19:34 -07:00
test_replicate.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_rerank.py refactor: refactor testing 2026-03-28 18:39:32 -07:00
test_router_llm_translation_tests.py test: test 2026-03-28 19:17:38 -07:00
test_sambanova_chat_transformation.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_skills_api.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_skills_e2e.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_snowflake.py Merge main and resolve Snowflake test conflict 2026-03-30 18:06:37 -07:00
test_text_completion.py Add inference providers support for Hugging Face (#8258) (#9738) (#9773) 2025-04-05 10:50:15 -07:00
test_text_completion_unit_tests.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_together_ai.py [Fix] TogetherAIConfig.get_supported_openai_params recursion 2026-04-16 17:20:58 -07:00
test_triton.py test: point router/completion/triton tests at the local fake OpenAI endpoint (#30900) 2026-06-20 16:20:35 -07:00
test_unit_test_bedrock_invoke.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_v0.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_vcr_classification.py test: stabilize batch VCR coverage and stop live upload/network leaks (#29477) 2026-06-02 16:11:52 -07:00
test_vcr_conftest_common_banner.py fix(tests/vcr): make Redis cassette cache replay deterministically (zero VCR misses on consecutive runs) (#28826) 2026-05-26 11:30:44 -07:00
test_vcr_filters.py fix(tests/vcr): mint Google OAuth tokens live to prevent stale-token replay (#29229) 2026-05-28 17:12:02 -07:00
test_vcr_redis_persister.py test(vcr): stop refreshing cassette TTL on read so cassettes lapse after 24h (#29784) 2026-06-05 10:22:41 -07:00
test_voyage_ai.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_watsonx.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_xai.py test(vcr): drop dead 'from respx import MockRouter' imports 2026-05-13 00:32:03 +00:00

Unit tests for individual LLM providers.

Name of the test file is the name of the LLM provider - e.g. test_openai.py is for OpenAI.

Redis-backed VCR cache

Every test in this directory is auto-decorated with @pytest.mark.vcr (via conftest.py). The first time a test runs we hit the live provider and record the HTTP exchange into Redis under litellm:vcr:cassette:<test_id>. Every subsequent run within 24h replays from Redis without touching the network. The 24h TTL means each new day's first run records again, so upstream API drift surfaces within a day.

The persister, header scrubbing, and 2xx-only filtering are defined in tests/_vcr_redis_persister.py. Files that already use respx (which patches the same httpx transport vcrpy does) are excluded from the auto-marker — see _RESPX_CONFLICTING_FILES in conftest.py.

The same VCR cache is used by other test directories that exercise live provider APIs. The reusable conftest plumbing lives in tests/_vcr_conftest_common.py and is wired into:

  • tests/llm_translation/
  • tests/llm_responses_api_testing/
  • tests/audio_tests/
  • tests/batches_tests/
  • tests/guardrails_tests/
  • tests/image_gen_tests/
  • tests/litellm_utils_tests/
  • tests/local_testing/ (covers local_testing_part1, local_testing_part2, litellm_router_testing, litellm_assistants_api_testing, langfuse_logging_unit_tests)
  • tests/logging_callback_tests/
  • tests/pass_through_unit_tests/
  • tests/router_unit_tests/
  • tests/unified_google_tests/

Test directories that run LiteLLM proxy in Docker (e.g. build_and_test, proxy_logging_guardrails_model_info_tests, proxy_store_model_in_db_tests) are intentionally not included: VCR.py patches the in-process httpx transport, so it cannot intercept the LLM calls that originate inside the Docker container.

Required environment

CASSETTE_REDIS_URL — separate Redis instance from the application Redis (REDIS_URL/REDIS_HOST) so test cassettes are not flushed by proxy tests. Provider credentials (ANTHROPIC_API_KEY, OPENAI_API_KEY, AWS_*, etc.) are needed only on cache-miss (the daily re-record), not on replay.

Flushing the cache

When you want the next run to re-record immediately instead of waiting for the 24h TTL:

make test-llm-translation-flush-vcr-cache

Disabling VCR

Skip the cache entirely (every call goes live, no recording):

LITELLM_VCR_DISABLE=1 uv run pytest tests/llm_translation/test_<file>.py