Skip to content

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

fix(llm-connection-vertex): use zodV3 for structured output calls (#7468)

Hassieb Pakzad39feb65d902c
diff --git a/packages/shared/src/server/llm/fetchLLMCompletion.ts b/packages/shared/src/server/llm/fetchLLMCompletion.ts
index b48de17a71..6de2f56299 100644
--- a/packages/shared/src/server/llm/fetchLLMCompletion.ts
+++ b/packages/shared/src/server/llm/fetchLLMCompletion.ts
@@ -1,4 +1,6 @@
-import { type ZodSchema } from "zod/v4";
+// We need to use Zod3 for structured outputs due to a bug in
+// ChatVertexAI. See issue: https://github.com/langfuse/langfuse/issues/7429
+import { type ZodSchema } from "zod/v3";
import { ChatAnthropic } from "@langchain/anthropic";
import { ChatVertexAI } from "@langchain/google-vertexai";
diff --git a/web/src/features/evals/server/router.ts b/web/src/features/evals/server/router.ts
index 0347e70e5f..d77fcd7a03 100644
--- a/web/src/features/evals/server/router.ts
+++ b/web/src/features/evals/server/router.ts
@@ -1,4 +1,5 @@
import { z } from "zod/v4";
+import { z as zodV3 } from "zod/v3";
import {
createTRPCRouter,
protectedProjectProcedure,
@@ -832,9 +833,9 @@ export const evalRouter = createTRPCRouter({
adapter: matchingLLMKey.adapter,
...input.modelParams,
},
- structuredOutputSchema: z.object({
- score: z.string(),
- reasoning: z.string(),
+ structuredOutputSchema: zodV3.object({
+ score: zodV3.string(),
+ reasoning: zodV3.string(),
}),
config: matchingLLMKey.config,
})
diff --git a/worker/src/features/evaluation/evalService.ts b/worker/src/features/evaluation/evalService.ts
index e1f717546f..e0c3abe19a 100644
--- a/worker/src/features/evaluation/evalService.ts
+++ b/worker/src/features/evaluation/evalService.ts
@@ -1,6 +1,7 @@
import { randomUUID } from "crypto";
import { sql } from "kysely";
import { z } from "zod/v4";
+import { z as zodV3 } from "zod/v3";
import { JobConfigState } from "@prisma/client";
import {
QueueJobs,
@@ -581,9 +582,9 @@ export const evaluate = async ({
throw new InvalidRequestError("Output schema not found");
}
- const evalScoreSchema = z.object({
- reasoning: z.string().describe(parsedOutputSchema.reasoning),
- score: z.number().describe(parsedOutputSchema.score),
+ const evalScoreSchema = zodV3.object({
+ reasoning: zodV3.string().describe(parsedOutputSchema.reasoning),
+ score: zodV3.number().describe(parsedOutputSchema.score),
});
const modelConfig = await DefaultEvalModelService.fetchValidModelConfig(
diff --git a/worker/src/features/utilities.ts b/worker/src/features/utilities.ts
index 6521652927..0785767b78 100644
--- a/worker/src/features/utilities.ts
+++ b/worker/src/features/utilities.ts
@@ -6,12 +6,14 @@ import {
type TraceParams,
} from "@langfuse/shared/src/server";
import { ApiError, LLMApiKeySchema, ZodModelConfig } from "@langfuse/shared";
-import { z, ZodSchema } from "zod/v4";
+import { z } from "zod/v4";
+import { z as zodV3 } from "zod/v3";
+import { ZodSchema as ZodV3Schema } from "zod/v3";
import { decrypt } from "@langfuse/shared/encryption";
import { tokenCount } from "./tokenisation/usage";
import Handlebars from "handlebars";
-export async function callStructuredLLM<T extends ZodSchema>(
+export async function callStructuredLLM<T extends ZodV3Schema>(
jeId: string,
llmApiKey: z.infer<typeof LLMApiKeySchema>,
messages: ChatMessage[],
@@ -19,7 +21,7 @@ export async function callStructuredLLM<T extends ZodSchema>(
provider: string,
model: string,
structuredOutputSchema: T,
-): Promise<z.infer<T>> {
+): Promise<zodV3.infer<T>> {
try {
const { completion } = await fetchLLMCompletion({
streaming: false,