Skip to content

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

refactor: improve type safety in ingestion score types across typescript versions (#2757)

marliessophie484ef3fcacfe
diff --git a/packages/shared/src/features/ingestion/types.ts b/packages/shared/src/features/ingestion/types.ts
index 36a38610cf..568845f164 100644
--- a/packages/shared/src/features/ingestion/types.ts
+++ b/packages/shared/src/features/ingestion/types.ts
@@ -168,63 +168,39 @@ const BaseScoreBody = z.object({
/**
* ScoreBody exactly mirrors `PostScoresBody` in the public API. Please refer there for source of truth.
*/
-export const ScoreBody = z
- .discriminatedUnion("dataType", [
- BaseScoreBody.merge(
- z.object({
- 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({
- value: z.number().refine((val) => val === 0 || val === 1, {
- message: "Value must be either 0 or 1",
- }),
- 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(),
- })
- ),
- ])
- .superRefine((data, ctx) => {
- if (data.dataType) {
- if (typeof data.value === "number") {
- if (data.dataType === "CATEGORICAL") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must be a string for data type ${data.dataType}`,
- });
- }
- } else if (typeof data.value === "string") {
- if (data.dataType === "NUMERIC") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must be a number for data type ${data.dataType}`,
- });
- } else if (data.dataType === "BOOLEAN") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must number equal to either 0 or 1 for data type ${data.dataType}`,
- });
- }
- }
- }
- });
+export const ScoreBody = z.discriminatedUnion("dataType", [
+ BaseScoreBody.merge(
+ z.object({
+ 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({
+ value: z.number().refine((value) => value === 0 || value === 1, {
+ message:
+ "Value must be a number equal to either 0 or 1 for data type BOOLEAN",
+ }),
+ 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(),
+ })
+ ),
+]);
// LEGACY, only required for backwards compatibility
export const LegacySpanPostSchema = z.object({
diff --git a/web/src/__tests__/scores.servertest.ts b/web/src/__tests__/scores.servertest.ts
index 3eeb4bfdd5..90e08877b2 100644
--- a/web/src/__tests__/scores.servertest.ts
+++ b/web/src/__tests__/scores.servertest.ts
@@ -597,7 +597,8 @@ describe("/api/public/scores API Endpoint", () => {
error: [
{
code: "custom",
- message: "Value must be either 0 or 1",
+ message:
+ "Value must be a number equal to either 0 or 1 for data type BOOLEAN",
path: ["value"],
},
],
diff --git a/web/src/features/public-api/types/scores.ts b/web/src/features/public-api/types/scores.ts
index fabedff927..068db1b04b 100644
--- a/web/src/features/public-api/types/scores.ts
+++ b/web/src/features/public-api/types/scores.ts
@@ -189,63 +189,39 @@ export const validateDbScore = (score: Score): APIScore =>
/**
* PostScoresBody is copied for the ingestion API as `ScoreBody`. Please copy any changes here in `packages/shared/src/features/ingestion/types.ts`
*/
-export const PostScoresBody = z
- .discriminatedUnion("dataType", [
- BaseScoreBody.merge(
- z.object({
- 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({
- value: z.number().refine((val) => val === 0 || val === 1, {
- message: "Value must be either 0 or 1",
- }),
- 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(),
+export const PostScoresBody = z.discriminatedUnion("dataType", [
+ BaseScoreBody.merge(
+ z.object({
+ 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({
+ value: z.number().refine((value) => value === 0 || value === 1, {
+ message:
+ "Value must be a number equal to either 0 or 1 for data type BOOLEAN",
}),
- ),
- ])
- .superRefine((data, ctx) => {
- if (data.dataType) {
- if (typeof data.value === "number") {
- if (data.dataType === "CATEGORICAL") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must be a string for data type ${data.dataType}`,
- });
- }
- } else if (typeof data.value === "string") {
- if (data.dataType === "NUMERIC") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must be a number for data type ${data.dataType}`,
- });
- } else if (data.dataType === "BOOLEAN") {
- ctx.addIssue({
- code: z.ZodIssueCode.custom,
- message: `Value must number equal to either 0 or 1 for data type ${data.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(),
+ }),
+ ),
+]);
export const PostScoresResponse = z.object({ id: z.string() });