From 62c128850acee922537b48312cba6b4dce6b5733 Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 10:56:03 -0800 Subject: [PATCH 1/7] patch handling for non object extract --- packages/core/lib/utils.ts | 2 +- packages/core/lib/v3/handlers/extractHandler.ts | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) 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..6091b2bb7 100644 --- a/packages/core/lib/v3/handlers/extractHandler.ts +++ b/packages/core/lib/v3/handlers/extractHandler.ts @@ -1,6 +1,6 @@ // 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"; @@ -151,10 +151,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] = From 2e89737686ef3d16047ccde05e7d839b3dba8611 Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:00:09 -0800 Subject: [PATCH 2/7] add changeset --- .changeset/khaki-women-travel.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/khaki-women-travel.md 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 From 33dd5641c806c0a7d55ae65a422caa7ac55d092b Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:04:32 -0800 Subject: [PATCH 3/7] format --- packages/core/lib/v3/handlers/extractHandler.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/v3/handlers/extractHandler.ts b/packages/core/lib/v3/handlers/extractHandler.ts index 6091b2bb7..03d6c4c94 100644 --- a/packages/core/lib/v3/handlers/extractHandler.ts +++ b/packages/core/lib/v3/handlers/extractHandler.ts @@ -1,6 +1,11 @@ // lib/v3/handlers/extractHandler.ts import { extract as runExtract } from "../../inference"; -import { getZFactory, 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"; From dcdd211f3fa6db372d05f9cfc230bf9672bfc83c Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:09:02 -0800 Subject: [PATCH 4/7] remove unused import --- packages/core/lib/v3/handlers/extractHandler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/core/lib/v3/handlers/extractHandler.ts b/packages/core/lib/v3/handlers/extractHandler.ts index 03d6c4c94..9def5154d 100644 --- a/packages/core/lib/v3/handlers/extractHandler.ts +++ b/packages/core/lib/v3/handlers/extractHandler.ts @@ -9,7 +9,6 @@ import { 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"; From 6cb6405a1143c6a1ffcbd2696dc9d21a834690b4 Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:19:05 -0800 Subject: [PATCH 5/7] update test --- packages/core/tests/public-types.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/tests/public-types.test.ts b/packages/core/tests/public-types.test.ts index 611f1d17c..0c6eb9df9 100644 --- a/packages/core/tests/public-types.test.ts +++ b/packages/core/tests/public-types.test.ts @@ -66,6 +66,7 @@ const publicApiShape = { connectToMCPServer: Stagehand.connectToMCPServer, default: StagehandDefaultExport, defaultExtractSchema: Stagehand.defaultExtractSchema, + getZFactory: Stagehand.getZFactory, getZodType: Stagehand.getZodType, injectUrls: Stagehand.injectUrls, isRunningInBun: Stagehand.isRunningInBun, From b48c4b0e6f2db00dd34baed52169735d962a6425 Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:26:45 -0800 Subject: [PATCH 6/7] restore Stagehand.getZFactory --- packages/core/lib/v3/v3.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/v3/v3.ts b/packages/core/lib/v3/v3.ts index f57067a46..8eba54efb 100644 --- a/packages/core/lib/v3/v3.ts +++ b/packages/core/lib/v3/v3.ts @@ -5,7 +5,7 @@ import path from "path"; import process from "process"; import { z } from "zod"; import type { InferStagehandSchema, StagehandZodSchema } from "./zodCompat"; -import { loadApiKeyFromEnv } from "../utils"; +import { loadApiKeyFromEnv, getZFactory } from "../utils"; import { StagehandLogger, LoggerOptions } from "../logger"; import { ActCache } from "./cache/ActCache"; import { AgentCache } from "./cache/AgentCache"; @@ -162,6 +162,7 @@ export class V3 { private readonly instanceId: string; private static _processGuardsInstalled = false; private static _instances: Set = new Set(); + public static getZFactory = getZFactory; private cacheStorage: CacheStorage; private actCache: ActCache; private agentCache: AgentCache; From 2fb7c422fe6888cb93c826a5facf4c2b27af4e2a Mon Sep 17 00:00:00 2001 From: tkattkat Date: Thu, 20 Nov 2025 11:27:23 -0800 Subject: [PATCH 7/7] Revert "restore Stagehand.getZFactory" This reverts commit b48c4b0e6f2db00dd34baed52169735d962a6425. --- packages/core/lib/v3/v3.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/lib/v3/v3.ts b/packages/core/lib/v3/v3.ts index 8eba54efb..f57067a46 100644 --- a/packages/core/lib/v3/v3.ts +++ b/packages/core/lib/v3/v3.ts @@ -5,7 +5,7 @@ import path from "path"; import process from "process"; import { z } from "zod"; import type { InferStagehandSchema, StagehandZodSchema } from "./zodCompat"; -import { loadApiKeyFromEnv, getZFactory } from "../utils"; +import { loadApiKeyFromEnv } from "../utils"; import { StagehandLogger, LoggerOptions } from "../logger"; import { ActCache } from "./cache/ActCache"; import { AgentCache } from "./cache/AgentCache"; @@ -162,7 +162,6 @@ export class V3 { private readonly instanceId: string; private static _processGuardsInstalled = false; private static _instances: Set = new Set(); - public static getZFactory = getZFactory; private cacheStorage: CacheStorage; private actCache: ActCache; private agentCache: AgentCache;