Skip to content

Commit 91a1ca0

Browse files
authored
patch handling for non object extract (#1295)
# why currently when importing zod vrom zod/v3, and extracting a non object, it will throw an error "def is undefined" # what changed replace static z.object with factory.object, to ensure we always wrap it in correct zod versions schema # test plan tested locally
1 parent 0bb9117 commit 91a1ca0

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

.changeset/khaki-women-travel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@browserbasehq/stagehand": patch
3+
---
4+
5+
Patch zod handling of non objects in extract

packages/core/lib/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const zFactories = {
1515
v3: z3 as unknown as typeof z,
1616
};
1717

18-
function getZFactory(schema: StagehandZodSchema): typeof z {
18+
export function getZFactory(schema: StagehandZodSchema): typeof z {
1919
return isZod4Schema(schema) ? zFactories.v4 : zFactories.v3;
2020
}
2121

packages/core/lib/v3/handlers/extractHandler.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// lib/v3/handlers/extractHandler.ts
22
import { extract as runExtract } from "../../inference";
3-
import { getZodType, injectUrls, transformSchema } from "../../utils";
3+
import {
4+
getZFactory,
5+
getZodType,
6+
injectUrls,
7+
transformSchema,
8+
} from "../../utils";
49
import { v3Logger } from "../logger";
510
import { V3FunctionName } from "../types/public/methods";
611
import { captureHybridSnapshot } from "../understudy/a11y/snapshot";
7-
import { z } from "zod";
812
import type { ZodTypeAny } from "zod";
913
import { LLMClient } from "../llm/LLMClient";
1014
import { ExtractHandlerParams } from "../types/private/handlers";
@@ -151,10 +155,11 @@ export class ExtractHandler {
151155
// Ensure we pass an object schema into inference; wrap non-object schemas
152156
const isObjectSchema = getZodType(baseSchema) === "object";
153157
const WRAP_KEY = "value" as const;
158+
const factory = getZFactory(baseSchema);
154159
const objectSchema: StagehandZodObject = isObjectSchema
155160
? (baseSchema as StagehandZodObject)
156-
: (z.object({
157-
[WRAP_KEY]: baseSchema as unknown as ZodTypeAny,
161+
: (factory.object({
162+
[WRAP_KEY]: baseSchema as ZodTypeAny,
158163
}) as StagehandZodObject);
159164

160165
const [transformedSchema, urlFieldPaths] =

0 commit comments

Comments
 (0)