diff --git a/litellm/interactions/background_cost_polling.py b/litellm/interactions/background_cost_polling.py index 275eb17787e..26e8b616db5 100644 --- a/litellm/interactions/background_cost_polling.py +++ b/litellm/interactions/background_cost_polling.py @@ -307,8 +307,13 @@ def _spend_metadata(logging_obj: "LiteLLMLoggingObj") -> Mapping[str, object]: rebuilt logging object has no request headers to derive them from. """ litellm_params: Final = _OBJECT_DICT.validate_python(logging_obj.model_call_details.get("litellm_params") or {}) + metadata: Final = _OBJECT_MAPPING.validate_python( + get_litellm_metadata_from_kwargs(kwargs=logging_obj.model_call_details) + ) + if metadata.get("tags") and not isinstance(metadata.get("tags"), list): + return metadata return { - **_OBJECT_MAPPING.validate_python(get_litellm_metadata_from_kwargs(kwargs=logging_obj.model_call_details)), + **metadata, **_request_tags(litellm_params), } diff --git a/tests/unit/interactions/test_background_cost_polling.py b/tests/unit/interactions/test_background_cost_polling.py index d55994dfb19..994aeca83e7 100644 --- a/tests/unit/interactions/test_background_cost_polling.py +++ b/tests/unit/interactions/test_background_cost_polling.py @@ -484,13 +484,14 @@ async def test_schedule_registers_an_agent_only_create_that_names_no_model(): @pytest.mark.asyncio -async def test_schedule_registers_a_create_whose_tags_are_a_string_and_keeps_them_as_sent(): +@pytest.mark.parametrize("metadata_field", ["metadata", "litellm_metadata"]) +async def test_schedule_registers_a_create_whose_tags_are_a_string_and_keeps_them_as_sent(metadata_field): """ The regression: storing the create's context read string tags as a list and raised, so a create sent with a tags string answered with an error after the provider had already started the interaction. """ - logging_obj = _logging_obj(litellm_params={"metadata": _create_metadata(tags="team-a,nightly-batch")}) + logging_obj = _logging_obj(litellm_params={metadata_field: _create_metadata(tags="team-a,nightly-batch")}) store = InMemoryBackgroundSettlementStore() task = await maybe_schedule_background_interaction_cost_polling(