@@ -37,6 +37,8 @@ class LocalScraperTool(BaseTool):
3737 Key init args:
3838 api_key: Your ScrapeGraph AI API key. If not provided, will look for SGAI_API_KEY env var.
3939 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.
4042
4143 Instantiate:
4244 .. code-block:: python
@@ -49,6 +51,16 @@ class LocalScraperTool(BaseTool):
4951 # Or provide API key directly
5052 tool = LocalScraperTool(api_key="your-api-key")
5153
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+
5264 Use the tool:
5365 .. code-block:: python
5466
@@ -71,21 +83,21 @@ class LocalScraperTool(BaseTool):
7183 })
7284
7385 print(result)
86+ # Without schema:
7487 # {
7588 # "description": "We are a technology company focused on AI solutions",
7689 # "contact": {
77907891 # "phone": "(555) 123-4567"
7992 # }
8093 # }
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+ # }
89101 """
90102
91103 name : str = "LocalScraper"
@@ -96,6 +108,7 @@ class LocalScraperTool(BaseTool):
96108 return_direct : bool = True
97109 client : Optional [Client ] = None
98110 api_key : str
111+ llm_output_schema : Optional [Type [BaseModel ]] = None
99112
100113 @model_validator (mode = "before" )
101114 @classmethod
@@ -117,10 +130,23 @@ def _run(
117130 """Use the tool to extract data from a website."""
118131 if not self .client :
119132 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+
124150 return response ["result" ]
125151
126152 async def _arun (
0 commit comments