Skip to content

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

feat(api): add GET /datasets (#1590)

Marc Klingen5aaa50995411
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index c4ed75a89d..507ca107c1 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -122,13 +122,26 @@ types:
observationId: optional<string>
timestamp: datetime
comment: optional<string>
- Dataset:
+ DatasetCore:
properties:
id: string
name: string
projectId: string
createdAt: datetime
updatedAt: datetime
+ DatasetWithReferences: # GET datasets
+ extends: DatasetCore
+ properties:
+ items:
+ type: list<string>
+ docs: list of dataset item ids
+ runs:
+ type: list<string>
+ docs: list of dataset run names
+ Dataset: # GET datasets/[name]
+ docs: Dataset including all items
+ extends: DatasetCore
+ properties:
items: list<DatasetItem>
runs: list<string>
DatasetItem:
diff --git a/fern/apis/server/definition/datasets.yml b/fern/apis/server/definition/datasets.yml
index e08d1d3e9f..6eed0426a3 100644
--- a/fern/apis/server/definition/datasets.yml
+++ b/fern/apis/server/definition/datasets.yml
@@ -1,10 +1,21 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json
imports:
commons: ./commons.yml
+ pagination: ./utils/pagination.yml
service:
auth: true
base-path: /api/public
endpoints:
+ list:
+ method: GET
+ docs: Get all datasets
+ path: /datasets
+ request:
+ name: GetDatasetsRequest
+ query-parameters:
+ page: optional<integer>
+ limit: optional<integer>
+ response: PaginatedDatasets
get:
method: GET
docs: Get a dataset and its items
@@ -28,6 +39,10 @@ service:
response: commons.DatasetRun
types:
+ PaginatedDatasets:
+ properties:
+ data: list<commons.DatasetWithReferences>
+ meta: pagination.MetaResponse
CreateDatasetRequest:
properties:
name: string
diff --git a/generated/postman/collection.json b/generated/postman/collection.json
index 649f1a6230..c09ac8e3d8 100644
--- a/generated/postman/collection.json
+++ b/generated/postman/collection.json
@@ -154,6 +154,42 @@
"description": null,
"name": "Datasets",
"item": [
+ {
+ "_type": "endpoint",
+ "name": "List",
+ "request": {
+ "description": "Get all datasets",
+ "url": {
+ "raw": "{{baseUrl}}/api/public/datasets?page=&limit=",
+ "host": [
+ "{{baseUrl}}"
+ ],
+ "path": [
+ "api",
+ "public",
+ "datasets"
+ ],
+ "query": [
+ {
+ "key": "page",
+ "value": "",
+ "description": null
+ },
+ {
+ "key": "limit",
+ "value": "",
+ "description": null
+ }
+ ],
+ "variable": []
+ },
+ "header": [],
+ "method": "GET",
+ "auth": null,
+ "body": null
+ },
+ "response": []
+ },
{
"_type": "endpoint",
"name": "Get",
diff --git a/web/generated/openapi-server/openapi.yml b/web/generated/openapi-server/openapi.yml
index 44e01bfd5b..27c353b28a 100644
--- a/web/generated/openapi-server/openapi.yml
+++ b/web/generated/openapi-server/openapi.yml
@@ -152,25 +152,32 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/CreateDatasetRunItemRequest'
- /api/public/datasets/{datasetName}:
+ /api/public/datasets:
get:
- description: Get a dataset and its items
- operationId: datasets_get
+ description: Get all datasets
+ operationId: datasets_list
tags:
- Datasets
parameters:
- - name: datasetName
- in: path
- required: true
+ - name: page
+ in: query
+ required: false
schema:
- type: string
+ type: integer
+ nullable: true
+ - name: limit
+ in: query
+ required: false
+ schema:
+ type: integer
+ nullable: true
responses:
'200':
description: ''
content:
application/json:
schema:
- $ref: '#/components/schemas/Dataset'
+ $ref: '#/components/schemas/PaginatedDatasets'
'400':
description: ''
content:
@@ -197,7 +204,6 @@ paths:
application/json:
schema: {}
security: *ref_0
- /api/public/datasets:
post:
description: Create a dataset
operationId: datasets_create
@@ -243,6 +249,51 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/CreateDatasetRequest'
+ /api/public/datasets/{datasetName}:
+ get:
+ description: Get a dataset and its items
+ operationId: datasets_get
+ tags:
+ - Datasets
+ parameters:
+ - name: datasetName
+ in: path
+ required: true
+ schema:
+ type: string
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Dataset'
+ '400':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '401':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '403':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '404':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ '405':
+ description: ''
+ content:
+ application/json:
+ schema: {}
+ security: *ref_0
/api/public/datasets/{datasetName}/runs/{runName}:
get:
description: Get a dataset run and its items
@@ -1420,8 +1471,8 @@ components:
- value
- source
- timestamp
- Dataset:
- title: Dataset
+ DatasetCore:
+ title: DatasetCore
type: object
properties:
id:
@@ -1436,6 +1487,36 @@ components:
updatedAt:
type: string
format: date-time
+ required:
+ - id
+ - name
+ - projectId
+ - createdAt
+ - updatedAt
+ DatasetWithReferences:
+ title: DatasetWithReferences
+ type: object
+ properties:
+ items:
+ type: array
+ items:
+ type: string
+ description: list of dataset item ids
+ runs:
+ type: array
+ items:
+ type: string
+ description: list of dataset run names
+ required:
+ - items
+ - runs
+ allOf:
+ - $ref: '#/components/schemas/DatasetCore'
+ Dataset:
+ title: Dataset
+ type: object
+ description: Dataset including all items
+ properties:
items:
type: array
items:
@@ -1445,13 +1526,10 @@ components:
items:
type: string
required:
- - id
- - name
- - projectId
- - createdAt
- - updatedAt
- items
- runs
+ allOf:
+ - $ref: '#/components/schemas/DatasetCore'
DatasetItem:
title: DatasetItem
type: object
@@ -1620,6 +1698,19 @@ components:
required:
- runName
- datasetItemId
+ PaginatedDatasets:
+ title: PaginatedDatasets
+ type: object
+ properties:
+ data:
+ type: array
+ items:
+ $ref: '#/components/schemas/DatasetWithReferences'
+ meta:
+ $ref: '#/components/schemas/utilsMetaResponse'
+ required:
+ - data
+ - meta
CreateDatasetRequest:
title: CreateDatasetRequest
type: object
diff --git a/web/src/__tests__/datasets.servertest.ts b/web/src/__tests__/datasets.servertest.ts
index 8c557410c9..7cf650ed4c 100644
--- a/web/src/__tests__/datasets.servertest.ts
+++ b/web/src/__tests__/datasets.servertest.ts
@@ -32,6 +32,93 @@ describe("/api/public/datasets and /api/public/dataset-items API Endpoints", ()
});
});
+ it("GET datasets", async () => {
+ await makeAPICall("POST", "/api/public/datasets", {
+ name: "dataset-name-1",
+ });
+
+ await makeAPICall("POST", "/api/public/datasets", {
+ name: "dataset-name-2",
+ });
+
+ const datasetItemId = v4();
+
+ await makeAPICall("POST", "/api/public/dataset-items", {
+ datasetName: "dataset-name-2",
+ input: { key: "value" },
+ expectedOutput: { key: "value" },
+ id: datasetItemId,
+ });
+
+ const traceId = v4();
+ const observationId = v4();
+
+ const response = await makeAPICall("POST", "/api/public/ingestion", {
+ batch: [
+ {
+ id: v4(),
+ type: "trace-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: traceId,
+ name: "trace-name",
+ userId: "user-1",
+ metadata: { key: "value" },
+ release: "1.0.0",
+ version: "2.0.0",
+ },
+ },
+ {
+ id: v4(),
+ type: "observation-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: observationId,
+ traceId: traceId,
+ type: "GENERATION",
+ name: "generation-name",
+ startTime: "2021-01-01T00:00:00.000Z",
+ endTime: "2021-01-01T00:00:00.000Z",
+ modelParameters: { key: "value" },
+ input: { key: "value" },
+ metadata: { key: "value" },
+ version: "2.0.0",
+ },
+ },
+ ],
+ });
+ expect(response.status).toBe(207);
+
+ await makeAPICall("POST", "/api/public/dataset-run-items", {
+ datasetItemId: datasetItemId,
+ observationId: observationId,
+ runName: "test-run",
+ metadata: { key: "value" },
+ });
+
+ const getDatasets = await makeAPICall("GET", `/api/public/datasets`);
+
+ expect(getDatasets.status).toBe(200);
+ expect(getDatasets.body).toMatchObject({
+ data: expect.arrayContaining([
+ expect.objectContaining({
+ name: "dataset-name-1",
+ items: [],
+ runs: [],
+ }),
+ expect.objectContaining({
+ name: "dataset-name-2",
+ items: [datasetItemId],
+ runs: ["test-run"],
+ }),
+ ]),
+ meta: expect.objectContaining({
+ totalItems: 2,
+ page: 1,
+ }),
+ });
+ });
+
it("should create and get a dataset item (via datasets and individually)", async () => {
await makeAPICall("POST", "/api/public/datasets", {
name: "dataset-name",
diff --git a/web/src/pages/api/public/dataset-items.ts b/web/src/pages/api/public/dataset-items.ts
index f537a357bc..b90ebd4a3b 100644
--- a/web/src/pages/api/public/dataset-items.ts
+++ b/web/src/pages/api/public/dataset-items.ts
@@ -41,10 +41,11 @@ export default async function handler(
const itemBody = CreateDatasetItemSchema.parse(req.body);
// CHECK ACCESS SCOPE
- if (authCheck.scope.accessLevel !== "all")
- return res.status(403).json({
- message: "Access denied",
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message: "Access denied - need to use basic auth with secret key",
});
+ }
// END CHECK ACCESS SCOPE
// Check access to dataset
diff --git a/web/src/pages/api/public/dataset-items/[datasetItemId].ts b/web/src/pages/api/public/dataset-items/[datasetItemId].ts
index 21c608fff7..31d87b2b92 100644
--- a/web/src/pages/api/public/dataset-items/[datasetItemId].ts
+++ b/web/src/pages/api/public/dataset-items/[datasetItemId].ts
@@ -38,10 +38,11 @@ export default async function handler(
const { datasetItemId } = GetDatasetItemQuerySchema.parse(req.query);
// CHECK ACCESS SCOPE
- if (authCheck.scope.accessLevel !== "all")
- return res.status(403).json({
- message: "Access denied",
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message: "Access denied - need to use basic auth with secret key",
});
+ }
// END CHECK ACCESS SCOPE
// Check access to dataset
diff --git a/web/src/pages/api/public/dataset-run-items.ts b/web/src/pages/api/public/dataset-run-items.ts
index 94330bd082..18a17011d9 100644
--- a/web/src/pages/api/public/dataset-run-items.ts
+++ b/web/src/pages/api/public/dataset-run-items.ts
@@ -39,8 +39,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
console.log(
diff --git a/web/src/pages/api/public/datasets.ts b/web/src/pages/api/public/datasets.ts
index cfffe1a340..fda722d076 100644
--- a/web/src/pages/api/public/datasets.ts
+++ b/web/src/pages/api/public/datasets.ts
@@ -4,11 +4,16 @@ import { cors, runMiddleware } from "@/src/features/public-api/server/cors";
import { prisma } from "@langfuse/shared/src/db";
import { verifyAuthHeaderAndReturnScope } from "@/src/features/public-api/server/apiAuth";
import { isPrismaException } from "@/src/utils/exceptions";
+import { paginationZod } from "@/src/utils/zod";
const CreateDatasetSchema = z.object({
name: z.string(),
});
+const GetDatasetsSchema = z.object({
+ ...paginationZod,
+});
+
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
@@ -37,10 +42,11 @@ export default async function handler(
const { name } = CreateDatasetSchema.parse(req.body);
// CHECK ACCESS SCOPE
- if (authCheck.scope.accessLevel !== "all")
- return res.status(403).json({
- message: "Access denied",
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message: "Access denied - need to use basic auth with secret key",
});
+ }
// END CHECK ACCESS SCOPE
const dataset = await prisma.dataset.upsert({
@@ -58,6 +64,77 @@ export default async function handler(
});
res.status(200).json({ ...dataset, items: [], runs: [] });
+ } else if (req.method === "GET") {
+ // CHECK ACCESS SCOPE
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message: "Access denied - need to use basic auth with secret key",
+ });
+ }
+ // END CHECK ACCESS SCOPE
+
+ const args = GetDatasetsSchema.parse(req.query); // uses query and not body
+ console.log("Trying to get datasets", args);
+
+ const datasets = await prisma.dataset.findMany({
+ select: {
+ name: true,
+ projectId: true,
+ createdAt: true,
+ updatedAt: true,
+ id: true,
+ datasetItems: {
+ select: {
+ id: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ },
+ datasetRuns: {
+ select: {
+ name: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ },
+ },
+ where: {
+ projectId: authCheck.scope.projectId,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ take: args.limit,
+ skip: (args.page - 1) * args.limit,
+ });
+
+ console.log("Found datasets", datasets);
+
+ const totalItems = await prisma.dataset.count({
+ where: {
+ projectId: authCheck.scope.projectId,
+ },
+ });
+
+ return res.status(200).json({
+ data: datasets.map(({ datasetItems, datasetRuns, ...rest }) => ({
+ ...rest,
+ items: datasetItems.map(({ id }) => id),
+ runs: datasetRuns.map(({ name }) => name),
+ })),
+ meta: {
+ page: args.page,
+ limit: args.limit,
+ totalItems,
+ totalPages: Math.ceil(totalItems / args.limit),
+ },
+ });
+ } else {
+ res.status(405).json({
+ message: "Method Not Allowed",
+ });
}
} catch (error: unknown) {
console.error(error);
diff --git a/web/src/pages/api/public/datasets/[name]/index.ts b/web/src/pages/api/public/datasets/[name]/index.ts
index c2f188666d..9335db1d28 100644
--- a/web/src/pages/api/public/datasets/[name]/index.ts
+++ b/web/src/pages/api/public/datasets/[name]/index.ts
@@ -27,8 +27,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
diff --git a/web/src/pages/api/public/datasets/[name]/runs/[runName].ts b/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
index 956ed43dd3..0eeae48a2f 100644
--- a/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
+++ b/web/src/pages/api/public/datasets/[name]/runs/[runName].ts
@@ -30,8 +30,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET dataset runs",
+ message: "Access denied - need to use basic auth with secret key",
});
}
console.log(
diff --git a/web/src/pages/api/public/metrics/daily.ts b/web/src/pages/api/public/metrics/daily.ts
index 5e5a7686c6..818c79c992 100644
--- a/web/src/pages/api/public/metrics/daily.ts
+++ b/web/src/pages/api/public/metrics/daily.ts
@@ -33,8 +33,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
const obj = GetUsageSchema.parse(req.query); // uses query and not body
diff --git a/web/src/pages/api/public/observations/[observationId].ts b/web/src/pages/api/public/observations/[observationId].ts
index e173c45747..d053e128f0 100644
--- a/web/src/pages/api/public/observations/[observationId].ts
+++ b/web/src/pages/api/public/observations/[observationId].ts
@@ -38,8 +38,7 @@ export default async function handler(
// CHECK ACCESS SCOPE
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET traces",
+ message: "Access denied - need to use basic auth with secret key",
});
}
// END CHECK ACCESS SCOPE
diff --git a/web/src/pages/api/public/observations/index.ts b/web/src/pages/api/public/observations/index.ts
index 72a73bf3f8..c122e7ec92 100644
--- a/web/src/pages/api/public/observations/index.ts
+++ b/web/src/pages/api/public/observations/index.ts
@@ -53,8 +53,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET generations",
+ message: "Access denied - need to use basic auth with secret key",
});
}
diff --git a/web/src/pages/api/public/prompts.ts b/web/src/pages/api/public/prompts.ts
index a72fc7c924..588b69cb06 100644
--- a/web/src/pages/api/public/prompts.ts
+++ b/web/src/pages/api/public/prompts.ts
@@ -46,8 +46,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET prompts",
+ message: "Access denied - need to use basic auth with secret key",
});
}
@@ -112,8 +111,7 @@ export default async function handler(
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to POST prompts",
+ message: "Access denied - need to use basic auth with secret key",
});
}
diff --git a/web/src/pages/api/public/scores.ts b/web/src/pages/api/public/scores.ts
index 40a6a2cee5..44d3fb9b4e 100644
--- a/web/src/pages/api/public/scores.ts
+++ b/web/src/pages/api/public/scores.ts
@@ -89,8 +89,7 @@ export default async function handler(
try {
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
diff --git a/web/src/pages/api/public/scores/[scoreId].ts b/web/src/pages/api/public/scores/[scoreId].ts
index d75b00ae89..0930181551 100644
--- a/web/src/pages/api/public/scores/[scoreId].ts
+++ b/web/src/pages/api/public/scores/[scoreId].ts
@@ -28,8 +28,7 @@ export default async function handler(
// END CHECK AUTH
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to DELETE scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
@@ -100,8 +99,7 @@ export default async function handler(
// CHECK ACCESS SCOPE
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
// END CHECK ACCESS SCOPE
diff --git a/web/src/pages/api/public/sessions/[sessionId].ts b/web/src/pages/api/public/sessions/[sessionId].ts
index 71bbe32684..4ac3f0ba32 100644
--- a/web/src/pages/api/public/sessions/[sessionId].ts
+++ b/web/src/pages/api/public/sessions/[sessionId].ts
@@ -37,8 +37,7 @@ export default async function handler(
// CHECK ACCESS SCOPE
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET traces",
+ message: "Access denied - need to use basic auth with secret key",
});
}
// END CHECK ACCESS SCOPE
diff --git a/web/src/pages/api/public/traces.ts b/web/src/pages/api/public/traces.ts
index 054db35d90..ea970e4282 100644
--- a/web/src/pages/api/public/traces.ts
+++ b/web/src/pages/api/public/traces.ts
@@ -63,10 +63,11 @@ export default async function handler(
JSON.stringify(req.body, null, 2),
);
- if (authCheck.scope.accessLevel !== "all")
- return res.status(403).json({
- message: "Access denied",
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ message: "Access denied - need to use basic auth with secret key",
});
+ }
const body = TraceBody.parse(req.body);
@@ -84,8 +85,7 @@ export default async function handler(
} else if (req.method === "GET") {
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}
diff --git a/web/src/pages/api/public/traces/[traceId].ts b/web/src/pages/api/public/traces/[traceId].ts
index 6a0527bba7..81adf3addf 100644
--- a/web/src/pages/api/public/traces/[traceId].ts
+++ b/web/src/pages/api/public/traces/[traceId].ts
@@ -38,8 +38,7 @@ export default async function handler(
// CHECK ACCESS SCOPE
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET traces",
+ message: "Access denied - need to use basic auth with secret key",
});
}
// END CHECK ACCESS SCOPE
diff --git a/web/src/pages/api/public/users.ts b/web/src/pages/api/public/users.ts
index 3545f209cb..05de6b2eaf 100644
--- a/web/src/pages/api/public/users.ts
+++ b/web/src/pages/api/public/users.ts
@@ -30,8 +30,7 @@ export default async function handler(
if (req.method === "GET") {
if (authCheck.scope.accessLevel !== "all") {
return res.status(401).json({
- message:
- "Access denied - need to use basic auth with secret key to GET scores",
+ message: "Access denied - need to use basic auth with secret key",
});
}