Skip to content

fix(utils): keep Optional fields in transform_schema - #1160

Open
MohammadHijjawi97 wants to merge 1 commit into
ScrapeGraphAI:pre/betafrom
MohammadHijjawi97:fix/transform-schema-optional-fields
Open

MohammadHijjawi97 wants to merge 1 commit into
ScrapeGraphAI:pre/betafrom
MohammadHijjawi97:fix/transform-schema-optional-fields

Conversation

@MohammadHijjawi97

Copy link
Copy Markdown

Summary

Pydantic v2 emits Optional[X] fields as anyOf: [X, {"type": "null"}], with no top-level type or $ref. transform_schema only handles those two keys. As a result, every Optional field is silently dropped from the simplified schema that GenerateCodeNode, PromptRefinerNode and ReasoningNode pass to the LLM:

class Product(BaseModel):
    title: str
    price: Optional[float] = None

transform_schema(Product.model_json_schema())
# before: {'title': {'type': 'string', 'description': ''}}   <- price missing
# after:  {'title': {...}, 'price': {'type': 'number', 'description': ''}}

Changes

  • When a property has anyOf with exactly one non-null variant, describe it as that variant, keeping the field's own description. It then goes through the existing type / array / $ref handling, so Optional scalars, lists and nested models are all covered. Unions of several non-null types are left as they were.
  • Added tests/utils/schema_transform_test.py.

Testing

  • pytest tests/utils/schema_transform_test.py: fails on pre/beta, passes with the fix.
  • ruff check on the changed files; black --check / isort --check-only on the new test file.

Pydantic v2 emits Optional[X] fields as `anyOf: [X, {"type": "null"}]`
with no top-level `type` or `$ref`. transform_schema only handled those
two keys, so every Optional field was silently dropped from the
simplified schema that GenerateCodeNode, PromptRefinerNode and
ReasoningNode send to the LLM.

Unwrap the single non-null variant of an anyOf and describe the field
as that type, keeping the field's own description.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant