@@ -37,6 +37,8 @@ class LocalScraperTool(BaseTool):
37
37
Key init args:
38
38
api_key: Your ScrapeGraph AI API key. If not provided, will look for SGAI_API_KEY env var.
39
39
client: Optional pre-configured ScrapeGraph client instance.
40
+ llm_output_schema: Optional Pydantic model or dictionary schema to structure the output.
41
+ If provided, the tool will ensure the output conforms to this schema.
40
42
41
43
Instantiate:
42
44
.. code-block:: python
@@ -49,6 +51,16 @@ class LocalScraperTool(BaseTool):
49
51
# Or provide API key directly
50
52
tool = LocalScraperTool(api_key="your-api-key")
51
53
54
+ # Optionally, you can provide an output schema:
55
+ from pydantic import BaseModel, Field
56
+
57
+ class CompanyInfo(BaseModel):
58
+ name: str = Field(description="Company name")
59
+ description: str = Field(description="Company description")
60
+ email: str = Field(description="Contact email")
61
+
62
+ tool_with_schema = LocalScraperTool(llm_output_schema=CompanyInfo)
63
+
52
64
Use the tool:
53
65
.. code-block:: python
54
66
@@ -71,21 +83,21 @@ class LocalScraperTool(BaseTool):
71
83
})
72
84
73
85
print(result)
86
+ # Without schema:
74
87
# {
75
88
# "description": "We are a technology company focused on AI solutions",
76
89
# "contact": {
77
90
78
91
# "phone": "(555) 123-4567"
79
92
# }
80
93
# }
81
-
82
- Async usage:
83
- .. code-block:: python
84
-
85
- result = await tool.ainvoke({
86
- "user_prompt": "Extract contact information",
87
- "website_html": html_content
88
- })
94
+ #
95
+ # With CompanyInfo schema:
96
+ # {
97
+ # "name": "Company Name",
98
+ # "description": "We are a technology company focused on AI solutions",
99
+
100
+ # }
89
101
"""
90
102
91
103
name : str = "LocalScraper"
@@ -96,6 +108,7 @@ class LocalScraperTool(BaseTool):
96
108
return_direct : bool = True
97
109
client : Optional [Client ] = None
98
110
api_key : str
111
+ llm_output_schema : Optional [Type [BaseModel ]] = None
99
112
100
113
@model_validator (mode = "before" )
101
114
@classmethod
@@ -117,10 +130,23 @@ def _run(
117
130
"""Use the tool to extract data from a website."""
118
131
if not self .client :
119
132
raise ValueError ("Client not initialized" )
120
- response = self .client .localscraper (
121
- website_html = website_html ,
122
- user_prompt = user_prompt ,
123
- )
133
+
134
+ if self .llm_output_schema is None :
135
+ response = self .client .localscraper (
136
+ website_html = website_html ,
137
+ user_prompt = user_prompt ,
138
+ )
139
+ elif isinstance (self .llm_output_schema , type ) and issubclass (
140
+ self .llm_output_schema , BaseModel
141
+ ):
142
+ response = self .client .localscraper (
143
+ website_html = website_html ,
144
+ user_prompt = user_prompt ,
145
+ output_schema = self .llm_output_schema ,
146
+ )
147
+ else :
148
+ raise ValueError ("llm_output_schema must be a Pydantic model class" )
149
+
124
150
return response ["result" ]
125
151
126
152
async def _arun (
0 commit comments