Skip to content

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

feat(api): add `traceTags` query param to GET /scores (#3843)

marliessophie53dc3c3473ef
diff --git a/fern/apis/server/definition/score.yml b/fern/apis/server/definition/score.yml
index d2edf7cbdc..5534f71e2e 100644
--- a/fern/apis/server/definition/score.yml
+++ b/fern/apis/server/definition/score.yml
@@ -58,7 +58,11 @@ service:
dataType:
type: optional<commons.ScoreDataType>
docs: Retrieve only scores with a specific dataType.
- response: Scores
+ traceTags:
+ type: optional<list<string>>
+ allow-multiple: true
+ docs: Only scores linked to traces that include all of these tags will be returned.
+ response: GetScoresResponse
get-by-id:
docs: Get a score
method: GET
@@ -135,7 +139,19 @@ types:
id:
type: string
docs: The id of the created object in Langfuse
- Scores:
+ GetScoresResponseTraceData:
properties:
- data: list<commons.Score>
+ userId:
+ type: optional<string>
+ docs: The user ID associated with the trace referenced by score
+ tags:
+ type: optional<list<string>>
+ docs: A list of tags associated with the trace referenced by score
+ GetScoresResponseData:
+ properties:
+ <<: commons.Score
+ trace: GetScoresResponseTraceData
+ GetScoresResponse:
+ properties:
+ data: list<GetScoresResponseData>
meta: pagination.MetaResponse
diff --git a/packages/shared/src/features/scores/scoreTypes.ts b/packages/shared/src/features/scores/scoreTypes.ts
index d1c9946174..e103b037b9 100644
--- a/packages/shared/src/features/scores/scoreTypes.ts
+++ b/packages/shared/src/features/scores/scoreTypes.ts
@@ -236,6 +236,7 @@ export const GetScoresQuery = z.object({
dataType: z.enum(ScoreDataType).nullish(),
configId: z.string().nullish(),
queueId: z.string().nullish(),
+ traceTags: z.union([z.array(z.string()), z.string()]).nullish(),
name: z.string().nullish(),
fromTimestamp: stringDateTime,
toTimestamp: stringDateTime,
@@ -257,6 +258,7 @@ const LegacyGetScoreResponseDataV1 = z.intersection(
z.object({
trace: z.object({
userId: z.string().nullish(),
+ tags: z.array(z.string()).nullish(),
}),
})
);
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index bf0f7174de..c378bd3bee 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -1788,13 +1788,24 @@ paths:
schema:
$ref: '#/components/schemas/ScoreDataType'
nullable: true
+ - name: traceTags
+ in: query
+ description: >-
+ Only scores linked to traces that include all of these tags will be
+ returned.
+ required: false
+ schema:
+ type: array
+ items:
+ type: string
+ nullable: true
responses:
'200':
description: ''
content:
application/json:
schema:
- $ref: '#/components/schemas/Scores'
+ $ref: '#/components/schemas/GetScoresResponse'
'400':
description: ''
content:
@@ -4305,14 +4316,39 @@ components:
description: The id of the created object in Langfuse
required:
- id
- Scores:
- title: Scores
+ GetScoresResponseTraceData:
+ title: GetScoresResponseTraceData
+ type: object
+ properties:
+ userId:
+ type: string
+ nullable: true
+ description: The user ID associated with the trace referenced by score
+ tags:
+ type: array
+ items:
+ type: string
+ nullable: true
+ description: A list of tags associated with the trace referenced by score
+ GetScoresResponseData:
+ title: GetScoresResponseData
+ type: object
+ properties:
+ '<<':
+ $ref: '#/components/schemas/Score'
+ trace:
+ $ref: '#/components/schemas/GetScoresResponseTraceData'
+ required:
+ - '<<'
+ - trace
+ GetScoresResponse:
+ title: GetScoresResponse
type: object
properties:
data:
type: array
items:
- $ref: '#/components/schemas/Score'
+ $ref: '#/components/schemas/GetScoresResponseData'
meta:
$ref: '#/components/schemas/utilsMetaResponse'
required:
diff --git a/web/public/generated/postman/collection.json b/web/public/generated/postman/collection.json
index b13ed0bbc3..66d7122556 100644
--- a/web/public/generated/postman/collection.json
+++ b/web/public/generated/postman/collection.json
@@ -1244,7 +1244,7 @@
"request": {
"description": "Get a list of scores",
"url": {
- "raw": "{{baseUrl}}/api/public/scores?page=&limit=&userId=&name=&fromTimestamp=&toTimestamp=&source=&operator=&value=&scoreIds=&configId=&queueId=&dataType=",
+ "raw": "{{baseUrl}}/api/public/scores?page=&limit=&userId=&name=&fromTimestamp=&toTimestamp=&source=&operator=&value=&scoreIds=&configId=&queueId=&dataType=&traceTags=",
"host": [
"{{baseUrl}}"
],
@@ -1318,6 +1318,11 @@
"key": "dataType",
"value": "",
"description": "Retrieve only scores with a specific dataType."
+ },
+ {
+ "key": "traceTags",
+ "value": "",
+ "description": "Only scores linked to traces that include all of these tags will be returned."
}
],
"variable": []
diff --git a/web/src/__tests__/scores.servertest.ts b/web/src/__tests__/scores.servertest.ts
index 6ebdc8c7c2..80be1a8f73 100644
--- a/web/src/__tests__/scores.servertest.ts
+++ b/web/src/__tests__/scores.servertest.ts
@@ -882,13 +882,19 @@ describe("/api/public/scores API Endpoint", () => {
describe("should Filter scores", () => {
let configId = "";
const userId = "user-name";
+ const traceTags = ["prod", "test"];
+ const traceTags_2 = ["staging", "dev"];
const scoreName = "score-name";
const queryUserName = `userId=${userId}&name=${scoreName}`;
const traceId = uuidv4();
+ const traceId_2 = uuidv4();
+ const traceId_3 = uuidv4();
const generationId = uuidv4();
const scoreId_1 = uuidv4();
const scoreId_2 = uuidv4();
const scoreId_3 = uuidv4();
+ const scoreId_4 = uuidv4();
+ const scoreId_5 = uuidv4();
beforeAll(async () => {
should_prune_db = false;
@@ -901,6 +907,27 @@ describe("/api/public/scores API Endpoint", () => {
{
id: traceId,
userId: userId,
+ tags: traceTags,
+ },
+ );
+ await makeZodVerifiedAPICall(
+ PostTracesV1Response,
+ "POST",
+ "/api/public/traces",
+ {
+ id: traceId_2,
+ userId: userId,
+ tags: traceTags_2,
+ },
+ );
+ await makeZodVerifiedAPICall(
+ PostTracesV1Response,
+ "POST",
+ "/api/public/traces",
+ {
+ id: traceId_3,
+ userId: userId,
+ tags: ["staging"],
},
);
await makeZodVerifiedAPICall(
@@ -950,6 +977,20 @@ describe("/api/public/scores API Endpoint", () => {
traceId: traceId,
comment: "comment",
});
+ await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId_4,
+ name: "other-score-name",
+ value: "best",
+ traceId: traceId_2,
+ comment: "comment",
+ });
+ await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId_5,
+ name: "other-score-name",
+ value: "test",
+ traceId: traceId_3,
+ comment: "comment",
+ });
});
afterAll(async () => {
await pruneDatabase();
@@ -1022,6 +1063,50 @@ describe("/api/public/scores API Endpoint", () => {
}
});
+ it("get all scores for trace tag 'prod'", async () => {
+ const getAllScore = await makeZodVerifiedAPICall(
+ GetScoresResponse,
+ "GET",
+ `/api/public/scores?traceTags=${"prod"}`,
+ );
+
+ expect(getAllScore.status).toBe(200);
+ expect(getAllScore.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 3,
+ totalPages: 1,
+ });
+ for (const val of getAllScore.body.data) {
+ expect(val).toMatchObject({
+ traceId: traceId,
+ trace: { tags: ["prod", "test"], userId: "user-name" },
+ });
+ }
+ });
+
+ it("get all scores for trace tags 'staging' and 'dev'", async () => {
+ const getAllScore = await makeZodVerifiedAPICall(
+ GetScoresResponse,
+ "GET",
+ `/api/public/scores?traceTags=${["staging", "dev"]}`,
+ );
+
+ expect(getAllScore.status).toBe(200);
+ expect(getAllScore.body.meta).toMatchObject({
+ page: 1,
+ limit: 50,
+ totalItems: 1,
+ totalPages: 1,
+ });
+ for (const val of getAllScore.body.data) {
+ expect(val).toMatchObject({
+ traceId: traceId_2,
+ trace: { tags: ["dev", "staging"], userId: "user-name" },
+ });
+ }
+ });
+
describe("should Filter scores by queueId", () => {
describe("queueId filtering", () => {
let queueId: string;
@@ -1296,10 +1381,10 @@ describe("/api/public/scores API Endpoint", () => {
expect(fetchedScores.body.meta).toMatchObject({
page: 1,
limit: 50,
- totalItems: 4,
+ totalItems: 6,
totalPages: 1,
});
- expect(fetchedScores.body.data.length).toBe(4);
+ expect(fetchedScores.body.data.length).toBe(6);
});
it("test invalid operator", async () => {
diff --git a/web/src/pages/api/public/scores/index.ts b/web/src/pages/api/public/scores/index.ts
index 6c1271fd21..81a8b84c64 100644
--- a/web/src/pages/api/public/scores/index.ts
+++ b/web/src/pages/api/public/scores/index.ts
@@ -53,6 +53,7 @@ export default withMiddlewares({
limit,
configId,
queueId,
+ traceTags,
userId,
name,
fromTimestamp,
@@ -71,6 +72,14 @@ export default withMiddlewares({
const queueCondition = queueId
? Prisma.sql`AND s."queue_id" = ${queueId}`
: Prisma.empty;
+ const traceTagsCondition = traceTags
+ ? Prisma.sql`AND ARRAY[${Prisma.join(
+ (Array.isArray(traceTags) ? traceTags : traceTags.split(",")).map(
+ (v) => Prisma.sql`${v}`,
+ ),
+ ", ",
+ )}] <@ t."tags"`
+ : Prisma.empty;
const dataTypeCondition = dataType
? Prisma.sql`AND s."data_type" = ${dataType}::"ScoreDataType"`
: Prisma.empty;
@@ -115,12 +124,13 @@ export default withMiddlewares({
s.queue_id as "queueId",
s.trace_id as "traceId",
s.observation_id as "observationId",
- json_build_object('userId', t.user_id) as "trace"
+ json_build_object('userId', t.user_id, 'tags', t.tags) as "trace"
FROM "scores" AS s
LEFT JOIN "traces" AS t ON t.id = s.trace_id AND t.project_id = ${auth.scope.projectId}
WHERE s.project_id = ${auth.scope.projectId}
${configCondition}
${queueCondition}
+ ${traceTagsCondition}
${dataTypeCondition}
${userCondition}
${nameCondition}
@@ -141,6 +151,7 @@ export default withMiddlewares({
WHERE s.project_id = ${auth.scope.projectId}
${configCondition}
${queueCondition}
+ ${traceTagsCondition}
${dataTypeCondition}
${userCondition}
${nameCondition}