Backtestspydanticrun-20261007-130130#13713
str_min_length and str_max_length are enforced by the validator but absent from the JSON schema
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fill in `minLength`/`maxLength` in `str_schema` from config when the core schema leaves them unset. Both keep the precedence rule from the issue: a field-level length replaces the config value. The difference is where each patch reads the config and which strings it applies it to. Patch B reads `self._config`, the ConfigWrapper stack, and applies it to every call that reaches `str_schema`. That includes strings pydantic-core never checks with the string validator: - the string branch of `Decimal`; - `format`/`to-string` serializer schemas; - serializer `return_schema`s and computed-field return schemas in serialization mode; - `json_schema_input_schema` of function validators; - `ImportString`. A model with `str_max_length=3` would therefore advertise `maxLength: 3` on decimal strings and serialized outputs. That is a new mismatch between schema and behaviour, the same class of bug the issue reports. B also does not model pydantic-core's config scoping. It cannot handle top-level `definitions` generated outside the model's config push, such as type aliases and recursive aliases, or typed dicts without their own config inheriting the parent's core config. Patch A tracks the actual core `config` blocks that pydantic-core uses: a stack pushed in `model_schema`, `typed_dict_schema` and `dataclass_schema`. It falls back to the TypeAdapter config at top level. It resolves the root config for `definitions` schemas. It explicitly suppresses the config for each non-validated string path listed above. It also changes `ImportString`'s JSON schema so it no longer goes through `str_schema`. This matches the issue's root-cause analysis: the config is applied by pydantic-core at validation time, so the schema should reflect the config in effect where validation happens. On tests, A covers every precedence row from the issue's table and checks each case against real validation. It also tests nested models, typed dicts with and without config, dataclasses, lists and dict keys, TypeAdapter config, unvalidated strings in both modes, and alias/recursive definitions. B has a single test with config-only and field-override cases; it would fail before the change, but it misses the false-positive cases above. A is larger, but the added scope is needed for correctness rather than being churn.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
16m 24s
Wall-clock time in the container
Full test suite
No new failures
12,409 passed · 501 skipped · 12,910 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
240vs 29 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
str_min_length and str_max_length are enforced by the validator but do not appear in the JSON schema, so a JSON Schema validator silently accepts input the model rejects.
class Model(BaseModel):
model_config = ConfigDict(str_min_length=3)
s: str
Model.model_json_schema()['properties']['s']
#> {'title': 'S', 'type': 'string'} <-- no minLength
Model(s='ab') #> ValidationError
jsonschema.validate({'s': 'ab'}, Model.model_json_schema()) #> accepted
The same holds for str_max_length. This is the same class of defect as #13704 — a constraint pydantic applies that a consumer of the schema cannot see — but it comes from a different place, so that fix will not cover it.
Why it happens
A field-level constraint lands on the core schema, where GenerateJsonSchema.str_schema finds it:
'schema': {'type': 'str', 'min_length': 3}
A config-level one does not. It stays in the model's config block and is applied by pydantic-core at validation time, leaving the string node bare:
{'type': 'model',
'schema': {'type': 'model-fields',
'fields': {'s': {'type': 'model-field', 'schema': {'type': 'str'}}}}, # <-- nothing here
'config': {'str_min_length': 3, 'title': 'Model'}}
str_schema only reads the core schema node, so the constraint is invisible to it.
Precedence
Worth stating because it decides what the schema should say when both are present: a field-level length replaces the config one rather than narrowing it. Measured on main (952c239):
| config | field | rejected lengths | schema today |
|---|---|---|---|
str_max_length=5 | — | 8, 12 | {} — nothing emitted |
| — | max_length=10 | 12 | maxLength: 10 ✅ |
str_max_length=5 | max_length=10 | 12 | maxLength: 10 ✅ |
str_max_length=10 | max_length=5 | 8, 12 | maxLength: 5 ✅ |
str_min_length=3 | — | 1 | {} — nothing emitted |
str_min_length=3 | min_length=1 | none | minLength: 1 ✅ |
So only the config-only rows are wrong, and the fix is to fall back to the config value when the field left the key unset — the rows that already work keep working untouched.
str_strip_whitespace, str_to_lower and str_to_upper are not affected: they transform rather than constrain, and the result is still a string, so the schema stays accurate.
Example Code
import jsonschema
from pydantic import BaseModel, ConfigDict, ValidationError
class Model(BaseModel):
model_config = ConfigDict(str_min_length=3, str_max_length=5)
s: str
schema = Model.model_json_schema()
print(schema['properties']['s'])
#> {'title': 'S', 'type': 'string'}
for value in ('ab', 'abcdef'):
try:
Model(s=value)
except ValidationError:
print(f'pydantic rejects {value!r}')
jsonschema.validate({'s': value}, schema)
print(f'jsonschema accepts {value!r} -- constraint lost')
Python, Pydantic & OS Version
pydantic version: 2.14.0b1
pydantic-core version: 2.48.0
pydantic-core build: profile=release pgo=false
python version: 3.13.15 [MSC v.1944 64 bit (AMD64)]
platform: Windows-11-10.0.26200-SP0
related packages: typing_extensions-4.16.0
commit: 952c239
I have a fix and a regression test ready and will open a PR without a closing reference, so it does not trip enforce-issue-assignment. Happy to have this assigned to me and the PR linked properly, or to leave it if you would rather take it in-house.
The two patches
pydantic/json_schema.pyindex 6fc639f1..c802d087 100644@@ -19,7 +19,8 @@ import os import re import warnings from collections import Counter, defaultdict-from collections.abc import Callable, Hashable, Iterable, Sequence+from collections.abc import Callable, Hashable, Iterable, Iterator, Sequence+from contextlib import ExitStack, contextmanager from copy import deepcopy from enum import Enum from re import Pattern@@ -285,6 +286,9 @@ class GenerateJsonSchema: self.definitions: dict[DefsRef, JsonSchemaValue] = {} self._config_wrapper_stack = _config.ConfigWrapperStack(_config.ConfigWrapper({}))+ # The core configs (the `'config'` key of model, typed dict and dataclass core schemas) that+ # pydantic-core uses to validate the schema currently being generated, innermost last:+ self._core_config_stack: list[core_schema.CoreConfig] = [] self._mode: JsonSchemaMode = 'validation' @@ -314,6 +318,22 @@ class GenerateJsonSchema: def _config(self) -> _config.ConfigWrapper: return self._config_wrapper_stack.tail + @contextmanager+ def _push_core_config(self, core_config: core_schema.CoreConfig | None) -> Iterator[None]:+ # pydantic-core replaces (rather than merges) the config when entering a schema holding one:+ self._core_config_stack.append(core_config or {})+ try:+ yield+ finally:+ self._core_config_stack.pop()++ def _get_core_config_value(self, key: Literal['str_min_length', 'str_max_length']) -> Any:+ if self._core_config_stack:+ return self._core_config_stack[-1].get(key)+ # Outside of any model/typed dict/dataclass, pydantic-core uses the validator's config+ # (e.g. the one provided to a type adapter):+ return self._config.config_dict.get(key)+ @property def mode(self) -> JsonSchemaMode: if self._config.json_schema_mode_override is not None:@@ -765,7 +785,9 @@ class GenerateJsonSchema: return pattern - json_schema = self.str_schema(core_schema.str_schema(pattern=get_decimal_pattern(schema)))+ # The string representation is not validated by a string validator, so the string config doesn't apply:+ with self._push_core_config(None):+ json_schema = self.str_schema(core_schema.str_schema(pattern=get_decimal_pattern(schema))) if self.mode == 'validation': multiple_of = schema.get('multiple_of') le = schema.get('le')@@ -800,6 +822,13 @@ class GenerateJsonSchema: """ json_schema = {'type': 'string'} self.update_with_validations(json_schema, schema, self.ValidationsMapping.string)+ # When unset on the schema, pydantic-core falls back to the `str_min_length`/`str_max_length` config values:+ for core_key, config_key, json_key in (+ ('min_length', 'str_min_length', 'minLength'),+ ('max_length', 'str_max_length', 'maxLength'),+ ):+ if core_key not in schema and (value := self._get_core_config_value(config_key)) is not None:+ json_schema[json_key] = value if isinstance(json_schema.get('pattern'), Pattern): # TODO: should we add regex flags to the pattern? json_schema['pattern'] = json_schema.get('pattern').pattern # type: ignore@@ -1179,7 +1208,9 @@ class GenerateJsonSchema: The generated JSON schema. """ if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):- return self.generate_inner(input_schema)+ # The input schema is never used for validation, so the config doesn't apply:+ with self._push_core_config(None):+ return self.generate_inner(input_schema) return self.generate_inner(schema['schema']) @@ -1204,7 +1235,9 @@ class GenerateJsonSchema: The generated JSON schema. """ if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):- return self.generate_inner(input_schema)+ # The input schema is never used for validation, so the config doesn't apply:+ with self._push_core_config(None):+ return self.generate_inner(input_schema) return self.handle_invalid_for_json_schema( schema, f'core_schema.PlainValidatorFunctionSchema ({schema["function"]})'@@ -1220,7 +1253,9 @@ class GenerateJsonSchema: The generated JSON schema. """ if self.mode == 'validation' and (input_schema := schema.get('json_schema_input_schema')):- return self.generate_inner(input_schema)+ # The input schema is never used for validation, so the config doesn't apply:+ with self._push_core_config(None):+ return self.generate_inner(input_schema) return self.generate_inner(schema['schema']) @@ -1533,7 +1568,7 @@ class GenerateJsonSchema: named_required_fields.extend(self._name_required_computed_fields(schema.get('computed_fields', []))) cls = schema.get('cls') config = _get_typed_dict_config(cls)- with self._config_wrapper_stack.push(config):+ with self._config_wrapper_stack.push(config), self._push_core_config(schema.get('config')): json_schema = self._named_required_fields_schema(named_required_fields) # There's some duplication between `extra_behavior` and@@ -1661,7 +1696,9 @@ class GenerateJsonSchema: Returns: The generated JSON schema. """- return self.generate_inner(schema['return_schema'])+ # Computed fields are never validated, so the config doesn't apply:+ with self._push_core_config(None):+ return self.generate_inner(schema['return_schema']) def model_schema(self, schema: core_schema.ModelSchema) -> JsonSchemaValue: """Generates a JSON schema that matches a schema that defines a model.@@ -1677,7 +1714,7 @@ class GenerateJsonSchema: cls = cast('type[BaseModel]', schema['cls']) config = cls.model_config - with self._config_wrapper_stack.push(config):+ with self._config_wrapper_stack.push(config), self._push_core_config(schema.get('config')): json_schema = self.generate_inner(schema['schema']) self._update_class_schema(json_schema, cls, config)@@ -1888,7 +1925,7 @@ class GenerateJsonSchema: cls = schema['cls'] config = cast('ConfigDict', getattr(cls, '__pydantic_config__', {})) - with self._config_wrapper_stack.push(config):+ with self._config_wrapper_stack.push(config), self._push_core_config(schema.get('config')): json_schema = self.generate_inner(schema['schema']).copy() self._update_class_schema(json_schema, cls, config)@@ -2204,13 +2241,24 @@ class GenerateJsonSchema: Returns: The generated JSON schema. """- for definition in schema['definitions']:- try:- self.generate_inner(definition)- except PydanticInvalidForJsonSchema as e: # noqa: PERF203- core_ref: CoreRef = CoreRef(definition['ref']) # type: ignore- self._core_defs_invalid_for_json_schema[self.get_defs_ref((core_ref, self.mode))] = e- continue+ with ExitStack() as stack:+ if not self._core_config_stack:+ # pydantic-core validates the definitions with the validator's config which, for models,+ # typed dicts and dataclasses, is the config of the root schema (possibly referenced):+ root_schema = schema['schema']+ if root_schema['type'] == 'definition-ref':+ root_schema = next(+ (d for d in schema['definitions'] if d.get('ref') == root_schema['schema_ref']), root_schema+ )+ if root_schema['type'] in ('model', 'typed-dict', 'dataclass'):+ stack.enter_context(self._push_core_config(root_schema.get('config'))) # type: ignore+ for definition in schema['definitions']:+ try:+ self.generate_inner(definition)+ except PydanticInvalidForJsonSchema as e: # noqa: PERF203+ core_ref: CoreRef = CoreRef(definition['ref']) # type: ignore+ self._core_defs_invalid_for_json_schema[self.get_defs_ref((core_ref, self.mode))] = e+ continue return self.generate_inner(schema['schema']) def definition_ref_schema(self, schema: core_schema.DefinitionReferenceSchema) -> JsonSchemaValue:@@ -2242,10 +2290,13 @@ class GenerateJsonSchema: # PlainSerializerFunctionSerSchema or WrapSerializerFunctionSerSchema return_schema = schema.get('return_schema') if return_schema is not None:- return self.generate_inner(return_schema)+ # The return schema is never used for validation, so the config doesn't apply:+ with self._push_core_config(None):+ return self.generate_inner(return_schema) case {'type': 'format' | 'to-string'}: # FormatSerSchema or ToStringSerSchema- return self.str_schema(core_schema.str_schema())+ with self._push_core_config(None):+ return self.str_schema(core_schema.str_schema()) case {'type': 'model'}: # ModelSerSchema return self.generate_inner(schema['schema'])pydantic/types.pyindex a12b1910..ec2d6fee 100644@@ -1024,7 +1024,7 @@ else: @classmethod def __get_pydantic_json_schema__(cls, cs: CoreSchema, handler: GetJsonSchemaHandler) -> JsonSchemaValue:- return handler(core_schema.str_schema())+ return {'type': 'string'} @staticmethod def _serialize(v: Any) -> str:tests/test_json_schema.pyindex 4c439df2..ab31c866 100644@@ -7418,3 +7418,154 @@ def test_nested_model_deduplication() -> None: assert 'Level1' in definitions assert 'Level1-Input' not in definitions assert 'Level1-Output' not in definitions+++@pytest.mark.parametrize(+ 'config,field_constraints,expected,rejected',+ [+ ({'str_max_length': 5}, {}, {'maxLength': 5}, ['a' * 8]),+ ({'str_min_length': 3}, {}, {'minLength': 3}, ['a']),+ ({'str_min_length': 3, 'str_max_length': 5}, {}, {'minLength': 3, 'maxLength': 5}, ['ab', 'abcdef']),+ ({'str_max_length': 5}, {'max_length': 10}, {'maxLength': 10}, ['a' * 12]),+ ({'str_max_length': 10}, {'max_length': 5}, {'maxLength': 5}, ['a' * 8]),+ ({'str_min_length': 3}, {'min_length': 1}, {'minLength': 1}, ['']),+ ({'str_min_length': 3, 'str_max_length': 10}, {'max_length': 5}, {'minLength': 3, 'maxLength': 5}, ['ab']),+ ],+)+def test_str_length_config_in_json_schema(config, field_constraints, expected, rejected) -> None:+ """https://github.com/pydantic/pydantic/issues/13713"""++ class Model(BaseModel):+ model_config = ConfigDict(**config)++ s: str = Field(**field_constraints)++ assert Model.model_json_schema()['properties']['s'] == {'title': 'S', 'type': 'string', **expected}+ for value in rejected:+ with pytest.raises(ValidationError):+ Model(s=value)+++def test_str_length_config_in_json_schema_nested_types() -> None:+ """https://github.com/pydantic/pydantic/issues/13713"""++ class Inner(BaseModel):+ s: str++ class TDNoConfig(TypedDict):+ s: str++ class TDWithConfig(TypedDict):+ __pydantic_config__ = ConfigDict(str_max_length=2) # type: ignore++ s: str++ @dataclass(config=ConfigDict(str_min_length=4))+ class DC:+ s: str++ class Model(BaseModel):+ model_config = ConfigDict(str_max_length=5)++ items: list[str]+ mapping: dict[str, int]+ inner: Inner+ td_no_config: TDNoConfig+ td_with_config: TDWithConfig+ dc: DC++ schema = Model.model_json_schema()+ assert schema['properties']['items']['items'] == {'type': 'string', 'maxLength': 5}+ assert schema['properties']['mapping']['propertyNames'] == {'maxLength': 5}+ # Nested models use their own config:+ assert schema['$defs']['Inner']['properties']['s'] == {'title': 'S', 'type': 'string'}+ # Typed dicts without config use the parent's config:+ assert schema['$defs']['TDNoConfig']['properties']['s'] == {'title': 'S', 'type': 'string', 'maxLength': 5}+ assert schema['$defs']['TDWithConfig']['properties']['s'] == {'title': 'S', 'type': 'string', 'maxLength': 2}+ assert schema['$defs']['DC']['properties']['s'] == {'title': 'S', 'type': 'string', 'minLength': 4}++ valid = {+ 'items': [],+ 'mapping': {},+ 'inner': {'s': 'a' * 8},+ 'td_no_config': {'s': 'a'},+ 'td_with_config': {'s': 'a'},+ 'dc': {'s': 'aaaa'},+ }+ Model.model_validate(valid)+ for path, value in [+ ('items', ['a' * 6]),+ ('mapping', {'a' * 6: 1}),+ ('td_no_config', {'s': 'a' * 6}),+ ('td_with_config', {'s': 'aaa'}),+ ('dc', {'s': 'aaa'}),+ ]:+ with pytest.raises(ValidationError):+ Model.model_validate({**valid, path: value})+++def test_str_length_config_in_json_schema_type_adapter() -> None:+ """https://github.com/pydantic/pydantic/issues/13713"""+ ta = TypeAdapter(list[str], config=ConfigDict(str_min_length=1, str_max_length=3))++ assert ta.json_schema() == {'type': 'array', 'items': {'type': 'string', 'minLength': 1, 'maxLength': 3}}+ for value in ([''], ['abcd']):+ with pytest.raises(ValidationError):+ ta.validate_python(value)+++def test_str_length_config_not_in_json_schema_of_unvalidated_strings() -> None:+ """https://github.com/pydantic/pydantic/issues/13713"""++ class Model(BaseModel):+ model_config = ConfigDict(str_max_length=3)++ dec: Decimal+ imp: ImportString+ inp: Annotated[int, PlainValidator(lambda v: int(v), json_schema_input_type=str)]+ ser: Annotated[int, PlainSerializer(str, return_type=str)]++ @computed_field+ def comp(self) -> str:+ return 'abcdef'++ validation_props = Model.model_json_schema()['properties']+ assert 'maxLength' not in validation_props['dec']['anyOf'][1]+ assert 'maxLength' not in validation_props['imp']+ assert 'maxLength' not in validation_props['inp']+ serialization_props = Model.model_json_schema(mode='serialization')['properties']+ assert 'maxLength' not in serialization_props['dec']+ assert 'maxLength' not in serialization_props['ser']+ assert 'maxLength' not in serialization_props['comp']++ m = Model(dec='1.00000', imp='math.cos', inp='123456', ser=123456)+ assert m.model_dump(mode='json') == {+ 'dec': '1.00000',+ 'imp': 'math.cos',+ 'inp': 123456,+ 'ser': '123456',+ 'comp': 'abcdef',+ }+++def test_str_length_config_in_json_schema_top_level_definitions() -> None:+ """https://github.com/pydantic/pydantic/issues/13713"""+ Alias = TypeAliasType('Alias', str)+ Rec = TypeAliasType('Rec', 'list[Rec] | str')++ class Model(BaseModel):+ model_config = ConfigDict(str_min_length=2, str_max_length=3)++ a: Alias+ b: Alias+ rec: Rec+ children: 'list[Model]' = []++ defs = Model.model_json_schema()['$defs']+ assert defs['Alias'] == {'type': 'string', 'minLength': 2, 'maxLength': 3}+ assert defs['Rec']['anyOf'][1] == {'type': 'string', 'minLength': 2, 'maxLength': 3}++ Model(a='ab', b='ab', rec=['ab'])+ for field, value in [('a', 'a'), ('b', 'abcd'), ('rec', ['a']), ('rec', 'abcd')]:+ with pytest.raises(ValidationError):+ Model.model_validate({'a': 'ab', 'b': 'ab', 'rec': 'ab', field: value})
pydantic/json_schema.pyindex 6fc639f1e..7578fc1e5 100644@@ -798,8 +798,12 @@ class GenerateJsonSchema: Returns: The generated JSON schema. """- json_schema = {'type': 'string'}+ json_schema: JsonSchemaValue = {'type': 'string'} self.update_with_validations(json_schema, schema, self.ValidationsMapping.string)+ if 'minLength' not in json_schema and self._config.str_min_length:+ json_schema['minLength'] = self._config.str_min_length+ if 'maxLength' not in json_schema and self._config.str_max_length is not None:+ json_schema['maxLength'] = self._config.str_max_length if isinstance(json_schema.get('pattern'), Pattern): # TODO: should we add regex flags to the pattern? json_schema['pattern'] = json_schema.get('pattern').pattern # type: ignoretests/test_json_schema.pyindex 4c439df2f..e718d3389 100644@@ -2048,6 +2048,29 @@ def test_typeddict_default_bytes(ser_json_bytes: Literal['base64', 'utf8'], prop } +def test_str_length_config() -> None:+ class Model(BaseModel):+ model_config = ConfigDict(str_min_length=3, str_max_length=5)++ from_config: str+ from_field: Annotated[str, Field(min_length=1, max_length=10)]++ properties = Model.model_json_schema()['properties']+ assert properties['from_config'] == {+ 'title': 'From Config',+ 'type': 'string',+ 'minLength': 3,+ 'maxLength': 5,+ }+ # A field-level length replaces the config one rather than narrowing it:+ assert properties['from_field'] == {+ 'title': 'From Field',+ 'type': 'string',+ 'minLength': 1,+ 'maxLength': 10,+ }++ def test_model_subclass_metadata(): class A(BaseModel): """A Model docstring"""