diff --git a/.changeset/khaki-women-travel.md b/.changeset/khaki-women-travel.md new file mode 100644 index 000000000..4387e88df --- /dev/null +++ b/.changeset/khaki-women-travel.md @@ -0,0 +1,5 @@ +--- +"@browserbasehq/stagehand": patch +--- + +Patch zod handling of non objects in extract diff --git a/packages/core/lib/utils.ts b/packages/core/lib/utils.ts index f6dfadad8..ade524cb9 100644 --- a/packages/core/lib/utils.ts +++ b/packages/core/lib/utils.ts @@ -15,7 +15,7 @@ const zFactories = { v3: z3 as unknown as typeof z, }; -function getZFactory(schema: StagehandZodSchema): typeof z { +export function getZFactory(schema: StagehandZodSchema): typeof z { return isZod4Schema(schema) ? zFactories.v4 : zFactories.v3; } diff --git a/packages/core/lib/v3/handlers/extractHandler.ts b/packages/core/lib/v3/handlers/extractHandler.ts index 20d8282a0..9def5154d 100644 --- a/packages/core/lib/v3/handlers/extractHandler.ts +++ b/packages/core/lib/v3/handlers/extractHandler.ts @@ -1,10 +1,14 @@ // lib/v3/handlers/extractHandler.ts import { extract as runExtract } from "../../inference"; -import { getZodType, injectUrls, transformSchema } from "../../utils"; +import { + getZFactory, + getZodType, + injectUrls, + transformSchema, +} from "../../utils"; import { v3Logger } from "../logger"; import { V3FunctionName } from "../types/public/methods"; import { captureHybridSnapshot } from "../understudy/a11y/snapshot"; -import { z } from "zod"; import type { ZodTypeAny } from "zod"; import { LLMClient } from "../llm/LLMClient"; import { ExtractHandlerParams } from "../types/private/handlers"; @@ -151,10 +155,11 @@ export class ExtractHandler { // Ensure we pass an object schema into inference; wrap non-object schemas const isObjectSchema = getZodType(baseSchema) === "object"; const WRAP_KEY = "value" as const; + const factory = getZFactory(baseSchema); const objectSchema: StagehandZodObject = isObjectSchema ? (baseSchema as StagehandZodObject) - : (z.object({ - [WRAP_KEY]: baseSchema as unknown as ZodTypeAny, + : (factory.object({ + [WRAP_KEY]: baseSchema as ZodTypeAny, }) as StagehandZodObject); const [transformedSchema, urlFieldPaths] =