Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scrapegraphai/utils/schema_trasform.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def transform_schema(pydantic_schema):
def process_properties(properties):
result = {}
for key, value in properties.items():
if "anyOf" in value:
# Optional[X] is emitted as anyOf [X, null]: describe it as X
variants = [v for v in value["anyOf"] if v.get("type") != "null"]
if len(variants) == 1:
value = {**value, **variants[0]}
if "type" in value:
if value["type"] == "array":
if "items" in value and "$ref" in value["items"]:
Expand Down
27 changes: 27 additions & 0 deletions tests/utils/schema_transform_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List, Optional

from pydantic import BaseModel, Field

from scrapegraphai.utils.schema_trasform import transform_schema


class Seller(BaseModel):
name: str


class Product(BaseModel):
title: str = Field(description="Product title")
price: Optional[float] = Field(default=None, description="Price in USD")
tags: Optional[List[str]] = None
seller: Optional[Seller] = None


def test_transform_schema_keeps_optional_fields():
result = transform_schema(Product.model_json_schema())

assert result == {
"title": {"type": "string", "description": "Product title"},
"price": {"type": "number", "description": "Price in USD"},
"tags": ["string"],
"seller": {"name": {"type": "string", "description": ""}},
}