Repository navigation
test: take keys out of the legacy proxy, enterprise and mcp unit tests before moving them #42901
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1ad9006
ci: fix the litellm-tests unit job with sysmon coverage, an env allow…
yuneng-berri 0df19f6
test: replace key-dependent proxy, enterprise and mcp unit tests with…
yuneng-berri 3833a4c
test: drop key reads at the legacy proxy, enterprise and mcp paths an…
yuneng-berri 8f84ef6
ci: fail the unit shard when circleci tests split errors
yuneng-berri 372f717
Merge branch 'litellm_ci_mig_1_ci_fix' into litellm_ci_mig_2_key_cleanup
yuneng-berri d2054d6
test: drop restating comments from the gemini pass-through split
yuneng-berri 5553b66
ci: exit the unit shard cleanly when circleci tests split assigns it …
yuneng-berri f2d1187
Merge branch 'litellm_ci_mig_1_ci_fix' into litellm_ci_mig_2_key_cleanup
yuneng-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
tests/e2e/llm_translation/test_token_counter_gemini_contents_e2e.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Live e2e: `/utils/token_counter?call_endpoint=true` counts Gemini `contents` upstream. | ||
|
|
||
| Google's countTokens API is the only tokenizer that knows Gemini's real token | ||
| boundaries, so the proxy must forward `contents` to it for both the AI Studio and | ||
| Vertex deployments and hand back the provider's `promptTokensDetails`. Claude on | ||
| Vertex is covered by `/v1/messages/count_tokens`; this is the Gemini `contents` | ||
| route the claude_code rows never reach | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| from e2e_config import unique_marker | ||
| from e2e_http import require_successful_call | ||
| from proxy_client import ProxyClient | ||
| from pydantic import BaseModel | ||
|
|
||
| pytestmark = pytest.mark.e2e | ||
|
|
||
| GEMINI_DEPLOYMENTS = ("gemini-2.5-flash", "gemini-2.5-flash-vertex") | ||
|
|
||
|
|
||
| class _Part(BaseModel): | ||
| text: str | ||
|
|
||
|
|
||
| class _Content(BaseModel): | ||
| parts: tuple[_Part, ...] | ||
|
|
||
|
|
||
| class _TokenCountBody(BaseModel): | ||
| model: str | ||
| contents: tuple[_Content, ...] | ||
|
|
||
|
|
||
| class _CallEndpoint(BaseModel): | ||
| call_endpoint: bool = True | ||
|
|
||
|
|
||
| class _ModalityTokens(BaseModel): | ||
| modality: str | ||
| tokenCount: int | ||
|
|
||
|
|
||
| class _CountTokensUpstream(BaseModel): | ||
| totalTokens: int | ||
| promptTokensDetails: tuple[_ModalityTokens, ...] | ||
|
|
||
|
|
||
| class _TokenCountResponse(BaseModel): | ||
| total_tokens: int | ||
| request_model: str | ||
| model_used: str | ||
| tokenizer_type: str | ||
| original_response: _CountTokensUpstream | ||
|
|
||
|
|
||
| class TestGeminiContentsTokenCounting: | ||
| @pytest.mark.parametrize("model", GEMINI_DEPLOYMENTS) | ||
| def test_contents_are_counted_by_the_provider_endpoint( | ||
| self, proxy: ProxyClient, scoped_key: str, model: str | ||
| ) -> None: | ||
| text = f"Hello world, how are you doing today? {unique_marker()}" | ||
| body = _TokenCountBody(model=model, contents=(_Content(parts=(_Part(text=text),)),)) | ||
|
|
||
| result = proxy.transport.send( | ||
| "/utils/token_counter", | ||
| headers=proxy.transport.bearer(scoped_key), | ||
| json=body, | ||
| params=_CallEndpoint(), | ||
| ) | ||
|
|
||
| require_successful_call(result) | ||
| counted = _TokenCountResponse.model_validate_json(result.body) | ||
| assert counted.request_model == model, counted | ||
| assert counted.original_response.totalTokens == counted.total_tokens > 0, counted | ||
| assert counted.original_response.promptTokensDetails, counted | ||
| assert all(detail.tokenCount > 0 for detail in counted.original_response.promptTokensDetails), counted | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import json | ||
| import uuid | ||
| from pathlib import Path | ||
| from typing import Final | ||
|
|
||
| from integration._support.client import Gateway | ||
| from integration._support.process import owned_proxy | ||
| from integration._support.wire import Reply, Request, wire_server | ||
|
|
||
| USER_KEY: Final = "sk-user-supplied-" + uuid.uuid4().hex | ||
|
|
||
|
|
||
| def _completion(request: Request) -> Reply: | ||
| if request.target != "/v1/chat/completions": | ||
| return Reply(status=404, body=b"{}") | ||
| body: Final = json.loads(request.body) | ||
| return Reply( | ||
| body=json.dumps( | ||
| { | ||
| "id": "chatcmpl-user-config", | ||
| "object": "chat.completion", | ||
| "created": 0, | ||
| "model": body["model"], | ||
| "choices": [ | ||
| {"index": 0, "message": {"role": "assistant", "content": "routed"}, "finish_reason": "stop"} | ||
| ], | ||
| "usage": {"prompt_tokens": 3, "completion_tokens": 1, "total_tokens": 4}, | ||
| } | ||
| ).encode() | ||
| ) | ||
|
|
||
|
|
||
| def _user_config(upstream_url: str) -> dict[str, object]: | ||
| return { | ||
| "model_list": [ | ||
| { | ||
| "model_name": "user-config-deployment", | ||
| "litellm_params": { | ||
| "model": "openai/gpt-4.1-mini", | ||
| "api_base": upstream_url + "/v1", | ||
| "api_key": USER_KEY, | ||
| }, | ||
| } | ||
| ], | ||
| "num_retries": 0, | ||
| } | ||
|
|
||
|
|
||
| def _opt_in_config(directory: Path, upstream_url: str) -> Path: | ||
| config: Final = directory / "allow_client_side_credentials_config.yaml" | ||
| config.write_text( | ||
| json.dumps( | ||
| { | ||
| "model_list": [ | ||
| { | ||
| "model_name": "admin-deployment", | ||
| "litellm_params": { | ||
| "model": "openai/gpt-4.1-mini", | ||
| "api_base": upstream_url + "/v1", | ||
| "api_key": "sk-admin-configured", | ||
| }, | ||
| } | ||
| ], | ||
| "general_settings": { | ||
| "master_key": "os.environ/LITELLM_MASTER_KEY", | ||
| "database_url": "os.environ/DATABASE_URL", | ||
| "store_model_in_db": True, | ||
| "allow_client_side_credentials": True, | ||
| }, | ||
| } | ||
| ) | ||
| ) | ||
| return config | ||
|
|
||
|
|
||
| def _request_body(upstream_url: str) -> dict[str, object]: | ||
| return { | ||
| "model": "user-config-deployment", | ||
| "messages": [{"role": "user", "content": "user config control"}], | ||
| "user_config": _user_config(upstream_url), | ||
| } | ||
|
|
||
|
|
||
| def test_user_config_routes_to_the_user_supplied_deployment_when_opted_in(gateway: Gateway, tmp_path: Path) -> None: | ||
| with wire_server(_completion) as upstream: | ||
| config: Final = _opt_in_config(tmp_path, upstream.url) | ||
| with owned_proxy(gateway, tmp_path, {}, config=config) as candidate: | ||
| response: Final = candidate.request("POST", "/v1/chat/completions", _request_body(upstream.url)) | ||
| assert response.status_code == 200, response.text | ||
| assert response.json()["choices"][0]["message"]["content"] == "routed" | ||
| outbound: Final = tuple(upstream.received.get_nowait() for _ in range(upstream.received.qsize())) | ||
| completions: Final = tuple(request for request in outbound if request.target == "/v1/chat/completions") | ||
| assert len(completions) == 1, outbound | ||
| assert completions[0].headers["authorization"] == f"Bearer {USER_KEY}" | ||
| assert json.loads(completions[0].body)["model"] == "gpt-4.1-mini" | ||
|
|
||
|
|
||
| def test_user_config_is_rejected_without_the_opt_in(gateway: Gateway) -> None: | ||
| with wire_server(_completion) as upstream: | ||
| response: Final = gateway.request("POST", "/v1/chat/completions", _request_body(upstream.url)) | ||
| assert response.status_code == 401, response.text | ||
| assert "user_config is not allowed in request body" in response.text | ||
| assert upstream.received.empty() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.