Skip to content

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

fix(dataset-items): handle version conflict errors in dataset item upsert (#13031)

marliessophie53869c72004c
diff --git a/web/src/pages/api/public/dataset-items/index.ts b/web/src/pages/api/public/dataset-items/index.ts
index 7966a0195c..897a590a2e 100644
--- a/web/src/pages/api/public/dataset-items/index.ts
+++ b/web/src/pages/api/public/dataset-items/index.ts
@@ -8,7 +8,11 @@ import {
PostDatasetItemsV1Response,
transformDbDatasetItemDomainToAPIDatasetItem,
} from "@/src/features/public-api/types/datasets";
-import { LangfuseNotFoundError, Prisma } from "@langfuse/shared";
+import {
+ LangfuseConflictError,
+ LangfuseNotFoundError,
+ Prisma,
+} from "@langfuse/shared";
import {
createDatasetItemFilterState,
getDatasetItems,
@@ -82,12 +86,23 @@ export default withMiddlewares({
// When this constraint is violated, the database will upsert based on (id, projectId, datasetId).
// If this record does not exist, the database will throw an error.
logger.warn(
- `Failed to upsert dataset item. Dataset item ${id} in project ${auth.scope.projectId} already exists for a different dataset than ${datasetName}`,
+ `Failed to upsert dataset item. Dataset item ${id} already exists for a different dataset than ${datasetName}`,
);
throw new LangfuseNotFoundError(
`The dataset item with id ${id} already exists in a dataset other than ${datasetName}`,
);
}
+ if (e.code === "P2002") {
+ // Unique constraint violation on (id, projectId, validFrom).
+ // This can happen when concurrent requests try to update the same dataset item
+ // and create versions with the same timestamp.
+ logger.warn(
+ `Failed to upsert dataset item due to version conflict. Dataset item ${id} was modified concurrently.`,
+ );
+ throw new LangfuseConflictError(
+ `Dataset item ${id ?? "new"} was modified concurrently. Please retry the request.`,
+ );
+ }
}
throw e;
}