Skip to content

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

fail observations on the API if they dont exist (#136)

Maximilian Deichmanne1c8deb534dc
diff --git a/src/__tests__/generations.servertest.ts b/src/__tests__/generations.servertest.ts
index 59a0fdbfd3..6a561dec52 100644
--- a/src/__tests__/generations.servertest.ts
+++ b/src/__tests__/generations.servertest.ts
@@ -472,4 +472,17 @@ describe("/api/public/generations API Endpoint", () => {
expect(dbGeneration?.metadata).toEqual({ key: "value" });
expect(dbGeneration?.version).toBe("2.0.0");
});
+ it("should fail update if generation does not exist", async () => {
+ const generationId = uuidv4();
+
+ const updateGeneration = await makeAPICall(
+ "PATCH",
+ "/api/public/generations",
+ {
+ generationId: generationId,
+ completion: "this is a great gpt response",
+ },
+ );
+ expect(updateGeneration.status).toBe(404);
+ });
});
diff --git a/src/__tests__/spans.servertest.ts b/src/__tests__/spans.servertest.ts
index 537042bd07..c66a7dd899 100644
--- a/src/__tests__/spans.servertest.ts
+++ b/src/__tests__/spans.servertest.ts
@@ -336,4 +336,14 @@ describe("/api/public/spans API Endpoint", () => {
expect(dbSpan?.metadata).toEqual({ meta: "value" });
expect(dbSpan?.version).toBe("2.0.0");
});
+
+ it("should fail update span if span does not exist", async () => {
+ const spanId = uuidv4();
+
+ const updatedSpan = await makeAPICall("PATCH", "/api/public/spans", {
+ spanId: spanId,
+ output: { key: "this is a great gpt output" },
+ });
+ expect(updatedSpan.status).toBe(404);
+ });
});
diff --git a/src/pages/api/public/generations.ts b/src/pages/api/public/generations.ts
index cc190aa371..8ff86975ef 100644
--- a/src/pages/api/public/generations.ts
+++ b/src/pages/api/public/generations.ts
@@ -261,7 +261,7 @@ export default async function handler(
} = GenerationPatchSchema.parse(req.body);
const existingObservation = await prisma.observation.findUnique({
- where: { id: generationId },
+ where: { id: generationId, projectId: authCheck.scope.projectId },
select: {
promptTokens: true,
completionTokens: true,
@@ -269,6 +269,14 @@ export default async function handler(
},
});
+ if (!existingObservation) {
+ console.log(`generation with id ${generationId} not found`);
+ return res.status(404).json({
+ success: false,
+ message: "Observation not found",
+ });
+ }
+
const mergedModel = model ?? existingObservation?.model ?? null;
const newPromptTokens =
diff --git a/src/pages/api/public/spans.ts b/src/pages/api/public/spans.ts
index e25dfe70de..018f8cf98e 100644
--- a/src/pages/api/public/spans.ts
+++ b/src/pages/api/public/spans.ts
@@ -180,7 +180,7 @@ export default async function handler(
// Check before upsert as Prisma only upserts in DB transaction when using unique key in select
// Including projectid would lead to race conditions and unique key errors
- const observationWithSameId = await prisma.observation.count({
+ const observationsWithSameId = await prisma.observation.count({
where: {
id: spanId,
projectId: {
@@ -188,11 +188,24 @@ export default async function handler(
},
},
});
- if (observationWithSameId > 0)
+
+ if (observationsWithSameId > 0)
throw new Error(
"Observation with same id already exists in another project",
);
+ const existingSpan = await prisma.observation.findUnique({
+ where: { id: spanId, projectId: authCheck.scope.projectId },
+ });
+
+ if (!existingSpan) {
+ console.log(`span with id ${spanId} not found`);
+ return res.status(404).json({
+ success: false,
+ message: "Span not found",
+ });
+ }
+
const newObservation = await prisma.observation.upsert({
where: {
id: spanId,