Skip to content

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

fix: fix pagination on public api (#4710)

Max Deichmann61240d7c92a8
diff --git a/packages/shared/src/features/scores/scoreConfigTypes.ts b/packages/shared/src/features/scores/scoreConfigTypes.ts
index 429edd88e1..5060cd3fca 100644
--- a/packages/shared/src/features/scores/scoreConfigTypes.ts
+++ b/packages/shared/src/features/scores/scoreConfigTypes.ts
@@ -6,7 +6,7 @@ import { isPresent } from "../../utils/typeChecks";
import {
jsonSchema,
paginationMetaResponseZod,
- paginationZod,
+ publicApiPaginationZod,
} from "../../utils/zod";
/**
@@ -17,7 +17,7 @@ export type ValidatedScoreConfig = z.infer<typeof ValidatedScoreConfigSchema>;
const validateCategories = (
categories: ConfigCategory[],
- ctx: z.RefinementCtx
+ ctx: z.RefinementCtx,
) => {
const uniqueNames = new Set<string>();
const uniqueValues = new Set<number>();
@@ -94,7 +94,7 @@ const BooleanScoreConfig = z.object({
return categories.every(
(category, index) =>
category.label === expectedCategories[index].label &&
- category.value === expectedCategories[index].value
+ category.value === expectedCategories[index].value,
);
}),
});
@@ -123,7 +123,7 @@ const ValidatedScoreConfigSchema = z
minValue: z.undefined().nullish(),
dataType: z.literal("CATEGORICAL"),
categories: Categories.superRefine(validateCategories),
- })
+ }),
),
ScoreConfigBase.merge(BooleanScoreConfig),
])
@@ -150,7 +150,7 @@ const ValidatedScoreConfigSchema = z
*/
export const filterAndValidateDbScoreConfigList = (
scoreConfigs: ScoreConfigDbType[],
- onParseError?: (error: z.ZodError) => void
+ onParseError?: (error: z.ZodError) => void,
): ValidatedScoreConfig[] =>
scoreConfigs.reduce((acc, ts) => {
const result = ValidatedScoreConfigSchema.safeParse(ts);
@@ -170,7 +170,7 @@ export const filterAndValidateDbScoreConfigList = (
* @throws error if score fails validation
*/
export const validateDbScoreConfig = (
- scoreConfig: ScoreConfigDbType
+ scoreConfig: ScoreConfigDbType,
): ValidatedScoreConfig => ValidatedScoreConfigSchema.parse(scoreConfig);
/**
@@ -205,7 +205,7 @@ export const PostScoreConfigBody = z
z.object({
dataType: z.literal("BOOLEAN"),
categories: z.undefined().nullish(),
- })
+ }),
),
])
.superRefine((data, ctx) => {
@@ -227,7 +227,7 @@ export const PostScoreConfigResponse = ValidatedScoreConfigSchema;
// GET /score-configs
export const GetScoreConfigsQuery = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
});
export const GetScoreConfigsResponse = z.object({
diff --git a/packages/shared/src/features/scores/scoreTypes.ts b/packages/shared/src/features/scores/scoreTypes.ts
index e103b037b9..b618aa7ff9 100644
--- a/packages/shared/src/features/scores/scoreTypes.ts
+++ b/packages/shared/src/features/scores/scoreTypes.ts
@@ -6,7 +6,7 @@ import { isPresent, stringDateTime } from "../../utils/typeChecks";
import {
NonEmptyString,
paginationMetaResponseZod,
- paginationZod,
+ publicApiPaginationZod,
} from "../../utils/zod";
import { Category as ConfigCategory } from "./scoreConfigTypes";
@@ -84,13 +84,13 @@ export const ScoreBodyWithoutConfig = z.discriminatedUnion("dataType", [
z.object({
value: z.number(),
dataType: z.literal("NUMERIC"),
- })
+ }),
),
BaseScoreBody.merge(
z.object({
value: z.string(),
dataType: z.literal("CATEGORICAL"),
- })
+ }),
),
BaseScoreBody.merge(
z.object({
@@ -98,7 +98,7 @@ export const ScoreBodyWithoutConfig = z.discriminatedUnion("dataType", [
message: "Value must be either 0 or 1",
}),
dataType: z.literal("BOOLEAN"),
- })
+ }),
),
]);
@@ -162,7 +162,7 @@ export const ScorePropsAgainstConfig = z.union([
*/
export const filterAndValidateDbScoreList = (
scores: Score[],
- onParseError?: (error: z.ZodError) => void
+ onParseError?: (error: z.ZodError) => void,
): APIScore[] =>
scores.reduce((acc, ts) => {
const result = APIScoreSchema.safeParse(ts);
@@ -199,14 +199,14 @@ export const PostScoresBody = z.discriminatedUnion("dataType", [
value: z.number(),
dataType: z.literal("NUMERIC"),
configId: z.string().nullish(),
- })
+ }),
),
BaseScoreBody.merge(
z.object({
value: z.string(),
dataType: z.literal("CATEGORICAL"),
configId: z.string().nullish(),
- })
+ }),
),
BaseScoreBody.merge(
z.object({
@@ -216,14 +216,14 @@ export const PostScoresBody = z.discriminatedUnion("dataType", [
}),
dataType: z.literal("BOOLEAN"),
configId: z.string().nullish(),
- })
+ }),
),
BaseScoreBody.merge(
z.object({
value: z.union([z.string(), z.number()]),
dataType: z.undefined(),
configId: z.string().nullish(),
- })
+ }),
),
]);
@@ -231,7 +231,7 @@ export const PostScoresResponse = z.object({ id: z.string() });
// GET /scores
export const GetScoresQuery = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
userId: z.string().nullish(),
dataType: z.enum(ScoreDataType).nullish(),
configId: z.string().nullish(),
@@ -260,7 +260,7 @@ const LegacyGetScoreResponseDataV1 = z.intersection(
userId: z.string().nullish(),
tags: z.array(z.string()).nullish(),
}),
- })
+ }),
);
export const GetScoresResponse = z.object({
data: z.array(LegacyGetScoreResponseDataV1),
@@ -269,7 +269,7 @@ export const GetScoresResponse = z.object({
export const legacyFilterAndValidateV1GetScoreList = (
scores: unknown[],
- onParseError?: (error: z.ZodError) => void
+ onParseError?: (error: z.ZodError) => void,
): z.infer<typeof LegacyGetScoreResponseDataV1>[] =>
scores.reduce(
(acc: z.infer<typeof LegacyGetScoreResponseDataV1>[], ts) => {
@@ -282,7 +282,7 @@ export const legacyFilterAndValidateV1GetScoreList = (
}
return acc;
},
- [] as z.infer<typeof LegacyGetScoreResponseDataV1>[]
+ [] as z.infer<typeof LegacyGetScoreResponseDataV1>[],
);
// GET /scores/{scoreId}
diff --git a/packages/shared/src/utils/zod.ts b/packages/shared/src/utils/zod.ts
index 421b52113b..dcbdc52479 100644
--- a/packages/shared/src/utils/zod.ts
+++ b/packages/shared/src/utils/zod.ts
@@ -49,6 +49,17 @@ export const paginationZod = {
),
};
+export const publicApiPaginationZod = {
+ page: z.preprocess(
+ (x) => (x === "" ? undefined : x),
+ z.coerce.number().gt(0).default(1),
+ ),
+ limit: z.preprocess(
+ (x) => (x === "" ? undefined : x),
+ z.coerce.number().lte(100).default(50),
+ ),
+};
+
export const optionalPaginationZod = {
page: z
.preprocess((x) => (x === "" ? undefined : x), z.coerce.number())
diff --git a/web/src/__tests__/async/traces-api.servertest.ts b/web/src/__tests__/async/traces-api.servertest.ts
index 94d18bbc55..ec53a4e093 100644
--- a/web/src/__tests__/async/traces-api.servertest.ts
+++ b/web/src/__tests__/async/traces-api.servertest.ts
@@ -3,7 +3,10 @@ import {
createObservationsCh,
createTracesCh,
} from "@langfuse/shared/src/server";
-import { makeZodVerifiedAPICall } from "@/src/__tests__/test-utils";
+import {
+ makeZodVerifiedAPICall,
+ makeZodVerifiedAPICallSilent,
+} from "@/src/__tests__/test-utils";
import {
GetTracesV1Response,
GetTraceV1Response,
@@ -258,4 +261,13 @@ describe("/api/public/traces API Endpoint", () => {
const trace2 = traces.body.data[1];
expect(trace2.name).toBe("trace-name1");
});
+ it("should return 400 error when page=0", async () => {
+ const response = await makeZodVerifiedAPICallSilent(
+ GetTracesV1Response,
+ "GET",
+ "/api/public/traces?page=0&limit=10",
+ );
+
+ expect(response.status).toBe(400);
+ });
});
diff --git a/web/src/features/public-api/types/comments.ts b/web/src/features/public-api/types/comments.ts
index 7ee8838168..7f55110bd4 100644
--- a/web/src/features/public-api/types/comments.ts
+++ b/web/src/features/public-api/types/comments.ts
@@ -1,7 +1,7 @@
import {
CommentObjectType,
CreateCommentData,
- paginationZod,
+ publicApiPaginationZod,
} from "@langfuse/shared";
import { z } from "zod";
@@ -38,7 +38,7 @@ export const GetCommentsV1Query = z
objectType: z.nativeEnum(CommentObjectType).nullish(),
objectId: z.string().nullish(),
authorUserId: z.string().nullish(),
- ...paginationZod,
+ ...publicApiPaginationZod,
})
.strict()
.refine(
diff --git a/web/src/features/public-api/types/datasets.ts b/web/src/features/public-api/types/datasets.ts
index 1c15c2aa7b..79587b6fd3 100644
--- a/web/src/features/public-api/types/datasets.ts
+++ b/web/src/features/public-api/types/datasets.ts
@@ -1,5 +1,6 @@
import {
jsonSchema,
+ publicApiPaginationZod,
paginationZod,
paginationMetaResponseZod,
queryStringZod,
@@ -101,7 +102,7 @@ export const PostDatasetsV2Response = APIDataset.strict();
// GET /v2/datasets
export const GetDatasetsV2Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
});
export const GetDatasetsV2Response = z
.object({
@@ -119,7 +120,7 @@ export const GetDatasetV2Response = APIDataset.strict();
// GET /datasets/{name}/runs
export const GetDatasetRunsV1Query = z.object({
name: queryStringZod, // dataset name from URL, name as it is v1
- ...paginationZod,
+ ...publicApiPaginationZod,
});
export const GetDatasetRunsV1Response = z
.object({
@@ -155,7 +156,7 @@ export const GetDatasetItemsV1Query = z.object({
datasetName: z.string().nullish(),
sourceTraceId: z.string().nullish(),
sourceObservationId: z.string().nullish(),
- ...paginationZod,
+ ...publicApiPaginationZod,
});
export const GetDatasetItemsV1Response = z
.object({
diff --git a/web/src/features/public-api/types/metrics.ts b/web/src/features/public-api/types/metrics.ts
index f01a9b4a67..06a2afd5ed 100644
--- a/web/src/features/public-api/types/metrics.ts
+++ b/web/src/features/public-api/types/metrics.ts
@@ -1,4 +1,7 @@
-import { paginationMetaResponseZod, paginationZod } from "@langfuse/shared";
+import {
+ paginationMetaResponseZod,
+ publicApiPaginationZod,
+} from "@langfuse/shared";
import { stringDateTime } from "@langfuse/shared/src/server";
import { z } from "zod";
@@ -8,7 +11,7 @@ import { z } from "zod";
// Get /metrics/daily
export const GetMetricsDailyV1Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
traceName: z.string().nullish(),
userId: z.string().nullish(),
tags: z.union([z.array(z.string()), z.string()]).nullish(),
diff --git a/web/src/features/public-api/types/models.ts b/web/src/features/public-api/types/models.ts
index 76875d04b5..25f18ae21b 100644
--- a/web/src/features/public-api/types/models.ts
+++ b/web/src/features/public-api/types/models.ts
@@ -1,9 +1,9 @@
import {
type ModelUsageUnit as PrismaModelUsageUnit,
paginationMetaResponseZod,
- paginationZod,
type Model as PrismaModel,
jsonSchema,
+ publicApiPaginationZod,
} from "@langfuse/shared";
import { z } from "zod";
@@ -67,7 +67,7 @@ export function prismaToApiModelDefinition({
// GET /models
export const GetModelsV1Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
});
export const GetModelsV1Response = z
.object({
diff --git a/web/src/features/public-api/types/observations.ts b/web/src/features/public-api/types/observations.ts
index 00717d75cb..6537aad323 100644
--- a/web/src/features/public-api/types/observations.ts
+++ b/web/src/features/public-api/types/observations.ts
@@ -1,7 +1,7 @@
import {
type ObservationView,
paginationMetaResponseZod,
- paginationZod,
+ publicApiPaginationZod,
} from "@langfuse/shared";
import { stringDateTime } from "@langfuse/shared/src/server";
@@ -115,7 +115,7 @@ export const transformDbToApiObservation = (
// GET /observations
export const GetObservationsV1Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
type: ObservationType.nullish(),
name: z.string().nullish(),
userId: z.string().nullish(),
diff --git a/web/src/features/public-api/types/sessions.ts b/web/src/features/public-api/types/sessions.ts
index d98bf1ed9f..40c1500b3b 100644
--- a/web/src/features/public-api/types/sessions.ts
+++ b/web/src/features/public-api/types/sessions.ts
@@ -1,5 +1,8 @@
import { APITrace } from "@/src/features/public-api/types/traces";
-import { paginationMetaResponseZod, paginationZod } from "@langfuse/shared";
+import {
+ paginationMetaResponseZod,
+ publicApiPaginationZod,
+} from "@langfuse/shared";
import { stringDateTime } from "@langfuse/shared/src/server";
import { z } from "zod";
@@ -22,7 +25,7 @@ const APISession = z
// GET /sessions
export const GetSessionsV1Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
fromTimestamp: stringDateTime,
toTimestamp: stringDateTime,
});
diff --git a/web/src/features/public-api/types/traces.ts b/web/src/features/public-api/types/traces.ts
index ec1fc276b0..a40ac771c8 100644
--- a/web/src/features/public-api/types/traces.ts
+++ b/web/src/features/public-api/types/traces.ts
@@ -1,9 +1,9 @@
import { APIObservation } from "@/src/features/public-api/types/observations";
import {
APIScoreSchema,
- paginationZod,
paginationMetaResponseZod,
orderBy,
+ publicApiPaginationZod,
} from "@langfuse/shared";
import { stringDateTime, TraceBody } from "@langfuse/shared/src/server";
import { z } from "zod";
@@ -48,7 +48,7 @@ const APIExtendedTrace = APITrace.extend({
// GET /api/public/traces
export const GetTracesV1Query = z.object({
- ...paginationZod,
+ ...publicApiPaginationZod,
userId: z.string().nullish(),
name: z.string().nullish(),
tags: z.union([z.array(z.string()), z.string()]).nullish(),