Skip to content

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

fix(media): increase retries (#4474)

Hassieb Pakzad6e290b9ad470
diff --git a/web/src/pages/api/public/media/index.ts b/web/src/pages/api/public/media/index.ts
index 6af1eaf2e0..22b3ed0f8f 100644
--- a/web/src/pages/api/public/media/index.ts
+++ b/web/src/pages/api/public/media/index.ts
@@ -17,6 +17,7 @@ import {
Prisma,
} from "@langfuse/shared";
import { prisma } from "@langfuse/shared/src/db";
+import { instrumentAsync } from "@langfuse/shared/src/server";
import { logger } from "@langfuse/shared/src/server";
export default withMiddlewares({
@@ -45,198 +46,210 @@ export default withMiddlewares({
// Retry up to 3 times to avoid race conditions for concurrent media uploads
// Multiple observations can reference the same media, so we need to make sure no deadlock occurs
- const MAX_RETRIES = 3;
+ const MAX_RETRIES = 6;
let retries = 0;
- while (retries < MAX_RETRIES) {
- try {
- return await prisma.$transaction<{
- mediaId: string;
- uploadUrl: string | null;
- }>(
- async (tx) => {
- const existingMedia = await prisma.media.findUnique({
- where: {
- projectId_sha256Hash: {
- projectId,
- sha256Hash,
- },
- },
- });
+ return await instrumentAsync(
+ { name: "media-create-upload-url" },
+ async (span) => {
+ span.setAttribute("projectId", projectId);
+ span.setAttribute("traceId", traceId);
+ span.setAttribute("observationId", observationId ?? "");
+ span.setAttribute("field", field);
- if (
- existingMedia &&
- existingMedia.uploadHttpStatus === 200 &&
- existingMedia.contentType === contentType
- ) {
- if (observationId) {
- await tx.observationMedia.upsert({
- where: {
- projectId_traceId_observationId_mediaId_field: {
- projectId,
- traceId,
- observationId,
- mediaId: existingMedia.id,
- field,
- },
- },
- update: {},
- create: {
- projectId,
- traceId,
- observationId,
- mediaId: existingMedia.id,
- field,
- },
- });
- } else {
- await tx.traceMedia.upsert({
+ while (retries < MAX_RETRIES) {
+ try {
+ return await prisma.$transaction<{
+ mediaId: string;
+ uploadUrl: string | null;
+ }>(
+ async (tx) => {
+ const existingMedia = await prisma.media.findUnique({
where: {
- projectId_traceId_mediaId_field: {
+ projectId_sha256Hash: {
projectId,
- traceId,
- mediaId: existingMedia.id,
- field,
+ sha256Hash,
},
},
- update: {},
- create: {
- projectId,
- traceId,
- field,
- mediaId: existingMedia.id,
- },
});
- }
-
- return {
- mediaId: existingMedia.id,
- uploadUrl: null,
- };
- }
- const mediaId = existingMedia?.id ?? randomUUID();
+ if (
+ existingMedia &&
+ existingMedia.uploadHttpStatus === 200 &&
+ existingMedia.contentType === contentType
+ ) {
+ if (observationId) {
+ await tx.observationMedia.upsert({
+ where: {
+ projectId_traceId_observationId_mediaId_field: {
+ projectId,
+ traceId,
+ observationId,
+ mediaId: existingMedia.id,
+ field,
+ },
+ },
+ update: {},
+ create: {
+ projectId,
+ traceId,
+ observationId,
+ mediaId: existingMedia.id,
+ field,
+ },
+ });
+ } else {
+ await tx.traceMedia.upsert({
+ where: {
+ projectId_traceId_mediaId_field: {
+ projectId,
+ traceId,
+ mediaId: existingMedia.id,
+ field,
+ },
+ },
+ update: {},
+ create: {
+ projectId,
+ traceId,
+ field,
+ mediaId: existingMedia.id,
+ },
+ });
+ }
- if (
- !(
- env.LANGFUSE_S3_MEDIA_UPLOAD_ENABLED === "true" &&
- env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET
- )
- )
- throw new InternalServerError(
- "Media upload to blob storage not enabled or no bucket configured",
- );
+ return {
+ mediaId: existingMedia.id,
+ uploadUrl: null,
+ };
+ }
- const s3Client = getMediaStorageServiceClient(
- env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
- );
+ const mediaId = existingMedia?.id ?? randomUUID();
+ span.setAttribute("mediaId", mediaId);
- const bucketPath = getBucketPath({
- projectId,
- mediaId,
- contentType,
- });
+ if (
+ !(
+ env.LANGFUSE_S3_MEDIA_UPLOAD_ENABLED === "true" &&
+ env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET
+ )
+ )
+ throw new InternalServerError(
+ "Media upload to blob storage not enabled or no bucket configured",
+ );
- const uploadUrl = await s3Client.getSignedUploadUrl({
- path: bucketPath,
- ttlSeconds: 60 * 60, // 1 hour
- sha256Hash,
- contentType,
- contentLength,
- });
+ const s3Client = getMediaStorageServiceClient(
+ env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
+ );
- await Promise.all([
- tx.media.upsert({
- where: {
- projectId_sha256Hash: {
- projectId,
- sha256Hash,
- },
- },
- update: {
- bucketName: env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
- bucketPath,
- contentType,
- contentLength,
- },
- create: {
- id: mediaId,
+ const bucketPath = getBucketPath({
projectId,
+ mediaId,
+ contentType,
+ });
+
+ const uploadUrl = await s3Client.getSignedUploadUrl({
+ path: bucketPath,
+ ttlSeconds: 60 * 60, // 1 hour
sha256Hash,
- bucketPath,
- bucketName: env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
contentType,
contentLength,
- },
- }),
- observationId
- ? tx.observationMedia.upsert({
+ });
+
+ await Promise.all([
+ tx.media.upsert({
where: {
- projectId_traceId_observationId_mediaId_field: {
+ projectId_sha256Hash: {
projectId,
- traceId,
- observationId,
- mediaId,
- field,
+ sha256Hash,
},
},
- update: {},
- create: {
- projectId,
- traceId,
- observationId,
- mediaId,
- field,
+ update: {
+ bucketName: env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
+ bucketPath,
+ contentType,
+ contentLength,
},
- })
- : tx.traceMedia.upsert({
- where: {
- projectId_traceId_mediaId_field: {
- projectId,
- traceId,
- mediaId,
- field,
- },
- },
- update: {},
create: {
+ id: mediaId,
projectId,
- traceId,
- field,
- mediaId,
+ sha256Hash,
+ bucketPath,
+ bucketName: env.LANGFUSE_S3_MEDIA_UPLOAD_BUCKET,
+ contentType,
+ contentLength,
},
}),
- ]);
+ observationId
+ ? tx.observationMedia.upsert({
+ where: {
+ projectId_traceId_observationId_mediaId_field: {
+ projectId,
+ traceId,
+ observationId,
+ mediaId,
+ field,
+ },
+ },
+ update: {},
+ create: {
+ projectId,
+ traceId,
+ observationId,
+ mediaId,
+ field,
+ },
+ })
+ : tx.traceMedia.upsert({
+ where: {
+ projectId_traceId_mediaId_field: {
+ projectId,
+ traceId,
+ mediaId,
+ field,
+ },
+ },
+ update: {},
+ create: {
+ projectId,
+ traceId,
+ field,
+ mediaId,
+ },
+ }),
+ ]);
- return {
- mediaId,
- uploadUrl,
- };
- },
- { isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
- );
- } catch (error) {
+ return {
+ mediaId,
+ uploadUrl,
+ };
+ },
+ {
+ isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
+ },
+ );
+ } catch (error) {
+ logger.error(
+ `Failed to get media upload URL for trace ${traceId} and observation ${observationId}. Retrying...`,
+ error,
+ );
+ if (
+ // See https://www.prisma.io/docs/orm/prisma-client/queries/transactions#transaction-timing-issues
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ error.code === "P2034"
+ ) {
+ retries++;
+ continue;
+ }
+
+ throw error;
+ }
+ }
logger.error(
- `Failed to get media upload URL for trace ${traceId} and observation ${observationId}. Retrying...`,
- error,
+ `Failed to get media upload URL for trace ${traceId} and observation ${observationId}.`,
);
- if (
- // See https://www.prisma.io/docs/orm/prisma-client/queries/transactions#transaction-timing-issues
- error instanceof Prisma.PrismaClientKnownRequestError &&
- error.code === "P2034"
- ) {
- retries++;
- continue;
- }
-
- throw error;
- }
- }
-
- logger.error(
- `Failed to get media upload URL for trace ${traceId} and observation ${observationId}.`,
+ throw new InternalServerError("Failed to get media upload URL");
+ },
);
- throw new InternalServerError("Failed to get media upload URL");
},
}),
});