litellm/tests/pass_through_unit_tests
Mateo Wang 2bbdbfa5c3
fix: passthrough endpoints duplicate logs (#29598)
* fix duplicate cost callbacks for anthropic streaming pass-through

Two bugs caused _PROXY_track_cost_callback to see stream=True +
complete_streaming_response=None on every streaming pass-through request,
making the dedup guard in dispatch_success_handlers permanently inactive:

1. pass_through_endpoints.py created the Logging object with stream=False
   for all requests. _is_assembled_stream_success short-circuits on
   self.stream is not True, so has_dispatched_final_stream_success was
   never set and any second dispatch went through unchecked.
   Fix: set logging_obj.stream = True after stream detection.

2. _create_anthropic_response_logging_payload set complete_streaming_response
   inside the try block after litellm.completion_cost(), so a pricing error
   caused an early return without setting it on model_call_details.
   Fix: set complete_streaming_response before the try block.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix stream

* add stream to logging obj

* test(pass_through): give mock logging object a real model_call_details dict

The anthropic passthrough logging payload now records the assembled
response on model_call_details before cost calculation, which requires
model_call_details to support item assignment. In production it is always
a dict; the existing unit test stubbed the logging object with a bare Mock
whose attribute is not subscriptable, so the new assignment raised
TypeError. Use a real dict to match the production logging object.

* test(pass_through): cover streaming logging-obj stream flag

The streaming branch of pass_through_request that marks the logging object
as streaming (logging_obj.stream and model_call_details["stream"]) had no
unit coverage, so the patch coverage gate flagged it. Add a regression test
that drives a streaming pass-through request through pass_through_request and
asserts the logging object is flagged as a stream before dispatch.

* test(pass_through): cover SSE-response stream flag fallback branch

The auto-detected streaming branch of pass_through_request (when a request
that was not flagged as streaming returns a text/event-stream response) sets
logging_obj.stream and model_call_details["stream"] but had no unit coverage,
so the codecov patch gate failed at 60%. Drive a non-streaming pass-through
request whose upstream response is SSE through pass_through_request and assert
the logging object is flagged as a stream before dispatch.

* fix(pass_through): gate complete_streaming_response on stream flag

perform_redaction only scrubs complete_streaming_response when
model_call_details["stream"] is True. Setting it unconditionally for
non-streaming Anthropic pass-through responses left the assembled
response unredacted in model_call_details, which is handed to logging
callbacks as kwargs when message logging is disabled. Only record it for
actual streaming responses so redaction always applies.

---------

Co-authored-by: mubashir1osmani <mubashir.osmani777@gmail.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-03 12:13:02 -07:00
..
messages_api_structured_output style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
base_anthropic_messages_prompt_caching_test.py fix(tests): use Sonnet 4.5 for Bedrock invoke prompt-caching tests 2026-04-28 14:51:47 -07:00
base_anthropic_messages_tool_search_test.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
base_anthropic_unified_messages_test.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
conftest.py fix(tests): stabilize image-edit VCR cassettes to stop live gpt-image-1 spend (#28110) 2026-05-18 09:15:39 -07:00
test_anthropic_messages_passthrough.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
test_anthropic_messages_prompt_caching.py fix(tests): use Sonnet 4.5 for Bedrock invoke prompt-caching tests 2026-04-28 14:51:47 -07:00
test_anthropic_messages_tool_search.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
test_assemblyai_unit_tests_passthrough.py fix: block path traversal SSRF in BitBucket, Arize Phoenix, and AssemblyAI clients (#26943) 2026-05-01 11:45:12 -07:00
test_bedrock_anthropic_messages_test.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
test_bedrock_tool_use_beta_header.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_claude_code_marketplace.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_context_management_polyfill.py feat(context_management): compact_20260112 polyfill for non-Anthropic providers (#28868) 2026-05-30 09:20:05 -07:00
test_custom_logger_passthrough.py chore(ci): modernize model references in tests and configs (#27856) 2026-05-15 15:44:28 -07:00
test_pass_through_unit_tests.py fix(bedrock): support tool search results + chat annotations (#29120) 2026-05-29 20:48:36 -07:00
test_passthrough_managed_ids.py feat(pass_through): extend passthrough_managed_object_ids to Azure (#29160) 2026-05-30 16:30:10 -07:00
test_passthrough_registry_updates.py Fix passthrough tests 2026-02-20 17:28:06 -08:00
test_unit_test_anthropic_pass_through.py fix: passthrough endpoints duplicate logs (#29598) 2026-06-03 12:13:02 -07:00
test_unit_test_passthrough_router.py test_get_credentials_from_env 2026-03-30 16:02:14 -07:00
test_unit_test_streaming.py [internal copy of #29089] fix: duplicate claude code traces (#29311) 2026-05-29 22:23:24 -07:00
test_vertex_ai_anthropic_streaming_cost_injection.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_vertex_ai_live_passthrough.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00
test_websearch_interception_e2e.py style: run black formatter on files from main merge 2026-04-17 13:02:59 -07:00