Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-women-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

Patch zod handling of non objects in extract
2 changes: 1 addition & 1 deletion packages/core/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 9 additions & 4 deletions packages/core/lib/v3/handlers/extractHandler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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] =
Expand Down