Skip to content

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

fix(models): accept any usage key when reading model prices (#19418)

Hassieb Pakzadae32a5a7a69e
diff --git a/web/src/__tests__/server/models-trpc.servertest.ts b/web/src/__tests__/server/models-trpc.servertest.ts
new file mode 100644
index 0000000000..4d95426f70
--- /dev/null
+++ b/web/src/__tests__/server/models-trpc.servertest.ts
@@ -0,0 +1,97 @@
+import { testFeatureFlags } from "@/src/__tests__/fixtures/feature-flags";
+import type { Session } from "next-auth";
+import { randomUUID } from "crypto";
+import { describe, expect, it } from "vitest";
+import { prisma } from "@langfuse/shared/src/db";
+import { appRouter } from "@/src/server/api/root";
+import { createInnerTRPCContext } from "@/src/server/api/trpc";
+import { createOrgProjectAndApiKey } from "@langfuse/shared/src/server";
+
+const createCaller = async () => {
+ const { project, org } = await createOrgProjectAndApiKey({ plan: "Team" });
+ const session: Session = {
+ expires: "1",
+ user: {
+ id: `user-${randomUUID()}`,
+ name: "Models Test User",
+ canCreateOrganizations: true,
+ admin: true,
+ organizations: [
+ {
+ id: org.id,
+ name: org.name,
+ role: "OWNER",
+ plan: "cloud:team",
+ cloudConfig: undefined,
+ metadata: {},
+ aiFeaturesEnabled: false,
+ aiTelemetryEnabled: false,
+ projects: [
+ {
+ id: project.id,
+ role: "ADMIN",
+ retentionDays: 30,
+ deletedAt: null,
+ name: project.name,
+ hasTraces: true,
+ metadata: {},
+ createdAt: new Date().toISOString(),
+ },
+ ],
+ },
+ ],
+ featureFlags: testFeatureFlags(),
+ },
+ environment: {} as any,
+ };
+
+ const ctx = createInnerTRPCContext({ session, headers: {} });
+ return {
+ projectId: project.id,
+ caller: appRouter.createCaller({ ...ctx, prisma }),
+ };
+};
+
+describe("models tRPC router", () => {
+ // The public API and the shared pricing-tier schema accept any string as a
+ // usage key, so rows written through them must stay readable here.
+ it("lists and reads models whose usage keys contain dots", async () => {
+ const { projectId, caller } = await createCaller();
+ const modelName = `dotted-keys-${randomUUID()}`;
+ const prices = {
+ input: 0.000001,
+ "input.cached": 0.0000005,
+ "output.reasoning-tokens": 0,
+ };
+
+ const { id: modelId } = await caller.models.upsert({
+ modelId: null,
+ projectId,
+ modelName,
+ matchPattern: `(?i)^(${modelName})$`,
+ pricingTiers: [
+ {
+ name: "Standard",
+ isDefault: true,
+ priority: 0,
+ conditions: [],
+ prices,
+ },
+ ],
+ });
+
+ const list = await caller.models.getAll({
+ projectId,
+ searchString: modelName,
+ page: 0,
+ limit: 50,
+ });
+
+ expect(list.totalCount).toBe(1);
+ expect(list.models).toHaveLength(1);
+ expect(list.models[0].pricingTiers[0].prices).toEqual(prices);
+
+ const model = await caller.models.getById({ projectId, modelId });
+ expect(model.pricingTiers[0].prices).toEqual(prices);
+ });
+});
diff --git a/web/src/features/models/validation.ts b/web/src/features/models/validation.ts
index 3676488571..a8ab1e1f2c 100644
--- a/web/src/features/models/validation.ts
+++ b/web/src/features/models/validation.ts
@@ -9,8 +9,10 @@ import {
validatePricingTiers,
} from "@langfuse/shared";
-const USAGE_TYPE_PATTERN = /^[a-zA-Z0-9_-]+$/;
-const UsageTypeSchema = z.string().regex(USAGE_TYPE_PATTERN);
+// Usage keys follow the shared write contract (`PricingTierInputSchema`), which
+// accepts any string. Narrowing them here makes rows written through the
+// public API unreadable, and one such row fails the whole model list.
+const UsageTypeSchema = z.string();
const PriceSchema = z.number().nonnegative();
const TokenizerSchema = z.enum(["openai", "claude"]).nullish();
// Input version: allows optional prices for form
@@ -42,14 +44,7 @@ const FormUsageTypeSchema = z.object({
key: z.string(),
// Trimmed here, so every later check and the submitted payload see one
// normalised name. The value in the input is untouched while typing.
- name: z
- .string()
- .trim()
- .min(1, "Usage type required")
- .regex(
- USAGE_TYPE_PATTERN,
- "Only letters, numbers, hyphens and underscores",
- ),
+ name: z.string().trim().min(1, "Usage type required"),
});
export type FormUsageType = z.infer<typeof FormUsageTypeSchema>;