litellm/tests/integration/sdk/test_langfuse_redaction_sdk.py
devin-ai-integration[bot] 8a103e26ab
test: move routing, caching and callback tests out of local_testing (#45761)
* test: move routing, caching and callback tests out of local_testing

Moves the 37 routing, caching and callback files in tests/local_testing into mirrored tests/unit paths and tests/integration sdk and security groups per the migration manifest, and deletes the 8 rows it marks for deletion

* test: cover least-busy routing and tighten migrated router, cache and pass-through tests

Add router-level least-busy tests for the three legacy rows that had no
destination, move completion fallback and model alias tests to their mirror
files, move router batch tests next to the router, and strengthen tests
whose mutants survived (specific deployment, batch pair grouping, custom
cache key, router cache ttl). Add a unit test for configured pass-through
Authorization forwarding.

* test: tighten migrated callback, proxy config and cache ttl tests

Assert the full StandardLoggingPayload key set and the wire user on callback
tests, add an async embedding failure callback test, drive the prometheus
redis failure through a real RedisCache, use the real salt key in proxy
config tests, pin the redis ttl value and rename fallback test helpers

* test: fix semantic cache assertions and order-dependent router and callback tests

Count semantic cache embeddings on the route, drain async cache writes before
the second lookup, echo the requested model from the callback provider mock so
response_cost does not depend on model_cost entries other tests register, and
move the two-key authorization assertion to the auth failure test it belongs to

* test: cover model_list fan-out and the usage-based routing tpm limit lost in the move

* test: wait for the streamed cache entry in redis and drop deleted files from the hosted mock guard

* test: restore sync callback, audio redaction and langfuse export coverage, keep redis-compat filter on the moved file

* ci: run the remaining router-keyword legacy tests on one node

* test: refuse the redis write in-process instead of resolving an invalid host

---------

Co-authored-by: yuneng <yuneng@berri.ai>
2026-10-10 09:33:09 -07:00

79 lines
3.1 KiB
Python

import asyncio
import json
import uuid
from collections.abc import Iterator
from typing import Final
import pytest
from integration._support.wire import Reply, Request, Wire, wire_server
from opentelemetry.proto.collector.trace.v1.trace_service_pb2 import ExportTraceServiceRequest
from opentelemetry.proto.trace.v1.trace_pb2 import Span
import litellm
from litellm.integrations.langfuse.langfuse_sdk import resolve_trace_id
REDACTED: Final = "redacted-by-litellm"
def _accept(request: Request) -> Reply:
return Reply()
def _spans(bodies: tuple[bytes, ...]) -> Iterator[Span]:
for body in bodies:
for resource_spans in ExportTraceServiceRequest.FromString(body).resource_spans:
for scope_spans in resource_spans.scope_spans:
yield from scope_spans.spans
def _generations(bodies: tuple[bytes, ...], trace_id: str) -> tuple[dict[str, str], ...]:
attributes: Final = (
{attribute.key: attribute.value.string_value for attribute in span.attributes}
for span in _spans(bodies)
if span.trace_id.hex() == trace_id
)
return tuple(span for span in attributes if span.get("langfuse.observation.type") == "generation")
async def _trace_exports(wire: Wire, trace_id: str, seen: tuple[bytes, ...], attempts: int) -> tuple[bytes, ...]:
bodies: Final = seen + tuple(
request.body for request in wire.drain() if request.method == "POST" and request.target.endswith("/traces")
)
if attempts == 0 or _generations(bodies, trace_id):
return bodies
await asyncio.sleep(0.25)
return await _trace_exports(wire, trace_id, bodies, attempts - 1)
@pytest.mark.asyncio
@pytest.mark.parametrize("stream", [False, True])
async def test_langfuse_export_carries_no_raw_prompt_or_answer_when_message_logging_is_off(
stream: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
prompt: Final = f"prompt-{uuid.uuid4()}"
answer: Final = f"answer-{uuid.uuid4()}"
trace_name: Final = f"litellm-test-{uuid.uuid4()}"
monkeypatch.setattr(litellm, "turn_off_message_logging", True)
monkeypatch.setattr(litellm, "success_callback", ["langfuse"])
with wire_server(_accept) as wire:
response: Final = await litellm.acompletion(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
mock_response=answer,
stream=stream,
metadata={"trace_id": trace_name},
langfuse_public_key=f"pk-lf-{trace_name}",
langfuse_secret_key="sk-lf-local",
langfuse_host=wire.url,
)
if stream:
_ = [chunk async for chunk in response]
bodies: Final = await _trace_exports(wire, resolve_trace_id(trace_name), (), 120)
generations: Final = _generations(bodies, resolve_trace_id(trace_name))
assert len(generations) == 1, generations
assert json.loads(generations[0]["langfuse.observation.input"]) == {
"messages": [{"content": REDACTED, "role": "user"}]
}
assert json.loads(generations[0]["langfuse.observation.output"])["content"] == REDACTED
assert all(prompt.encode() not in body and answer.encode() not in body for body in bodies)