Skip to content

Commit e65da4d

Browse files
authored
Merge pull request #1001 from Mirza-Samad-Ahmed-Baig/fix-schema-transform-bugs
Fix critical schema transformation bugs and improve logging
2 parents 72b43b3 + 4cc1fc5 commit e65da4d

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

scrapegraphai/graphs/smart_scraper_graph.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
SmartScraperGraph Module
33
"""
44

5+
import logging
56
from typing import Optional, Type
67

78
from pydantic import BaseModel
89

10+
# Initialize logger
11+
logger = logging.getLogger(__name__)
12+
913
from ..nodes import (
1014
ConditionalNode,
1115
FetchNode,
@@ -92,9 +96,12 @@ def _create_graph(self) -> BaseGraph:
9296
user_prompt=self.prompt,
9397
)
9498

95-
# Print the response
96-
print(f"Request ID: {response['request_id']}")
97-
print(f"Result: {response['result']}")
99+
# Use logging instead of print for better production practices
100+
if 'request_id' in response and 'result' in response:
101+
logger.info(f"Request ID: {response['request_id']}")
102+
logger.info(f"Result: {response['result']}")
103+
else:
104+
logger.warning("Missing expected keys in response.")
98105

99106
sgai_client.close()
100107

scrapegraphai/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
from .proxy_rotation import Proxy, parse_or_search_proxy, search_proxy_servers
4444
from .save_audio_from_bytes import save_audio_from_bytes
4545
from .save_code_to_file import save_code_to_file
46-
from .schema_trasform import transform_schema
46+
from .schema_trasform import transform_schema # Note: filename has typo but kept for compatibility
4747
from .screenshot_scraping.screenshot_preparation import (
4848
crop_image,
4949
select_area_with_ipywidget,

scrapegraphai/utils/schema_trasform.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
This utility function trasfrom the pydantic schema into a more comprehensible schema.
2+
This utility function transforms the pydantic schema into a more comprehensible schema.
33
"""
44

55

@@ -19,25 +19,35 @@ def process_properties(properties):
1919
for key, value in properties.items():
2020
if "type" in value:
2121
if value["type"] == "array":
22-
if "$ref" in value["items"]:
22+
if "items" in value and "$ref" in value["items"]:
2323
ref_key = value["items"]["$ref"].split("/")[-1]
24-
result[key] = [
25-
process_properties(
26-
pydantic_schema["$defs"][ref_key]["properties"]
27-
)
28-
]
29-
else:
24+
if "$defs" in pydantic_schema and ref_key in pydantic_schema["$defs"]:
25+
result[key] = [
26+
process_properties(
27+
pydantic_schema["$defs"][ref_key].get("properties", {})
28+
)
29+
]
30+
else:
31+
result[key] = ["object"] # fallback for missing reference
32+
elif "items" in value and "type" in value["items"]:
3033
result[key] = [value["items"]["type"]]
34+
else:
35+
result[key] = ["unknown"] # fallback for malformed array
3136
else:
3237
result[key] = {
3338
"type": value["type"],
3439
"description": value.get("description", ""),
3540
}
3641
elif "$ref" in value:
3742
ref_key = value["$ref"].split("/")[-1]
38-
result[key] = process_properties(
39-
pydantic_schema["$defs"][ref_key]["properties"]
40-
)
43+
if "$defs" in pydantic_schema and ref_key in pydantic_schema["$defs"]:
44+
result[key] = process_properties(
45+
pydantic_schema["$defs"][ref_key].get("properties", {})
46+
)
47+
else:
48+
result[key] = {"type": "object", "description": "Missing reference"} # fallback
4149
return result
4250

51+
if "properties" not in pydantic_schema:
52+
raise ValueError("Invalid pydantic schema: missing 'properties' key")
4353
return process_properties(pydantic_schema["properties"])

0 commit comments

Comments
 (0)