Skip to content

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

fix(prompts-api): handle unique constraint errors (#5534)

Hassieb Pakzad8b7b3570e893
diff --git a/web/src/features/prompts/server/handlers/promptsHandler.ts b/web/src/features/prompts/server/handlers/promptsHandler.ts
index cb5a020ae1..7cfd14bf61 100644
--- a/web/src/features/prompts/server/handlers/promptsHandler.ts
+++ b/web/src/features/prompts/server/handlers/promptsHandler.ts
@@ -10,6 +10,7 @@ import { withMiddlewares } from "@/src/features/public-api/server/withMiddleware
import { prisma } from "@langfuse/shared/src/db";
import { authorizePromptRequestOrThrow } from "../utils/authorizePromptRequest";
import { RateLimitService } from "@/src/features/public-api/server/RateLimitService";
+import { InvalidRequestError } from "@langfuse/shared";
const getPromptsHandler = async (req: NextApiRequest, res: NextApiResponse) => {
const authCheck = await authorizePromptRequestOrThrow(req);
@@ -54,6 +55,18 @@ const postPromptsHandler = async (
projectId: authCheck.scope.projectId,
createdBy: "API",
prisma: prisma,
+ }).catch((err) => {
+ if (
+ typeof err === "object" &&
+ err.constructor.name === "PrismaClientKnownRequestError" &&
+ err.code === "P2002" // Unique constraint failed: https://www.prisma.io/docs/orm/reference/error-reference#p2002
+ ) {
+ throw new InvalidRequestError(
+ `Failed to create prompt '${input.name}' due to unique constraint failure. This is likely due to too many concurrent prompt creations for this prompt name. Please add a delay.`,
+ );
+ }
+
+ throw err;
});
return res.status(201).json(createdPrompt);