diff --git a/litellm/proxy/batches_endpoints/litellm_executed_batches.py b/litellm/proxy/batches_endpoints/litellm_executed_batches.py index 4d99dc5db34..979e9abdba5 100644 --- a/litellm/proxy/batches_endpoints/litellm_executed_batches.py +++ b/litellm/proxy/batches_endpoints/litellm_executed_batches.py @@ -49,6 +49,7 @@ BatchStatus: TypeAlias = Literal[ TERMINAL_BATCH_STATUSES: Final[frozenset[str]] = frozenset({"completed", "failed", "cancelled", "expired"}) _STOP_STATUSES: Final[frozenset[str]] = TERMINAL_BATCH_STATUSES | frozenset({"cancelling"}) _BATCH_ENDPOINT_ADAPTER: Final[TypeAdapter[BatchEndpoint]] = TypeAdapter(BatchEndpoint) +_CLIENT_METADATA_ADAPTER: Final[TypeAdapter[dict[str, object]]] = TypeAdapter(dict[str, object]) _CANCEL_POLL_SECONDS: Final = 1.0 _HEARTBEAT_SECONDS: Final = 30.0 _STALE_AFTER_SECONDS: Final = 180.0 @@ -350,6 +351,19 @@ def _error_body(error: Exception) -> _ErrorBody: return body +def _without_client_key_metadata(body: Mapping[str, object]) -> Mapping[str, object]: + litellm_metadata: Final = body.get("litellm_metadata") + if not isinstance(litellm_metadata, Mapping): + return body + client_metadata: Final = _CLIENT_METADATA_ADAPTER.validate_python(litellm_metadata) + return { + **body, + "litellm_metadata": { + key: value for key, value in client_metadata.items() if not key.startswith("user_api_key_") + }, + } + + def _line_response(outcome: RowOutcome | ExpiredRow) -> _ResultResponse | None: if isinstance(outcome, ExpiredRow): return None @@ -645,7 +659,12 @@ class LiteLLMExecutedBatchRunner: async def _dispatch(self, run: _BatchRun, line: BatchInputLine) -> Mapping[str, object]: params: Final = MappingProxyType( - {**line.body, "model": run.model, "metadata": self._row_metadata(run), "disable_fallbacks": True} + { + **_without_client_key_metadata(line.body), + "model": run.model, + "metadata": self._row_metadata(run), + "disable_fallbacks": True, + } ) return _dump(await self._router_call(run.endpoint)(**params)) diff --git a/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py b/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py index c34ccf9d446..0272cbb3399 100644 --- a/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py +++ b/tests/unit/proxy/batches_endpoints/test_litellm_executed_batches.py @@ -660,7 +660,9 @@ async def test_resolve_litellm_executed_provider_asks_the_router_for_the_team_sc assert ( await resolve_litellm_executed_provider(router, BATCH_MODEL, "team-1", FakeFilesApiProbe(True, [])) == expected ) - router.registry.return_value.credentials.assert_called_once_with(BATCH_MODEL, form="with_provider", team_id="team-1") + router.registry.return_value.credentials.assert_called_once_with( + BATCH_MODEL, form="with_provider", team_id="team-1" + ) async def test_create_stores_a_validating_batch_and_completes_it_in_the_background() -> None: @@ -728,6 +730,20 @@ async def test_each_row_keeps_the_virtual_key_team_for_tag_spend(via_virtual_key assert teams == [expected] * 2 +async def test_a_row_cannot_claim_another_team_through_its_own_litellm_metadata() -> None: + from litellm.proxy.db.db_spend_update_writer import _virtual_key_team_id + + forged = {"user_api_key_is_virtual_key": True, "user_api_key_team_id": "team-other", "custom_field": "kept"} + user = UserAPIKeyAuth(api_key="sk-batch-key", team_id="team-1").model_copy(update={"via_virtual_key": False}) + harness = make_runner(content=jsonl(chat_row("row-1", "hi 1", litellm_metadata=forged)), user=user) + await harness.create_and_finish() + + (call,) = harness.router.acompletion.await_args_list + params = {"metadata": call.kwargs["metadata"], "litellm_metadata": call.kwargs["litellm_metadata"]} + assert _virtual_key_team_id({"litellm_params": params}) == "" + assert call.kwargs["litellm_metadata"] == {"custom_field": "kept"} + + async def test_create_dispatches_each_row_with_the_key_team_and_project_tags_as_inherited_tags() -> None: user = UserAPIKeyAuth( api_key="sk-batch-key",