Skip to content

Commit fa5ab2f

Browse files
authored
fix(prompts): caching with versioned get and previous production fetch (#690)
1 parent 28b7bfc commit fa5ab2f

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

packages/client/src/prompt/promptManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export class PromptManager {
286286
const cacheKey = this.cache.createKey({
287287
name,
288288
label: options?.label,
289+
version: options?.version,
289290
});
290291
const cachedPrompt = this.cache.getIncludingExpired(cacheKey);
291292
if (!cachedPrompt || options?.cacheTtlSeconds === 0) {

tests/e2e/prompts.e2e.test.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {
33
PromptTemplate,
44
MessagesPlaceholder,
55
} from "@langchain/core/prompts";
6-
import { describe, it, expect, beforeEach, vi } from "vitest";
7-
8-
import { LangfuseClient } from "@langfuse/client";
96
import {
7+
LangfuseClient,
108
ChatPromptClient,
119
TextPromptClient,
1210
ChatMessageType,
1311
} from "@langfuse/client";
1412
import type { ChatMessage, ChatMessageWithPlaceholders } from "@langfuse/core";
13+
import { nanoid } from "nanoid";
14+
import { describe, it, expect, beforeEach, vi } from "vitest";
1515

1616
describe("Langfuse Prompts E2E", () => {
1717
let langfuse: LangfuseClient;
@@ -209,6 +209,39 @@ describe("Langfuse Prompts E2E", () => {
209209
expect(result.config).toEqual({ temperature: 0.7 });
210210
});
211211

212+
it("should fetch and cache a prompt with correct version when not in cache", async () => {
213+
const promptName = "test-cache-prompt-versioned-" + nanoid();
214+
215+
// Create two prompts first
216+
await langfuse.prompt.create({
217+
name: promptName,
218+
prompt: "This is a cached prompt with {{variable}}",
219+
config: { temperature: 0.7 },
220+
labels: ["production"],
221+
});
222+
223+
await langfuse.prompt.create({
224+
name: promptName,
225+
prompt: "This is a cached prompt with {{variable}}",
226+
config: { temperature: 0.7 },
227+
});
228+
229+
// Get it (should fetch and cache)
230+
const result = await langfuse.prompt.get(promptName);
231+
232+
expect(result).toBeInstanceOf(TextPromptClient);
233+
expect(result.name).toBe(promptName);
234+
expect(result.version).toBe(1);
235+
236+
const result2 = await langfuse.prompt.get(promptName, {
237+
version: 2,
238+
});
239+
240+
expect(result2).toBeInstanceOf(TextPromptClient);
241+
expect(result2.name).toBe(promptName);
242+
expect(result2.version).toBe(2);
243+
});
244+
212245
it("should throw an error if prompt not found", async () => {
213246
await expect(
214247
langfuse.prompt.get("non-existent-prompt"),

tests/e2e/vercel-ai-sdk.e2e.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ describe("Vercel AI SDK integration E2E tests", () => {
716716
(o: any) => o.type === "GENERATION",
717717
);
718718

719-
expect(generations.length).toBe(2);
719+
expect(generations.length).toBe(1);
720720

721721
const generation = generations.find((g) => g.name?.includes("doGenerate"))!;
722722

@@ -780,7 +780,7 @@ describe("Vercel AI SDK integration E2E tests", () => {
780780
(o: any) => o.type === "GENERATION",
781781
);
782782

783-
expect(generations.length).toBe(2);
783+
expect(generations.length).toBe(1);
784784

785785
const generation = generations.find((g) => g.name?.includes("doGenerate"))!;
786786

0 commit comments

Comments
 (0)