fix(logging): keep string metadata.tags as a single tag

This commit is contained in:
karank2512 2026-10-22 12:22:25 -05:00
parent 2744144960
commit 0efd8fb451
2 changed files with 9 additions and 18 deletions

View file

@ -7087,15 +7087,6 @@ class StandardLoggingPayloadSetup:
return header_tags if header_tags else None
@staticmethod
def _coerce_tags_to_list(tags: object) -> list[str]:
"""Return a copy of caller-provided tags as a list; a bare string becomes a one-item list."""
if isinstance(tags, str):
return [tags]
if isinstance(tags, (list, tuple, set)):
return cast(list[str], list(tags)) # cast-ok: tags are caller-provided
return []
@staticmethod
def get_request_tags(
litellm_params: dict[str, object],
@ -7107,12 +7098,11 @@ class StandardLoggingPayloadSetup:
litellm_metadata: Final = cast( # cast-ok: request metadata is caller-provided
Mapping[str, object], litellm_params.get("litellm_metadata") or {}
)
metadata_tags: Final = metadata.get("tags", [])
litellm_metadata_tags: Final = litellm_metadata.get("tags", [])
if metadata_tags:
request_tags = StandardLoggingPayloadSetup._coerce_tags_to_list(metadata_tags)
elif litellm_metadata_tags:
request_tags = StandardLoggingPayloadSetup._coerce_tags_to_list(litellm_metadata_tags)
raw_tags: Final = metadata.get("tags", []) or litellm_metadata.get("tags", [])
if isinstance(raw_tags, str):
request_tags = [raw_tags]
elif raw_tags:
request_tags = cast(list[str], raw_tags).copy() # cast-ok: tags are caller-provided
else:
request_tags = []
user_agent_tags: Final = StandardLoggingPayloadSetup._get_user_agent_tags(proxy_server_request)

View file

@ -484,11 +484,12 @@ 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():
async def test_schedule_registers_a_create_whose_tags_are_a_string_and_stores_them_as_a_list():
"""
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.
after the provider had already started the interaction. A string is now
read as a single tag.
"""
logging_obj = _logging_obj(litellm_params={"metadata": _create_metadata(tags="team-a,nightly-batch")})
store = InMemoryBackgroundSettlementStore()
@ -507,7 +508,7 @@ async def test_schedule_registers_a_create_whose_tags_are_a_string_and_keeps_the
pending = await store.pending("interactions/bg-abc")
assert pending is not None
rebuilt = _rebuild_logging_obj(pending.create_context)
assert get_litellm_metadata_from_kwargs(kwargs=rebuilt.model_call_details)["tags"] == "team-a,nightly-batch"
assert get_litellm_metadata_from_kwargs(kwargs=rebuilt.model_call_details)["tags"] == ["team-a,nightly-batch"]
@pytest.mark.asyncio