Skip to content

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

fix(ingestion): do best-effort parsing for invalid usageDetails (#10926)

Hassieb Pakzad21b76029ee18
diff --git a/packages/shared/src/server/ingestion/types.ts b/packages/shared/src/server/ingestion/types.ts
index 7249fe29a0..6d12f3c4e8 100644
--- a/packages/shared/src/server/ingestion/types.ts
+++ b/packages/shared/src/server/ingestion/types.ts
@@ -68,13 +68,36 @@ export const usage = MixedUsage.nullish()
.pipe(Usage.nullish());
const CostDetails = z
- .record(z.string(), z.number().nonnegative().nullish())
+ .record(z.string(), z.unknown())
+ .nullish()
+ .transform((val) => {
+ if (!val) return val;
+
+ const result: Record<string, number> = {};
+
+ for (const [key, value] of Object.entries(val)) {
+ if (typeof value === "number" && !isNaN(value) && value >= 0) {
+ result[key] = value;
+ }
+ }
+
+ return Object.keys(result).length > 0 ? result : undefined;
+ })
.nullish();
-const RawUsageDetails = z.record(
- z.string(),
- z.number().int().nonnegative().nullish(),
-);
+const RawUsageDetails = z.record(z.string(), z.unknown()).transform((val) => {
+ if (!val) return;
+
+ const result: Record<string, number> = {};
+
+ for (const [key, value] of Object.entries(val)) {
+ if (typeof value === "number" && Number.isInteger(value) && value >= 0) {
+ result[key] = value;
+ }
+ }
+
+ return Object.keys(result).length > 0 ? result : undefined;
+});
const OpenAICompletionUsageSchema = z
.object({
diff --git a/web/src/__tests__/async/ingestion-api.servertest.ts b/web/src/__tests__/async/ingestion-api.servertest.ts
index 0bb602f6ff..3dbead9c34 100644
--- a/web/src/__tests__/async/ingestion-api.servertest.ts
+++ b/web/src/__tests__/async/ingestion-api.servertest.ts
@@ -582,7 +582,7 @@ describe("/api/public/ingestion API Endpoint", () => {
expect(response.body.errors[0].message).toBe("Invalid request data");
});
- it("should fail for float in usageDetails", async () => {
+ it("should silently drop invalid float values in usageDetails", async () => {
const response = await makeAPICall("POST", "/api/public/ingestion", {
batch: [
{
@@ -598,18 +598,17 @@ describe("/api/public/ingestion API Endpoint", () => {
input: { text: "input" },
output: { text: "output" },
usageDetails: {
- key: 0.1,
+ key: 0.1, // invalid float - should be silently dropped
},
},
},
],
});
+ // Event should succeed, invalid usageDetails values are silently dropped
expect(response.status).toBe(207);
- expect("errors" in response.body).toBe(true);
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
- expect(response.body.errors.length).toBe(1);
- expect(response.body.errors[0].message).toBe("Invalid request data");
+ expect(response.body.successes.length).toBe(1);
+ expect(response.body.errors.length).toBe(0);
});
it.each([