Skip to content

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

feat(api): add prompt tags via API (#2180)

Richard Krümmel2be198400df6
diff --git a/fern/apis/server/definition/prompts.yml b/fern/apis/server/definition/prompts.yml
index 20e88c3f40..8838d76ce9 100644
--- a/fern/apis/server/definition/prompts.yml
+++ b/fern/apis/server/definition/prompts.yml
@@ -70,14 +70,24 @@ types:
name: string
prompt: list<ChatMessage>
config: optional<unknown>
- labels: optional<list<string>>
+ labels:
+ type: optional<list<string>>
+ docs: List of deployment labels of this prompt version.
+ tags:
+ type: optional<list<string>>
+ docs: List of tags to apply to all versions of this prompt.
CreateTextPromptRequest:
properties:
name: string
prompt: string
config: optional<unknown>
- labels: optional<list<string>>
+ labels:
+ type: optional<list<string>>
+ docs: List of deployment labels of this prompt version.
+ tags:
+ type: optional<list<string>>
+ docs: List of tags to apply to all versions of this prompt.
Prompt:
union:
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index e5c26525be..cba7d06f29 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -2654,6 +2654,13 @@ components:
items:
type: string
nullable: true
+ description: List of deployment labels of this prompt version.
+ tags:
+ type: array
+ items:
+ type: string
+ nullable: true
+ description: List of tags to apply to all versions of this prompt.
required:
- name
- prompt
@@ -2672,6 +2679,13 @@ components:
items:
type: string
nullable: true
+ description: List of deployment labels of this prompt version.
+ tags:
+ type: array
+ items:
+ type: string
+ nullable: true
+ description: List of tags to apply to all versions of this prompt.
required:
- name
- prompt
diff --git a/web/public/generated/postman/collection.json b/web/public/generated/postman/collection.json
index be18df492d..65757b2af2 100644
--- a/web/public/generated/postman/collection.json
+++ b/web/public/generated/postman/collection.json
@@ -699,7 +699,7 @@
"auth": null,
"body": {
"mode": "raw",
- "raw": "{\n \"type\": \"chat\",\n \"name\": \"example\",\n \"prompt\": [\n {\n \"role\": \"example\",\n \"content\": \"example\"\n }\n ],\n \"config\": \"UNKNOWN\",\n \"labels\": [\n \"example\"\n ]\n}",
+ "raw": "{\n \"type\": \"chat\",\n \"name\": \"example\",\n \"prompt\": [\n {\n \"role\": \"example\",\n \"content\": \"example\"\n }\n ],\n \"config\": \"UNKNOWN\",\n \"labels\": [\n \"example\"\n ],\n \"tags\": [\n \"example\"\n ]\n}",
"options": {
"raw": {
"language": "json"
diff --git a/web/src/__tests__/prompts.v2.servertest.ts b/web/src/__tests__/prompts.v2.servertest.ts
index 348f4555ed..6ffec26579 100644
--- a/web/src/__tests__/prompts.v2.servertest.ts
+++ b/web/src/__tests__/prompts.v2.servertest.ts
@@ -680,6 +680,71 @@ describe("/api/public/v2/prompts API Endpoint", () => {
expect(fetchedPrompt.body.createdBy).toBe("API");
expect(fetchedPrompt.body.config).toEqual({});
});
+
+ it("should update tags across versions", async () => {
+ const promptName = "prompt-name" + nanoid();
+
+ const createPromptVersion = async (tags?: string[]) => {
+ await makeAPICall("POST", baseURI, {
+ name: promptName,
+ prompt: "This is a test prompt",
+ type: PromptType.Text,
+ ...(tags !== undefined && { tags: tags }),
+ });
+ };
+
+ const fetchPromptVersion = async (version: number) => {
+ const fetchedPrompt = await makeAPICall(
+ "GET",
+ `${baseURI}/${promptName}?version=${version}`,
+ undefined,
+ );
+ expect(fetchedPrompt.status).toBe(200);
+ if (!isPrompt(fetchedPrompt.body)) {
+ throw new Error("Expected body to be a prompt");
+ }
+ return fetchedPrompt.body;
+ };
+
+ // Create version 1 with ["tag"]
+ await createPromptVersion(["tag"]);
+ let fetchedPrompt1 = await fetchPromptVersion(1);
+ expect(fetchedPrompt1.tags).toEqual(["tag"]);
+ expect(fetchedPrompt1.version).toBe(1);
+
+ // Create version 2 with no tags provided (should use tags from version 1)
+ await createPromptVersion();
+ let fetchedPrompt2 = await fetchPromptVersion(2);
+ expect(fetchedPrompt2.tags).toEqual(["tag"]);
+ expect(fetchedPrompt2.version).toBe(2);
+
+ // Create version 3 with ["tag1", "tag2", "tag3"] (should update tags across versions)
+ await createPromptVersion(["tag1", "tag2", "tag3"]);
+ fetchedPrompt1 = await fetchPromptVersion(1);
+ fetchedPrompt2 = await fetchPromptVersion(2);
+ let fetchedPrompt3 = await fetchPromptVersion(3);
+ expect(fetchedPrompt1.tags).toEqual(["tag1", "tag2", "tag3"]);
+ expect(fetchedPrompt1.version).toBe(1);
+ expect(fetchedPrompt2.tags).toEqual(["tag1", "tag2", "tag3"]);
+ expect(fetchedPrompt2.version).toBe(2);
+ expect(fetchedPrompt3.tags).toEqual(["tag1", "tag2", "tag3"]);
+ expect(fetchedPrompt3.version).toBe(3);
+
+ // remove tags
+ await createPromptVersion([]);
+ fetchedPrompt1 = await fetchPromptVersion(1);
+ fetchedPrompt2 = await fetchPromptVersion(2);
+ fetchedPrompt3 = await fetchPromptVersion(3);
+ let fetchedPrompt4 = await fetchPromptVersion(4);
+ expect(fetchedPrompt1.tags).toEqual([]);
+ expect(fetchedPrompt1.version).toBe(1);
+ expect(fetchedPrompt2.tags).toEqual([]);
+ expect(fetchedPrompt2.version).toBe(2);
+ expect(fetchedPrompt3.tags).toEqual([]);
+ expect(fetchedPrompt3.version).toBe(3);
+ expect(fetchedPrompt4.tags).toEqual([]);
+ expect(fetchedPrompt4.version).toBe(4);
+ });
});
describe("when fetching a prompt list", () => {
diff --git a/web/src/features/prompts/server/actions/createPrompt.ts b/web/src/features/prompts/server/actions/createPrompt.ts
index de27aeeda9..1f852d7900 100644
--- a/web/src/features/prompts/server/actions/createPrompt.ts
+++ b/web/src/features/prompts/server/actions/createPrompt.ts
@@ -6,6 +6,8 @@ import { ValidationError } from "@langfuse/shared";
import { jsonSchema } from "@/src/utils/zod";
import { type PrismaClient } from "@langfuse/shared/src/db";
import { LATEST_PROMPT_LABEL } from "@/src/features/prompts/constants";
+import { removeLabelsFromPreviousPromptVersions } from "@/src/features/prompts/server/utils/updatePromptLabels";
+import { updatePromptTagsOnAllVersions } from "@/src/features/prompts/server/utils/updatePromptTags";
export type CreatePromptParams = CreatePromptTRPCType & {
createdBy: string;
@@ -21,6 +23,7 @@ export const createPrompt = async ({
config,
createdBy,
prisma,
+ tags,
}: CreatePromptParams) => {
const latestPrompt = await prisma.prompt.findFirst({
where: { projectId, name },
@@ -35,14 +38,8 @@ export const createPrompt = async ({
const finalLabels = [...labels, LATEST_PROMPT_LABEL]; // Newly created prompts are always labeled as 'latest'
- const previousLabeledPrompts = await prisma.prompt.findMany({
- where: {
- projectId,
- name,
- labels: { hasSome: finalLabels },
- },
- orderBy: [{ version: "desc" }],
- });
+ // If tags are undefined, use the tags from the latest prompt version
+ const finalTags = [...new Set(tags ?? latestPrompt?.tags ?? [])];
const create = [
prisma.prompt.create({
@@ -52,7 +49,7 @@ export const createPrompt = async ({
createdBy,
labels: [...new Set(finalLabels)], // Ensure labels are unique
type,
- tags: latestPrompt?.tags,
+ tags: finalTags,
version: latestPrompt?.version ? latestPrompt.version + 1 : 1,
project: { connect: { id: projectId } },
config: jsonSchema.parse(config),
@@ -62,18 +59,28 @@ export const createPrompt = async ({
if (finalLabels.length > 0)
// If we're creating a new labeled prompt, we must remove those labels on previous prompts since labels are unique
- previousLabeledPrompts.forEach((prevPrompt) => {
- create.push(
- prisma.prompt.update({
- where: { id: prevPrompt.id },
- data: {
- labels: prevPrompt.labels.filter(
- (prevLabel) => !finalLabels.includes(prevLabel),
- ),
- },
- }),
- );
- });
+ create.push(
+ ...(await removeLabelsFromPreviousPromptVersions({
+ prisma,
+ projectId,
+ promptName: name,
+ labelsToRemove: finalLabels,
+ })),
+ );
+
+ const haveTagsChanged =
+ JSON.stringify([...new Set(finalTags)].sort()) !==
+ JSON.stringify([...new Set(latestPrompt?.tags)].sort());
+ if (haveTagsChanged)
+ // If we're creating a new prompt with tags, we must update those tags on previous prompts since tags are consistent across versions
+ create.push(
+ ...(await updatePromptTagsOnAllVersions({
+ prisma,
+ projectId,
+ promptName: name,
+ tags: finalTags,
+ })),
+ );
const [createdPrompt] = await prisma.$transaction(create);
diff --git a/web/src/features/prompts/server/utils/updatePromptLabels.ts b/web/src/features/prompts/server/utils/updatePromptLabels.ts
new file mode 100644
index 0000000000..d751ff16a0
--- /dev/null
+++ b/web/src/features/prompts/server/utils/updatePromptLabels.ts
@@ -0,0 +1,33 @@
+import { type PrismaClient } from "@langfuse/shared/src/db";
+
+export const removeLabelsFromPreviousPromptVersions = async ({
+ prisma,
+ projectId,
+ promptName,
+ labelsToRemove,
+}: {
+ prisma: PrismaClient;
+ projectId: string;
+ promptName: string;
+ labelsToRemove: string[];
+}) => {
+ const previouslyLabeledPrompts = await prisma.prompt.findMany({
+ where: {
+ projectId,
+ name: promptName,
+ labels: { hasSome: labelsToRemove },
+ },
+ orderBy: [{ version: "desc" }],
+ });
+
+ return previouslyLabeledPrompts.map((prevPrompt) =>
+ prisma.prompt.update({
+ where: { id: prevPrompt.id },
+ data: {
+ labels: prevPrompt.labels.filter(
+ (prevLabel) => !labelsToRemove.includes(prevLabel),
+ ),
+ },
+ }),
+ );
+};
diff --git a/web/src/features/prompts/server/utils/updatePromptTags.ts b/web/src/features/prompts/server/utils/updatePromptTags.ts
new file mode 100644
index 0000000000..62ebb164ff
--- /dev/null
+++ b/web/src/features/prompts/server/utils/updatePromptTags.ts
@@ -0,0 +1,28 @@
+import { type PrismaClient } from "@langfuse/shared/src/db";
+
+export const updatePromptTagsOnAllVersions = async ({
+ prisma,
+ projectId,
+ promptName,
+ tags,
+}: {
+ prisma: PrismaClient;
+ projectId: string;
+ promptName: string;
+ tags: string[];
+}) => {
+ const previousVersions = await prisma.prompt.findMany({
+ where: { projectId, name: promptName },
+ });
+
+ if (previousVersions.length === 0) return [];
+
+ return previousVersions.map((prevVersion) =>
+ prisma.prompt.update({
+ where: { id: prevVersion.id },
+ data: {
+ tags: [...new Set(tags)], // Ensure tags are unique
+ },
+ }),
+ );
+};
diff --git a/web/src/features/prompts/server/utils/validation.ts b/web/src/features/prompts/server/utils/validation.ts
index 3ecb0cac1a..ab2f711143 100644
--- a/web/src/features/prompts/server/utils/validation.ts
+++ b/web/src/features/prompts/server/utils/validation.ts
@@ -27,6 +27,7 @@ export const CreateTextPromptSchema = z.object({
type: z.literal(PromptType.Text).optional(),
prompt: z.string(),
config: jsonSchema.nullable().default({}),
+ tags: z.array(z.string()).optional(),
});
export const CreateChatPromptSchema = z.object({
@@ -35,6 +36,7 @@ export const CreateChatPromptSchema = z.object({
type: z.literal(PromptType.Chat),
prompt: z.array(ChatMessageSchema),
config: jsonSchema.nullable().default({}),
+ tags: z.array(z.string()).optional(),
});
export const CreatePromptSchema = z.union([