Skip to content

Commit 3ccf335

Browse files
fix: missing URLs for extract() with array schema (#1107)
# why - before this change, when we convert `z.string().url()` to an ID, if it was inside a `z.array()`, it was not getting converted back into a URL - this meant that if you defined a schema like this: ```ts schema: z.object({ records: z.array(z.string().url()), }) ``` you would receive an array like this: ``` { records: [ '0-302', '0-309', '0-316', '0-323', '0-330', '0-337', '0-344', '0-351', '0-358', '0-365' ] } ``` - with this change, you will now receive the actual URLs, ie: ``` { records: [ 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10003-10041.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10143%20(C06932208).pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10143.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10156.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10004-10213.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10005-10321.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10006-10247.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10007-10345.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10009-10021.pdf', 'https://www.archives.gov/files/research/jfk/releases/2025/0318/104-10009-10222.pdf' ] } ``` # what changed - updated the `injectUrls` function so that when it hits an array and there is not deeper path, it loops through the array and injects the URLs # test plan - evals
1 parent 0791404 commit 3ccf335

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

.changeset/twelve-suits-peel.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+
fix: url extraction not working inside an array

lib/utils.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -382,11 +382,29 @@ export function injectUrls(
382382
idToUrlMapping: Record<string, string>,
383383
): void {
384384
if (path.length === 0) return;
385+
const toId = (value: unknown): string | undefined => {
386+
if (typeof value === "number") {
387+
return String(value);
388+
}
389+
if (typeof value === "string" && ID_PATTERN.test(value)) {
390+
return value;
391+
}
392+
return undefined;
393+
};
385394
const [key, ...rest] = path;
386395

387396
if (key === "*") {
388397
if (Array.isArray(obj)) {
389-
for (const item of obj) injectUrls(item, rest, idToUrlMapping);
398+
if (rest.length === 0) {
399+
for (let i = 0; i < obj.length; i += 1) {
400+
const id = toId(obj[i]);
401+
if (id !== undefined) {
402+
obj[i] = idToUrlMapping[id] ?? "";
403+
}
404+
}
405+
} else {
406+
for (const item of obj) injectUrls(item, rest, idToUrlMapping);
407+
}
390408
}
391409
return;
392410
}
@@ -395,14 +413,7 @@ export function injectUrls(
395413
const record = obj as Record<string | number, unknown>;
396414
if (path.length === 1) {
397415
const fieldValue = record[key];
398-
399-
const id =
400-
typeof fieldValue === "number"
401-
? String(fieldValue)
402-
: typeof fieldValue === "string" && ID_PATTERN.test(fieldValue)
403-
? fieldValue
404-
: undefined;
405-
416+
const id = toId(fieldValue);
406417
if (id !== undefined) {
407418
record[key] = idToUrlMapping[id] ?? "";
408419
}

0 commit comments

Comments
 (0)