fix(utils): keep Optional fields in transform_schema - #1160
Open
MohammadHijjawi97 wants to merge 1 commit into
Open
MohammadHijjawi97 wants to merge 1 commit into
MohammadHijjawi97 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pydantic v2 emits
Optional[X]fields asanyOf: [X, {"type": "null"}], with no top-leveltypeor$ref.transform_schemaonly handles those two keys. As a result, every Optional field is silently dropped from the simplified schema thatGenerateCodeNode,PromptRefinerNodeandReasoningNodepass to the LLM:Changes
anyOfwith exactly one non-null variant, describe it as that variant, keeping the field's own description. It then goes through the existing type / array /$refhandling, so Optional scalars, lists and nested models are all covered. Unions of several non-null types are left as they were.tests/utils/schema_transform_test.py.Testing
pytest tests/utils/schema_transform_test.py: fails onpre/beta, passes with the fix.ruff checkon the changed files;black --check/isort --check-onlyon the new test file.