Fail clearly on unsupported dynamic object schemas - #1835
Fail clearly on unsupported dynamic object schemas#1835sylvesterkaczmarek wants to merge 1 commit into
Conversation
|
Reviewed this against the repro pattern most likely to actually show up in real Pydantic schemas: Pydantic emits that as class M(BaseModel):
m: Dict[str, Any]
M.model_json_schema()["properties"]["m"]
# {'additionalProperties': True, 'title': 'M', 'type': 'object'}
transform_schema({'additionalProperties': True, 'title': 'M', 'type': 'object'})
# {'type': 'object', 'title': 'M', 'properties': {}, 'additionalProperties': False}That's the exact bug this PR closes — a dict field turned into a schema that can only ever hold additional_properties = json_schema.get("additionalProperties")
if additional_properties is True or isinstance(additional_properties, dict):
keywords.append("additionalProperties")(Verified locally on this branch: applied the diff, imported |
Summary
Prevent
transform_schema()from silently turning dynamic-key object schemas into objects that accept no keys.When an object has no explicit
propertiesand relies onpatternProperties,propertyNames,unevaluatedProperties, or an explicitadditionalPropertiesschema to admit keys, the current transformer can demote or discard that information and then forceproperties: {}plusadditionalProperties: false. The transformed schema can therefore only produce{}even though the source schema admits populated dictionaries.Fixes #1817.
Fix
Detect explicit dynamic-object forms before closing an otherwise property-less object and raise a targeted
ValueErrorinstead of returning a silently unsatisfiable schema.The guard is deliberately narrow:
patternPropertieswithout explicit properties is rejected;additionalPropertieswithout properties is rejected;propertyNamesis rejected only while the source object still admits additional keys;unevaluatedPropertieswithout explicit properties is rejected;Regression coverage
Adds focused tests for the dynamic-key forms, an ordinary Pydantic
dict[str, str]field, and controls for explicit-property and already-closed objects.The patch is confined to Anthropic's hand-maintained
src/anthropic/lib/_parse/transformer and regression tests.