Skip to content

Commit 10a1a5a

Browse files
committed
feat: implemented support for requests with schema
1 parent cf2f28f commit 10a1a5a

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

scrapegraph-js/src/smartScraper.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import axios from 'axios';
2-
import handleError from './utils/handleError.js'
2+
import handleError from './utils/handleError.js';
3+
import { ZodType } from 'zod';
4+
import { zodToJsonSchema } from 'zod-to-json-schema';
35

46
/**
57
* Scrape and extract structured data from a webpage using ScrapeGraph AI.
@@ -25,12 +27,11 @@ export async function smartScraper(apiKey, url, prompt, schema = null) {
2527
};
2628

2729
if (schema) {
28-
payload.output_schema = {
29-
description: schema.title || 'Schema',
30-
name: schema.title || 'Schema',
31-
properties: schema.properties || {},
32-
required: schema.required || []
33-
};
30+
if (schema instanceof ZodType) {
31+
payload.output_schema = zodToJsonSchema(schema);
32+
} else {
33+
throw new Error('The schema must be an instance of a valid Zod schema');
34+
}
3435
}
3536

3637
try {

0 commit comments

Comments
 (0)