Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(annotation): gracefully handle deleted parent objects for annotation queue items (#10417)

marliessophie9e4214046e93
diff --git a/web/src/components/ui/object-not-found-card.tsx b/web/src/components/ui/object-not-found-card.tsx
new file mode 100644
index 0000000000..4286160099
--- /dev/null
+++ b/web/src/components/ui/object-not-found-card.tsx
@@ -0,0 +1,17 @@
+import { Card } from "@/src/components/ui/card";
+import { SearchXIcon } from "lucide-react";
+
+export const ObjectNotFoundCard = ({
+ type,
+}: {
+ type: "TRACE" | "OBSERVATION" | "SESSION";
+}) => (
+ <Card className="flex h-full items-center justify-center p-6">
+ <div className="text-center">
+ <SearchXIcon className="mx-auto mb-2 h-8 w-8 text-muted-foreground" />
+ <p className="text-sm capitalize text-muted-foreground">
+ {type.toLowerCase()} not found. Likely deleted.
+ </p>
+ </div>
+ </Card>
+);
diff --git a/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx b/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
index 255f5e75c4..d20fc7f992 100644
--- a/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
+++ b/web/src/features/annotation-queues/components/AnnotationQueueItemPage.tsx
@@ -15,6 +15,7 @@ import { useAnnotationQueueData } from "./shared/hooks/useAnnotationQueueData";
import { useAnnotationObjectData } from "./shared/hooks/useAnnotationObjectData";
import { TraceAnnotationProcessor } from "./processors/TraceAnnotationProcessor";
import { SessionAnnotationProcessor } from "./processors/SessionAnnotationProcessor";
+import { ObjectNotFoundCard } from "@/src/components/ui/object-not-found-card";
export const AnnotationQueueItemPage: React.FC<{
annotationQueueId: string;
@@ -177,6 +178,16 @@ export const AnnotationQueueItemPage: React.FC<{
};
const renderContent = () => {
+ // Handle deleted object (trace/observation/session not found)
+ if (objectData.isError && objectData.errorCode === "NOT_FOUND") {
+ return (
+ <ObjectNotFoundCard
+ type={relevantItem?.objectType ?? AnnotationQueueObjectType.TRACE}
+ />
+ );
+ }
+
+ // Handle deleted queue item
if (!relevantItem) {
return (
<Card className="flex h-full w-full flex-col items-center justify-center overflow-hidden">
@@ -258,7 +269,9 @@ export const AnnotationQueueItemPage: React.FC<{
onClick={handleComplete}
size="lg"
className="w-full"
- disabled={completeMutation.isPending || !hasAccess}
+ disabled={
+ completeMutation.isPending || !hasAccess || objectData.isError
+ }
>
{isSingleItem || progressIndex + 1 === totalItems
? "Complete"
diff --git a/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts b/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
index 7ce4c2ed34..d183c6ac3a 100644
--- a/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
+++ b/web/src/features/annotation-queues/components/shared/hooks/useAnnotationObjectData.ts
@@ -7,6 +7,8 @@ import {
export interface ObjectDataHook<TData> {
data: TData | undefined;
isLoading: boolean;
+ isError: boolean;
+ errorCode?: string;
}
export const useAnnotationObjectData = (
@@ -23,7 +25,11 @@ export const useAnnotationObjectData = (
(item.objectType === AnnotationQueueObjectType.TRACE ||
item.objectType === AnnotationQueueObjectType.OBSERVATION),
retry(failureCount, error) {
- if (error.data?.code === "UNAUTHORIZED") return false;
+ if (
+ error.data?.code === "UNAUTHORIZED" ||
+ error.data?.code === "NOT_FOUND"
+ )
+ return false;
return failureCount < 3;
},
},
@@ -51,6 +57,7 @@ export const useAnnotationObjectData = (
return {
data: undefined,
isLoading: false,
+ isError: false,
};
}
@@ -60,11 +67,15 @@ export const useAnnotationObjectData = (
return {
data: traceQuery.data,
isLoading: traceQuery.isLoading,
+ isError: traceQuery.isError,
+ errorCode: traceQuery.error?.data?.code,
};
case AnnotationQueueObjectType.SESSION:
return {
data: sessionQuery.data,
isLoading: sessionQuery.isLoading,
+ isError: sessionQuery.isError,
+ errorCode: sessionQuery.error?.data?.code,
};
default:
// This should never happen as it's a DB enum
diff --git a/web/src/features/annotation-queues/server/annotationQueueItemsRouter.ts b/web/src/features/annotation-queues/server/annotationQueueItemsRouter.ts
index 7a9aee3997..9a893eee0f 100644
--- a/web/src/features/annotation-queues/server/annotationQueueItemsRouter.ts
+++ b/web/src/features/annotation-queues/server/annotationQueueItemsRouter.ts
@@ -14,6 +14,7 @@ import {
BatchActionType,
BatchExportTableName,
type BatchTableNames,
+ LangfuseNotFoundError,
paginationZod,
Prisma,
} from "@langfuse/shared";
@@ -81,62 +82,56 @@ export const queueItemRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "annotationQueues:read",
+ });
+
+ const item = await ctx.prisma.annotationQueueItem.findUnique({
+ where: {
+ id: input.itemId,
projectId: input.projectId,
- scope: "annotationQueues:read",
- });
+ },
+ });
- const item = await ctx.prisma.annotationQueueItem.findUnique({
+ // Expected behavior, non-error case: if user has seen item in given session, prior to it being deleted, we return null
+ if (!item) return null;
+ let lockedByUser: { name: string | null } | null = null;
+
+ if (isItemLocked(item)) {
+ lockedByUser = await ctx.prisma.user.findUnique({
where: {
- id: input.itemId,
- projectId: input.projectId,
+ id: item.lockedByUserId as string,
+ },
+ select: {
+ name: true,
},
});
+ }
- // Expected behavior, non-error case: if user has seen item in given session, prior to it being deleted, we return null
- if (!item) return null;
- let lockedByUser: { name: string | null } | null = null;
-
- if (isItemLocked(item)) {
- lockedByUser = await ctx.prisma.user.findUnique({
- where: {
- id: item.lockedByUserId as string,
- },
- select: {
- name: true,
- },
- });
- }
+ const inflatedItem = {
+ ...item,
+ lockedByUser,
+ };
- const inflatedItem = {
- ...item,
- lockedByUser,
- };
+ if (item.objectType === AnnotationQueueObjectType.OBSERVATION) {
+ const clickhouseObservation = await getObservationById({
+ id: item.objectId,
+ projectId: input.projectId,
+ });
- if (item.objectType === AnnotationQueueObjectType.OBSERVATION) {
- const clickhouseObservation = await getObservationById({
- id: item.objectId,
- projectId: input.projectId,
- });
- return {
- ...inflatedItem,
- parentTraceId: clickhouseObservation?.traceId,
- };
+ if (!clickhouseObservation) {
+ throw new LangfuseNotFoundError("Observation not found");
}
- return inflatedItem;
- } catch (error) {
- logger.error(error);
- if (error instanceof TRPCError) {
- throw error;
- }
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Fetching annotation queue item by id failed.",
- });
+ return {
+ ...inflatedItem,
+ parentTraceId: clickhouseObservation?.traceId,
+ };
}
+
+ return inflatedItem;
}),
itemsByQueueId: protectedProjectProcedure
.input(
@@ -147,26 +142,25 @@ export const queueItemRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "annotationQueues:read",
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "annotationQueues:read",
+ });
- const [queueItems, totalItems] = await Promise.all([
- await ctx.prisma.$queryRaw<
- Array<{
- id: string;
- status: AnnotationQueueStatus;
- objectId: string;
- objectType: AnnotationQueueObjectType;
- completedAt: string | null;
- annotatorUserId: string | null;
- annotatorUserImage: string | null;
- annotatorUserName: string | null;
- }>
- >(Prisma.sql`
+ const [queueItems, totalItems] = await Promise.all([
+ await ctx.prisma.$queryRaw<
+ Array<{
+ id: string;
+ status: AnnotationQueueStatus;
+ objectId: string;
+ objectType: AnnotationQueueObjectType;
+ completedAt: string | null;
+ annotatorUserId: string | null;
+ annotatorUserImage: string | null;
+ annotatorUserName: string | null;
+ }>
+ >(Prisma.sql`
SELECT
aqi.id,
aqi.status,
@@ -189,43 +183,33 @@ export const queueItemRouter = createTRPCRouter({
${input.limit ? Prisma.sql`LIMIT ${input.limit}` : Prisma.empty}
${input.page && input.limit ? Prisma.sql`OFFSET ${input.page * input.limit}` : Prisma.empty}
`),
- ctx.prisma.annotationQueueItem.count({
- where: {
- queueId: input.queueId,
- projectId: input.projectId,
- },
- }),
- ]);
+ ctx.prisma.annotationQueueItem.count({
+ where: {
+ queueId: input.queueId,
+ projectId: input.projectId,
+ },
+ }),
+ ]);
- const observationIds = queueItems
- .filter(
- (item) => item.objectType === AnnotationQueueObjectType.OBSERVATION,
- )
- .map((item) => item.objectId);
+ const observationIds = queueItems
+ .filter(
+ (item) => item.objectType === AnnotationQueueObjectType.OBSERVATION,
+ )
+ .map((item) => item.objectId);
- const traceIds =
- observationIds.length > 0
- ? await getTraceIdsForObservations(input.projectId, observationIds)
- : [];
+ const traceIds =
+ observationIds.length > 0
+ ? await getTraceIdsForObservations(input.projectId, observationIds)
+ : [];
- return {
- queueItems: queueItems.map((item) => ({
- ...item,
- parentTraceId:
- traceIds.find((t) => t.id === item.objectId)?.traceId || null,
- })),
- totalItems,
- };
- } catch (error) {
- logger.error(error);
- if (error instanceof TRPCError) {
- throw error;
- }
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Fetching annotation queue items failed.",
- });
- }
+ return {
+ queueItems: queueItems.map((item) => ({
+ ...item,
+ parentTraceId:
+ traceIds.find((t) => t.id === item.objectId)?.traceId || null,
+ })),
+ totalItems,
+ };
}),
unseenPendingItemCountByQueueId: protectedProjectProcedure
.input(
@@ -236,34 +220,23 @@ export const queueItemRouter = createTRPCRouter({
}),
)
.query(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "annotationQueues:read",
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "annotationQueues:read",
+ });
- const count = await ctx.prisma.annotationQueueItem.count({
- where: {
- queueId: input.queueId,
- projectId: input.projectId,
- status: AnnotationQueueStatus.PENDING,
- id: {
- notIn: input.seenItemIds,
- },
+ const count = await ctx.prisma.annotationQueueItem.count({
+ where: {
+ queueId: input.queueId,
+ projectId: input.projectId,
+ status: AnnotationQueueStatus.PENDING,
+ id: {
+ notIn: input.seenItemIds,
},
- });
- return count;
- } catch (error) {
- logger.error(error);
- if (error instanceof TRPCError) {
- throw error;
- }
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Fetching unseen pending item count by queueId failed.",
- });
- }
+ },
+ });
+ return count;
}),
createMany: protectedProjectProcedure
.input(
@@ -279,98 +252,87 @@ export const queueItemRouter = createTRPCRouter({
}),
)
.mutation(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "annotationQueues:CUD",
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "annotationQueues:CUD",
+ });
- let createdCount = 0;
+ let createdCount = 0;
- if (input.isBatchAction && input.query) {
- const actionProps =
- MAP_OBJECT_TYPE_TO_ACTION_PROPS[
- input.objectType as "TRACE" | "SESSION"
- ];
- if (!actionProps) {
- throw new TRPCError({
- code: "BAD_REQUEST",
- message: `Batch action not supported for object type: ${input.objectType}`,
- });
- }
- const { actionId, tableName } = actionProps;
- await createBatchActionJob({
- projectId: input.projectId,
- actionId,
- actionType: BatchActionType.Create,
- tableName,
- session: ctx.session,
- query: input.query,
- targetId: input.queueId,
- });
- } else {
- const { count } = await ctx.prisma.annotationQueueItem.createMany({
- data: input.objectIds.map((objectId) => ({
- projectId: input.projectId,
- queueId: input.queueId,
- objectId,
- objectType: input.objectType,
- })),
- skipDuplicates: true,
- });
- createdCount = count;
-
- const createdItems = await ctx.prisma.annotationQueueItem.findMany({
- where: {
- projectId: input.projectId,
- queueId: input.queueId,
- objectId: { in: input.objectIds },
- objectType: input.objectType,
- },
- orderBy: { createdAt: "desc" },
+ if (input.isBatchAction && input.query) {
+ const actionProps =
+ MAP_OBJECT_TYPE_TO_ACTION_PROPS[
+ input.objectType as "TRACE" | "SESSION"
+ ];
+ if (!actionProps) {
+ throw new TRPCError({
+ code: "BAD_REQUEST",
+ message: `Batch action not supported for object type: ${input.objectType}`,
});
-
- for (const item of createdItems) {
- await auditLog(
- {
- session: ctx.session,
- resourceType: "annotationQueueItem",
- resourceId: item.id,
- action: "create",
- after: item,
- },
- ctx.prisma,
- );
- }
}
+ const { actionId, tableName } = actionProps;
+ await createBatchActionJob({
+ projectId: input.projectId,
+ actionId,
+ actionType: BatchActionType.Create,
+ tableName,
+ session: ctx.session,
+ query: input.query,
+ targetId: input.queueId,
+ });
+ } else {
+ const { count } = await ctx.prisma.annotationQueueItem.createMany({
+ data: input.objectIds.map((objectId) => ({
+ projectId: input.projectId,
+ queueId: input.queueId,
+ objectId,
+ objectType: input.objectType,
+ })),
+ skipDuplicates: true,
+ });
+ createdCount = count;
- const queue = await ctx.prisma.annotationQueue.findUnique({
+ const createdItems = await ctx.prisma.annotationQueueItem.findMany({
where: {
- id: input.queueId,
projectId: input.projectId,
+ queueId: input.queueId,
+ objectId: { in: input.objectIds },
+ objectType: input.objectType,
},
- select: {
- name: true,
- id: true,
- },
+ orderBy: { createdAt: "desc" },
});
- return {
- createdCount,
- queueName: queue?.name,
- queueId: queue?.id,
- };
- } catch (error) {
- logger.error(error);
- if (error instanceof TRPCError) {
- throw error;
+ for (const item of createdItems) {
+ await auditLog(
+ {
+ session: ctx.session,
+ resourceType: "annotationQueueItem",
+ resourceId: item.id,
+ action: "create",
+ after: item,
+ },
+ ctx.prisma,
+ );
}
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Creating multiple annotation queue items failed.",
- });
}
+
+ const queue = await ctx.prisma.annotationQueue.findUnique({
+ where: {
+ id: input.queueId,
+ projectId: input.projectId,
+ },
+ select: {
+ name: true,
+ id: true,
+ },
+ });
+
+ return {
+ createdCount,
+ queueName: queue?.name,
+ queueId: queue?.id,
+ };
}),
deleteMany: protectedProjectProcedure
.input(
@@ -380,54 +342,43 @@ export const queueItemRouter = createTRPCRouter({
}),
)
.mutation(async ({ input, ctx }) => {
- try {
- throwIfNoProjectAccess({
- session: ctx.session,
- projectId: input.projectId,
- scope: "annotationQueues:CUD",
- });
+ throwIfNoProjectAccess({
+ session: ctx.session,
+ projectId: input.projectId,
+ scope: "annotationQueues:CUD",
+ });
- const items = await ctx.prisma.annotationQueueItem.findMany({
- where: {
- id: {
- in: input.itemIds,
- },
- projectId: input.projectId,
+ const items = await ctx.prisma.annotationQueueItem.findMany({
+ where: {
+ id: {
+ in: input.itemIds,
},
- });
+ projectId: input.projectId,
+ },
+ });
- for (const item of items) {
- await auditLog({
- resourceType: "annotationQueueItem",
- resourceId: item.id,
- before: item,
- action: "delete",
- session: ctx.session,
- });
- }
+ for (const item of items) {
+ await auditLog({
+ resourceType: "annotationQueueItem",
+ resourceId: item.id,
+ before: item,
+ action: "delete",
+ session: ctx.session,
+ });
+ }
- const { count } = await ctx.prisma.annotationQueueItem.deleteMany({
- where: {
- id: {
- in: input.itemIds,
- },
- projectId: input.projectId,
+ const { count } = await ctx.prisma.annotationQueueItem.deleteMany({
+ where: {
+ id: {
+ in: input.itemIds,
},
- });
+ projectId: input.projectId,
+ },
+ });
- return {
- deletedCount: count,
- };
- } catch (error) {
- logger.error(error);
- if (error instanceof TRPCError) {
- throw error;
- }
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Deleting annotation queue items failed.",
- });
- }
+ return {
+ deletedCount: count,
+ };
}),
complete: protectedProjectProcedure
.input(
@@ -481,10 +432,7 @@ export const queueItemRouter = createTRPCRouter({
"The item to complete was not found, it was likely deleted.",
});
}
- throw new TRPCError({
- code: "INTERNAL_SERVER_ERROR",
- message: "Completing annotation queue item failed.",
- });
+ throw error;
}
}),
});