Allow bare dict / Dict[Any, ...] fields in json() schema generation#1494
Open
ErenAta16 wants to merge 1 commit into
Open
Allow bare dict / Dict[Any, ...] fields in json() schema generation#1494ErenAta16 wants to merge 1 commit into
ErenAta16 wants to merge 1 commit into
Conversation
GenerateJsonSchemaSafe rejected any dict whose key type wasn't exactly 'str', which also caught bare dict / Dict[Any, ...] fields (keys_schema type 'any'). Those map to an unconstrained JSON object, which is valid (JSON object keys are always strings) and is accepted by the backend, so rejecting them with 'JSON does not support non-string keys' is a false positive with a misleading message. Allow the 'any' key type alongside 'str'; int/float/etc. keys stay rejected. Also read keys_schema/type defensively via .get(). Adds tests for bare dict, Dict[str, Any], Dict[Any, Any], and a bare-dict model field being accepted, and keeps the existing int-key rejection test. Closes guidance-ai#1493
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.
Closes #1493.
What
GenerateJsonSchemaSafe.generate_inner(inguidance/library/_pydantic.py) rejected any dict whose key type wasn't exactly"str". That correctly catches genuinely non-string keys likedict[int, int], but it also caught a baredict/Dict[Any, ...]field, whosekeys_schematype is"any". Those map to an unconstrained JSON object, which is valid (JSON object keys are always strings) and is accepted by the backend, so rejecting them withTypeError: JSON does not support non-string keys, got type anyis a false positive with a misleading message.Reproduction (before)
The schema a bare dict produces is supported end to end; only the pre-check blocked it:
Fix
Allow the
"any"key type alongside"str", and readkeys_schema/typedefensively:int/float/etc. keys stay rejected.Tests
TestDict.test_any_keys_allowed: baredict,Dict[str, Any],Dict[Any, Any]all accepted.TestDict.test_any_keys_allowed_as_model_field: a model with a bare-dict field is accepted.TestDict.test_prevent_non_string_keys(int keys still rejected) is unchanged and passes.RED/GREEN verified: the two new tests fail on
main(theTypeErrorfires) and pass with the fix;TestDictis green (4 passed).Not included
dict[Literal["x", "y"], int](string-literal keys) stays rejected. Those keys are strings, but that schema usespropertyNames, which the backend reports asUnimplemented keys: ["propertyNames"], so it's genuinely unsupported today. I left it rejected rather than push an unsupported schema downstream; noted in #1493 for visibility.