Skip to content

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

fix(dataset-runs): implement optimistic concurrency handling for dataset run creation in POST /dataset-run-items (#8494)

marliessophie8245888e3cae
diff --git a/web/src/features/public-api/server/dataset-runs.ts b/web/src/features/public-api/server/dataset-runs.ts
index 58a87515cc..3e034e35f3 100644
--- a/web/src/features/public-api/server/dataset-runs.ts
+++ b/web/src/features/public-api/server/dataset-runs.ts
@@ -14,6 +14,19 @@ const isUniqueConstraintError = (error: any): boolean => {
);
};
+/**
+ * Create or fetch a dataset run with optimistic concurrency handling.
+ *
+ * Behavior:
+ * - First tries to find an existing run by (projectId, datasetId, name).
+ * - If not found, attempts to create it.
+ * - If creation fails due to a unique constraint (likely created concurrently),
+ * fetches and returns the existing run.
+ * - If all steps fail, throws an error.
+ *
+ * Rationale: The public API can receive many POST requests almost simultaneously,
+ * which is not concurrency-safe without this guard.
+ */
export const createOrFetchDatasetRun = async ({
projectId,
datasetId,
@@ -28,7 +41,21 @@ export const createOrFetchDatasetRun = async ({
metadata?: Json | null;
}) => {
try {
- // Attempt optimistic creation
+ // Attempt to fetch existing run
+ const existingRun = await prisma.datasetRuns.findUnique({
+ where: {
+ datasetId_projectId_name: {
+ datasetId,
+ projectId,
+ name: name,
+ },
+ },
+ });
+ if (existingRun) {
+ return existingRun;
+ }
+
+ // Attempt creation
const datasetRun = await prisma.datasetRuns.create({
data: {
id: v4(),