Skip to content

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

GET /traces (#60)

Marc Klingen46021b9d2a56
diff --git a/fern/server/definition/commons.yml b/fern/server/definition/commons.yml
index f0c66aef93..e7973148dd 100644
--- a/fern/server/definition/commons.yml
+++ b/fern/server/definition/commons.yml
@@ -39,6 +39,15 @@ types:
userId: optional<string>
metadata: optional<unknown>
TraceWithDetails:
+ extends: Trace
+ properties:
+ observations:
+ type: list<string>
+ docs: List of observation ids
+ scores:
+ type: list<string>
+ docs: List of score ids
+ TraceWithFullDetails:
extends: Trace
properties:
observations: list<Observation>
diff --git a/fern/server/definition/trace.yml b/fern/server/definition/trace.yml
index d50c3294d5..2518d2c280 100644
--- a/fern/server/definition/trace.yml
+++ b/fern/server/definition/trace.yml
@@ -1,5 +1,6 @@
# yaml-language-server: $schema=https://raw.githubusercontent.com/fern-api/fern/main/fern.schema.json
imports:
+ pagination: ./utils/pagination.yml
commons: ./commons.yml
service:
auth: true
@@ -19,7 +20,19 @@ service:
traceId:
type: string
docs: The unique langfuse identifier of a trace
- response: commons.TraceWithDetails
+ response: commons.TraceWithFullDetails
+ list:
+ docs: Get list of traces
+ method: GET
+ path: /traces
+ request:
+ name: GetTracesRequest
+ query-parameters:
+ page: optional<integer>
+ limit: optional<integer>
+ userId: optional<string>
+ name: optional<string>
+ response: Traces
types:
CreateTraceRequest:
@@ -28,3 +41,7 @@ types:
userId: optional<string>
externalId: optional<string>
metadata: optional<unknown>
+ Traces:
+ properties:
+ data: list<commons.TraceWithDetails>
+ meta: pagination.MetaResponse
diff --git a/fern/server/definition/utils/pagination.yml b/fern/server/definition/utils/pagination.yml
index e66b72b30f..251994fb10 100644
--- a/fern/server/definition/utils/pagination.yml
+++ b/fern/server/definition/utils/pagination.yml
@@ -1,7 +1,15 @@
types:
MetaResponse:
properties:
- page: integer
- limit: integer
- totalItems: integer
- totalPages: integer
+ page:
+ type: integer
+ docs: current page number
+ limit:
+ type: integer
+ docs: number of items per page
+ totalItems:
+ type: integer
+ docs: number of total items given the current filters/selection (if any)
+ totalPages:
+ type: integer
+ docs: number of total pages given the current limit
diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.yml
index 7f4bcf42c4..079cd83a53 100644
--- a/generated/openapi-server/openapi.yml
+++ b/generated/openapi-server/openapi.yml
@@ -478,6 +478,74 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/CreateTraceRequest'
+ get:
+ description: Get list of traces
+ operationId: trace_list
+ tags:
+ - Trace
+ parameters:
+ - name: page
+ in: query
+ required: false
+ schema:
+ type: integer
+ nullable: true
+ - name: limit
+ in: query
+ required: false
+ schema:
+ type: integer
+ nullable: true
+ - name: userId
+ in: query
+ required: false
+ schema:
+ type: string
+ nullable: true
+ - name: name
+ in: query
+ required: false
+ schema:
+ type: string
+ nullable: true
+ responses:
+ '200':
+ description: ''
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/Traces'
+ '400':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '401':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '403':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '404':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ '405':
+ description: ''
+ content:
+ application/json:
+ schema:
+ type: string
+ security: *ref_0
/api/public/traces/{traceId}:
get:
description: Get a specific trace
@@ -497,7 +565,7 @@ paths:
content:
application/json:
schema:
- $ref: '#/components/schemas/TraceWithDetails'
+ $ref: '#/components/schemas/TraceWithFullDetails'
'400':
description: ''
content:
@@ -629,6 +697,25 @@ components:
TraceWithDetails:
title: TraceWithDetails
type: object
+ properties:
+ observations:
+ type: array
+ items:
+ type: string
+ description: List of observation ids
+ scores:
+ type: array
+ items:
+ type: string
+ description: List of score ids
+ required:
+ - observations
+ - scores
+ allOf:
+ - $ref: '#/components/schemas/Trace'
+ TraceWithFullDetails:
+ title: TraceWithFullDetails
+ type: object
properties:
observations:
type: array
@@ -889,18 +976,35 @@ components:
nullable: true
metadata:
nullable: true
+ Traces:
+ title: Traces
+ type: object
+ properties:
+ data:
+ type: array
+ items:
+ $ref: '#/components/schemas/TraceWithDetails'
+ meta:
+ $ref: '#/components/schemas/utilsMetaResponse'
+ required:
+ - data
+ - meta
utilsMetaResponse:
title: utilsMetaResponse
type: object
properties:
page:
type: integer
+ description: current page number
limit:
type: integer
+ description: number of items per page
totalItems:
type: integer
+ description: number of total items given the current filters/selection (if any)
totalPages:
type: integer
+ description: number of total pages given the current limit
required:
- page
- limit
diff --git a/src/pages/api/public/scores.ts b/src/pages/api/public/scores.ts
index dd4170273e..8ea3420028 100644
--- a/src/pages/api/public/scores.ts
+++ b/src/pages/api/public/scores.ts
@@ -115,7 +115,6 @@ export default async function handler(
});
}
- console.log(req.query);
const obj = ScoresGetSchema.parse(req.query); // uses query and not body
const [scores, totalItems] = await Promise.all([
diff --git a/src/pages/api/public/traces.ts b/src/pages/api/public/traces.ts
index 78cbc434ed..5aa80f285c 100644
--- a/src/pages/api/public/traces.ts
+++ b/src/pages/api/public/traces.ts
@@ -11,17 +11,19 @@ const CreateTraceSchema = z.object({
metadata: z.unknown().nullish(),
});
+const GetTracesSchema = z.object({
+ page: z.coerce.number().int().positive().default(1),
+ limit: z.coerce.number().int().positive().lte(100).default(50),
+ userId: z.string().nullish(),
+ name: z.string().nullish(),
+});
+
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
await runMiddleware(req, res, cors);
- if (req.method !== "POST") {
- console.error(req.method, req.body);
- return res.status(405).json({ message: "Method not allowed" });
- }
-
// CHECK AUTH
const authCheck = await verifyAuthHeaderAndReturnScope(
req.headers.authorization
@@ -34,54 +36,111 @@ export default async function handler(
// END CHECK AUTH
try {
- console.log("Trying to create trace:", req.body);
+ if (req.method === "POST") {
+ console.log("Trying to create trace:", req.body);
- const { name, metadata, externalId, userId } = CreateTraceSchema.parse(
- req.body
- );
+ const { name, metadata, externalId, userId } = CreateTraceSchema.parse(
+ req.body
+ );
- // CHECK ACCESS SCOPE
- if (authCheck.scope.accessLevel !== "all")
- return res.status(403).json({
- success: false,
- message: "Access denied",
- });
- // END CHECK ACCESS SCOPE
+ // CHECK ACCESS SCOPE
+ if (authCheck.scope.accessLevel !== "all")
+ return res.status(403).json({
+ success: false,
+ message: "Access denied",
+ });
+ // END CHECK ACCESS SCOPE
- if (externalId) {
- // For traces created with external ids, allow upserts
- const newTrace = await prisma.trace.upsert({
- where: {
- projectId_externalId: {
+ if (externalId) {
+ // For traces created with external ids, allow upserts
+ const newTrace = await prisma.trace.upsert({
+ where: {
+ projectId_externalId: {
+ externalId: externalId,
+ projectId: authCheck.scope.projectId,
+ },
+ },
+ create: {
+ timestamp: new Date(),
+ projectId: authCheck.scope.projectId,
externalId: externalId,
+ name: name ?? undefined,
+ userId: userId ?? undefined,
+ metadata: metadata ?? undefined,
+ },
+ update: {
+ name: name ?? undefined,
+ metadata: metadata ?? undefined,
+ },
+ });
+ res.status(200).json(newTrace);
+ } else {
+ const newTrace = await prisma.trace.create({
+ data: {
+ timestamp: new Date(),
projectId: authCheck.scope.projectId,
+ name: name ?? undefined,
+ userId: userId ?? undefined,
+ metadata: metadata ?? undefined,
},
- },
- create: {
- timestamp: new Date(),
- projectId: authCheck.scope.projectId,
- externalId: externalId,
- name: name ?? undefined,
- userId: userId ?? undefined,
- metadata: metadata ?? undefined,
- },
- update: {
- name: name ?? undefined,
- metadata: metadata ?? undefined,
+ });
+ res.status(200).json(newTrace);
+ }
+ } else if (req.method === "GET") {
+ if (authCheck.scope.accessLevel !== "all") {
+ return res.status(401).json({
+ success: false,
+ message:
+ "Access denied - need to use basic auth with secret key to GET scores",
+ });
+ }
+
+ const obj = GetTracesSchema.parse(req.query); // uses query and not body
+
+ const [traces, totalItems] = await Promise.all([
+ prisma.trace.findMany({
+ where: {
+ projectId: authCheck.scope.projectId,
+ name: obj.name ?? undefined,
+ userId: obj.userId ?? undefined,
+ },
+ include: {
+ observations: {
+ select: {
+ id: true,
+ },
+ },
+ },
+ skip: (obj.page - 1) * obj.limit,
+ take: obj.limit,
+ orderBy: {
+ timestamp: "desc",
+ },
+ }),
+ prisma.trace.count({
+ where: {
+ projectId: authCheck.scope.projectId,
+ name: obj.name ?? undefined,
+ userId: obj.userId ?? undefined,
+ },
+ }),
+ ]);
+
+ return res.status(200).json({
+ data: traces.map((trace) => ({
+ ...trace,
+ observations: trace.observations.map((observation) => observation.id),
+ })),
+ meta: {
+ page: obj.page,
+ limit: obj.limit,
+ totalItems,
+ totalPages: Math.ceil(totalItems / obj.limit),
},
});
- res.status(200).json(newTrace);
} else {
- const newTrace = await prisma.trace.create({
- data: {
- timestamp: new Date(),
- projectId: authCheck.scope.projectId,
- name: name ?? undefined,
- userId: userId ?? undefined,
- metadata: metadata ?? undefined,
- },
- });
- res.status(200).json(newTrace);
+ console.error(req.method, req.body);
+ return res.status(405).json({ message: "Method not allowed" });
}
} catch (error: unknown) {
console.error(error);