litellm/tests/integration/database/test_lens_dataset_repository.py
ishaan-berri 5fb23eabd8
feat(lens): add datasets built from real traces (#44765)
* feat(lens): add dataset case and size limits

* feat(lens): add dataset, case and build models

* feat(lens): build dataset cases from traces, findings and text

* feat(lens): store dataset revisions insert-only

* feat(lens): add dataset routes for build, save, export and eval cases

* feat(lens): mount the dataset router before lens routes

* feat(lens): add LiteLLM_LensDataset table

* feat(lens): add LiteLLM_LensDataset table to proxy schema

* feat(lens): add LiteLLM_LensDataset table to extras schema

* feat(lens): add migration that creates the dataset table

* test(lens): cover dataset case building, dedupe and limits

* test(lens): cover dataset revisions, conflicts and eval cases

* chore(ui): regenerate API types for lens datasets

* feat(lens): add dataset UI types

* feat(lens): add datasets API client

* feat(lens): add dataset query and mutation hooks

* feat(lens): add case selection and expected edit logic

* test(lens): cover case selection and expected edits

* feat(lens): add the add to dataset dialog

* test(lens): cover saving picked cases from the dialog

* feat(lens): add datasets list

* feat(lens): add dataset detail with revisions and export

* test(lens): cover editing, revisions and export in datasets tab

* feat(lens): expose datasets on the lens API

* feat(lens): add in-memory datasets for demo mode

* feat(lens): wire demo datasets into the demo lens API

* feat(lens): add optional lens API hook

* feat(lens): add optional onboarding hook

* feat(lens): add datasets tab and dataset routing

* feat(lens): show the datasets tab

* feat(lens): add to dataset from the trace header

* feat(lens): add a single turn to a dataset from a step

* feat(lens): add finding evidence to a dataset

* docs(lens): add datasets screenshots for the PR

* test(lens): cover dataset revision storage against Postgres

* test(lens): cover dataset trace paging, findings and route errors

* test(lens): cover dataset build fallbacks and no_content skips

* fix(lens): register the dataset table for postgres span names

* feat(lens): add invalid skip reason for unparseable case lines

* fix(lens): keep valid JSONL cases, provenance and size limits; stop reading past the case cap

* test(lens): cover malformed JSONL, re-import provenance, size fields and early cap

* chore(ui): regenerate API types for the invalid skip reason

* feat(lens): show text for the invalid skip reason

* feat(lens): render datasets in the same inspector table as investigations

* test(lens): open a dataset by clicking its table row

* docs(lens): update the datasets list screenshot

* style(lens): format the datasets table

* fix(lens): normalize JSON text span input and output into messages when building cases

* test(lens): cover JSON text span normalization for dataset cases

* Revert "fix(lens): normalize JSON text span input and output into messages when building cases"

This reverts commit 29c3e34962fa12427dbd516c08f2599db997a905.

* fix(traces): normalize agent assistant summaries into UI messages

* feat(lens): add dataset case view helpers built on the trace parsers

* test(lens): cover dataset case view helpers

* feat(lens): show dataset cases in an inspector table

* feat(lens): open a dataset case in a side panel with trace message cards

* feat(lens): rebuild the dataset page header and layout

* feat(lens): keep the open dataset case in the URL

* test(lens): drive dataset edits through the case table and panel

* docs(lens): update dataset view screenshots

* Revert "test(lens): cover JSON text span normalization for dataset cases"

This reverts commit 30229ae8ae90265c09d755c43c1f1eeaa66050c7.

* fix(lens): import StateMessage from shared in dataset detail

* fix(lens): import StateMessage from shared in datasets list

* fix(lens): give the datasets migration a unique timestamp after review checkpoints
2026-10-06 23:04:43 +00:00

149 lines
5.4 KiB
Python

import os
from collections.abc import AsyncIterator
from datetime import datetime, timedelta, timezone
from typing import Final
from uuid import uuid4
import pytest
import pytest_asyncio
from prisma import Prisma
from prisma.types import DatasourceOverride
from litellm.proxy.db.prisma_client import PrismaWrapper
from litellm.proxy.lens.dataset_repository import DatasetRepository, StoredSummary
from litellm.proxy.lens.models import CaseSource, Dataset, DatasetCase, DatasetMessage, DatasetSummary
from litellm.proxy.lens.repository import WriterDatabase
SAVED_AT: Final = datetime(2026, 3, 1, 12, 0, 0, 123000, tzinfo=timezone.utc)
@pytest_asyncio.fixture(loop_scope="function")
async def lens_db() -> AsyncIterator[Prisma]:
async with Prisma(datasource=DatasourceOverride(url=os.environ["DATABASE_URL"])) as db:
yield db
@pytest_asyncio.fixture(loop_scope="function")
async def dataset_ids(lens_db: Prisma) -> AsyncIterator[tuple[str, ...]]:
ids: Final = tuple(uuid4().hex for _ in range(3))
yield ids
await lens_db.execute_raw('DELETE FROM "LiteLLM_LensDataset" WHERE id = ANY($1::text[])', list(ids))
def _repo(db: Prisma) -> DatasetRepository:
return DatasetRepository(WriterDatabase(PrismaWrapper(db)))
def _dataset(dataset_id: str, revision: int, case_count: int, team_id: str = "team-a") -> Dataset:
return Dataset(
id=dataset_id,
name=f"Dataset r{revision}",
agent_name="support-agent",
team_id=team_id,
created_at=SAVED_AT,
revision=revision,
created_by="user-1",
cases=tuple(
DatasetCase(
id=f"case-{revision}-{i}",
messages=(DatasetMessage(role="user", content=f"question {i}"),),
reply=f"answer {i}",
source=CaseSource(trace_id=f"trace-{i}"),
)
for i in range(case_count)
),
)
@pytest.mark.asyncio
async def test_get_returns_requested_revision_and_defaults_to_latest(
lens_db: Prisma, dataset_ids: tuple[str, ...]
) -> None:
repo: Final = _repo(lens_db)
dataset_id: Final = dataset_ids[0]
revisions: Final = tuple(_dataset(dataset_id, revision, revision) for revision in (1, 3, 2))
assert [await repo.insert(dataset, SAVED_AT) for dataset in revisions] == [True, True, True]
assert await repo.get(dataset_id, revision=1) == revisions[0]
assert await repo.get(dataset_id, revision=2) == revisions[2]
assert await repo.get(dataset_id) == revisions[1]
@pytest.mark.asyncio
async def test_get_unknown_dataset_or_revision_returns_none(lens_db: Prisma, dataset_ids: tuple[str, ...]) -> None:
repo: Final = _repo(lens_db)
assert await repo.insert(_dataset(dataset_ids[0], 1, 1), SAVED_AT)
assert await repo.get(dataset_ids[1]) is None
assert await repo.get(dataset_ids[1], revision=1) is None
assert await repo.get(dataset_ids[0], revision=2) is None
@pytest.mark.asyncio
async def test_inserting_an_existing_revision_is_rejected_and_keeps_the_first(
lens_db: Prisma, dataset_ids: tuple[str, ...]
) -> None:
repo: Final = _repo(lens_db)
original: Final = _dataset(dataset_ids[0], 1, 1)
overwrite: Final = _dataset(dataset_ids[0], 1, 4).model_copy(update={"name": "Overwritten"})
assert await repo.insert(original, SAVED_AT) is True
assert await repo.insert(overwrite, SAVED_AT + timedelta(days=1)) is False
assert await repo.get(dataset_ids[0], revision=1) == original
summaries: Final = tuple(s for s in await repo.summaries() if s.summary.id == dataset_ids[0])
assert tuple(s.summary.updated_at for s in summaries) == (SAVED_AT,)
@pytest.mark.asyncio
async def test_summaries_list_each_dataset_once_at_its_latest_revision_newest_first(
lens_db: Prisma, dataset_ids: tuple[str, ...]
) -> None:
repo: Final = _repo(lens_db)
older, newer, single = dataset_ids
writes: Final = (
(_dataset(older, 1, 1, "team-a"), SAVED_AT),
(_dataset(older, 2, 3, "team-a"), SAVED_AT + timedelta(minutes=1)),
(_dataset(newer, 1, 5, "team-b"), SAVED_AT + timedelta(minutes=2)),
(_dataset(newer, 2, 2, "team-b"), SAVED_AT + timedelta(minutes=4)),
(_dataset(single, 1, 0, ""), SAVED_AT + timedelta(minutes=3)),
)
assert [await repo.insert(dataset, saved_at) for dataset, saved_at in writes] == [True] * len(writes)
summaries: Final = tuple(s for s in await repo.summaries() if s.summary.id in dataset_ids)
assert summaries == (
StoredSummary(
team_id="team-b",
summary=DatasetSummary(
id=newer,
name="Dataset r2",
agent_name="support-agent",
revision=2,
case_count=2,
updated_at=SAVED_AT + timedelta(minutes=4),
),
),
StoredSummary(
team_id="",
summary=DatasetSummary(
id=single,
name="Dataset r1",
agent_name="support-agent",
revision=1,
case_count=0,
updated_at=SAVED_AT + timedelta(minutes=3),
),
),
StoredSummary(
team_id="team-a",
summary=DatasetSummary(
id=older,
name="Dataset r2",
agent_name="support-agent",
revision=2,
case_count=3,
updated_at=SAVED_AT + timedelta(minutes=1),
),
),
)