-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Litellm apikey trace #22294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Litellm apikey trace #22294
Changes from 2 commits
c58aea4
0edf31a
b92f9cb
f67cb1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -95,6 +95,92 @@ async def mock_aiter_bytes(): | |||||
| ), "Collected chunks do not match raw chunks" | ||||||
|
|
||||||
|
|
||||||
| @pytest.mark.asyncio | ||||||
| async def test_chunk_processor_passes_kwargs_to_logging_handler(): | ||||||
| """ | ||||||
| Test that kwargs (containing litellm_params with API key metadata) are | ||||||
| propagated from chunk_processor through to _route_streaming_logging_to_handler. | ||||||
|
|
||||||
| This ensures API key attribution reaches Langfuse traces for streaming | ||||||
| pass-through requests (e.g., Claude Code hitting /anthropic/v1/messages). | ||||||
| """ | ||||||
| response = AsyncMock(spec=httpx.Response) | ||||||
|
|
||||||
| # Minimal streaming response with message_start and message_stop events | ||||||
| raw_chunks = [ | ||||||
| b'event: message_start\ndata: {"type":"message_start","message":{"id":"msg_123","type":"message","role":"assistant","content":[],"model":"claude-3-haiku-20240307","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":10,"output_tokens":1}}}\n\n', | ||||||
| b'event: content_block_start\ndata: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}\n\n', | ||||||
| b'event: content_block_delta\ndata: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}\n\n', | ||||||
| b'event: content_block_stop\ndata: {"type":"content_block_stop","index":0}\n\n', | ||||||
| b'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":5}}\n\n', | ||||||
| b'event: message_stop\ndata: {"type":"message_stop"}\n\n', | ||||||
| ] | ||||||
|
|
||||||
| async def mock_aiter_bytes(): | ||||||
| for chunk in raw_chunks: | ||||||
| yield chunk | ||||||
|
|
||||||
| response.aiter_bytes = mock_aiter_bytes | ||||||
|
|
||||||
| request_body = {"model": "claude-3-haiku-20240307", "messages": [{"role": "user", "content": "Hi"}]} | ||||||
| litellm_logging_obj = MagicMock() | ||||||
| litellm_logging_obj.async_success_handler = AsyncMock() | ||||||
| litellm_logging_obj._should_run_sync_callbacks_for_async_calls = MagicMock(return_value=False) | ||||||
| litellm_logging_obj.model_call_details = {} | ||||||
| start_time = datetime.now() | ||||||
| passthrough_success_handler_obj = MagicMock() | ||||||
|
|
||||||
| # The kwargs that should be threaded through — simulating what | ||||||
| # _init_kwargs_for_pass_through_endpoint() creates | ||||||
| input_kwargs = { | ||||||
| "litellm_params": { | ||||||
| "metadata": { | ||||||
| "user_api_key_hash": "sk-hashed-abc123", | ||||||
| "user_api_key_alias": "test-key-alias", | ||||||
| "user_api_key_team_id": "team-456", | ||||||
| "user_api_key_user_id": "user-789", | ||||||
| "user_api_key_org_id": "org-012", | ||||||
| }, | ||||||
| "proxy_server_request": { | ||||||
| "url": "https://proxy/anthropic/v1/messages", | ||||||
| "method": "POST", | ||||||
| "body": request_body, | ||||||
| }, | ||||||
| }, | ||||||
| "passthrough_logging_payload": PassthroughStandardLoggingPayload( | ||||||
| url="https://api.anthropic.com/v1/messages", | ||||||
| request_body=request_body, | ||||||
| ), | ||||||
| "call_type": "pass_through_endpoint", | ||||||
| "litellm_call_id": "call-test-123", | ||||||
| } | ||||||
|
|
||||||
| # Consume the async generator | ||||||
| async for _ in PassThroughStreamingHandler.chunk_processor( | ||||||
| response=response, | ||||||
| request_body=request_body, | ||||||
| litellm_logging_obj=litellm_logging_obj, | ||||||
| endpoint_type=EndpointType.ANTHROPIC, | ||||||
| start_time=start_time, | ||||||
| passthrough_success_handler_obj=passthrough_success_handler_obj, | ||||||
| url_route="/v1/messages", | ||||||
| kwargs=input_kwargs, | ||||||
| ): | ||||||
| pass | ||||||
|
|
||||||
| # Allow the asyncio.create_task to run | ||||||
| import asyncio | ||||||
| await asyncio.sleep(0.5) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing
Suggested change
The import needs to be added at the top of the file: import asyncio
import json
import os
...Context Used: Context from |
||||||
|
|
||||||
| # Verify async_success_handler was called with kwargs containing | ||||||
| # the API key metadata from input_kwargs | ||||||
| if litellm_logging_obj.async_success_handler.called: | ||||||
| call_kwargs = litellm_logging_obj.async_success_handler.call_args | ||||||
| # The handler_kwargs are spread as **kwargs, check they include response_cost | ||||||
| # (set by the Anthropic handler) and that litellm_params metadata was preserved | ||||||
| assert call_kwargs is not None, "async_success_handler was called but with no args" | ||||||
Harshit28j marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
|
|
||||||
| def test_convert_raw_bytes_to_str_lines(): | ||||||
| """ | ||||||
| Test that the _convert_raw_bytes_to_str_lines method correctly converts raw bytes to a list of strings | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.