Backtestspydanticrun-20260930-183947#13675
TypeAdapter config not used for JSON Schema generation
Better
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches fix the reported `TypeAdapter.json_schema()` case the same way. They push `self._config` onto the generator's `_config_wrapper_stack` around `generate()`, which addresses the root cause: the adapter config is not part of the core schema, so the generator never saw it. Patch B stops there. The sibling public API `TypeAdapter.json_schemas()` still ignores each adapter's config and would keep emitting `format: binary` for a `ser_json_bytes='base64'` adapter. The issue title reads "TypeAdapter config not used for JSON Schema generation", so leaving that path broken is an incomplete fix. Patch A also handles `json_schemas()`. It lets `generate_definitions` accept an optional fourth element per input (a per-input config) and pushes that config during both generation passes, so each adapter gets its own config. The change is backward compatible: 3-tuples still work and `None` configs push nothing meaningful. The union tuple typing is a bit awkward, but it adds little risk. On tests, Patch A covers: - the exact issue scenario; - a no-config baseline; - the `json_schemas` path with mixed config and no-config adapters. All of these fail before the change. Patch B's single test (`list[bytes]`) is valid and shows nested propagation, but it covers only one path. On craft, Patch B has a helpful explanatory comment. Patch A's docstring update documents the new tuple element. Overall, Patch A resolves the bug more completely, with better tests and little added risk.
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
6m 51s
Wall-clock time in the container
Full test suite
No new failures
12,372 passed · 500 skipped · 12,872 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
58vs 11 merged
Changed lines, ours vs merged
The issue
View on GitHubfrom pydantic import BaseModel, TypeAdapter
class Model(BaseModel, ser_json_bytes='base64'):
b: bytes
Model.model_json_schema()['properties']['b']
#> {'format': 'base64url', 'title': 'B', 'type': 'string'}
ta = TypeAdapter(bytes, config={'ser_json_bytes': 'base64'})
ta.json_schema()
#> {'format': 'binary', 'type': 'string'}The two patches
Our patch+50 −8
pydantic/json_schema.pyindex e7abcad0..f59e7025 100644@@ -348,7 +348,11 @@ class GenerateJsonSchema: return mapping def generate_definitions(- self, inputs: Sequence[tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]]+ self,+ inputs: Sequence[+ tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema]+ | tuple[JsonSchemaKeyT, JsonSchemaMode, core_schema.CoreSchema, ConfigDict | None]+ ], ) -> tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], dict[DefsRef, JsonSchemaValue]]: """Generates JSON schema definitions from a list of core schemas, pairing the generated definitions with a mapping that links the input keys to the definition references.@@ -359,6 +363,8 @@ class GenerateJsonSchema: - The first element is a JSON schema key type. - The second element is the JSON mode: either 'validation' or 'serialization'. - The third element is a core schema.+ - The optional fourth element is a configuration dictionary, used when generating+ the JSON Schema of the core schema (e.g. the config of a type adapter). Returns: A tuple where:@@ -379,16 +385,22 @@ class GenerateJsonSchema: code='json-schema-already-used', ) - for _, mode, schema in inputs:+ for input_ in inputs:+ _, mode, schema = input_[:3]+ config = input_[3] if len(input_) == 4 else None self._mode = mode- self.generate_inner(schema)+ with self._config_wrapper_stack.push(config):+ self.generate_inner(schema) definitions_remapping = self._build_definitions_remapping() json_schemas_map: dict[tuple[JsonSchemaKeyT, JsonSchemaMode], DefsRef] = {}- for key, mode, schema in inputs:+ for input_ in inputs:+ key, mode, schema = input_[:3]+ config = input_[3] if len(input_) == 4 else None self._mode = mode- json_schema = self.generate_inner(schema)+ with self._config_wrapper_stack.push(config):+ json_schema = self.generate_inner(schema) json_schemas_map[(key, mode)] = definitions_remapping.remap_json_schema(json_schema) json_schema = {'$defs': self.definitions}pydantic/type_adapter.pyindex 447ac34b..890bb817 100644@@ -736,7 +736,8 @@ class TypeAdapter(Generic[T]): if isinstance(self.core_schema, _mock_val_ser.MockCoreSchema): self.core_schema.rebuild() assert not isinstance(self.core_schema, _mock_val_ser.MockCoreSchema), 'this is a bug! please report it'- return schema_generator_instance.generate(self.core_schema, mode=mode)+ with schema_generator_instance._config_wrapper_stack.push(self._config):+ return schema_generator_instance.generate(self.core_schema, mode=mode) @staticmethod def json_schemas(@@ -784,7 +785,10 @@ class TypeAdapter(Generic[T]): by_alias=by_alias, ref_template=ref_template, union_format=union_format ) - inputs_ = []+ inputs_: list[+ tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema]+ | tuple[JsonSchemaKeyT, JsonSchemaMode, CoreSchema, ConfigDict | None]+ ] = [] for key, mode, adapter in inputs: # This is the same pattern we follow for model json schemas - we attempt a core schema rebuild if we detect a mock if isinstance(adapter.core_schema, _mock_val_ser.MockCoreSchema):@@ -792,7 +796,10 @@ class TypeAdapter(Generic[T]): assert not isinstance(adapter.core_schema, _mock_val_ser.MockCoreSchema), ( 'this is a bug! please report it' )- inputs_.append((key, mode, adapter.core_schema))+ if adapter._config is not None:+ inputs_.append((key, mode, adapter.core_schema, adapter._config))+ else:+ inputs_.append((key, mode, adapter.core_schema)) json_schemas_map, definitions = schema_generator_instance.generate_definitions(inputs_) tests/test_type_adapter.pyindex ad746451..e1df642c 100644@@ -757,3 +757,26 @@ def test_validate_json_invalid_json(): 'ctx': {'error': 'trailing comma at line 3 column 3'}, } ]+++def test_json_schema_uses_config() -> None:+ ta = TypeAdapter(bytes, config={'ser_json_bytes': 'base64'})+ assert ta.json_schema() == {'type': 'string', 'format': 'base64url'}++ ta_override = TypeAdapter(bytes, config={'json_schema_mode_override': 'serialization', 'ser_json_bytes': 'base64'})+ assert ta_override.json_schema(mode='validation') == {'type': 'string', 'format': 'base64url'}++ assert TypeAdapter(bytes).json_schema() == {'type': 'string', 'format': 'binary'}+++def test_json_schemas_uses_config() -> None:+ ta_config = TypeAdapter(bytes, config={'ser_json_bytes': 'base64'})+ ta_no_config = TypeAdapter(bytes)++ json_schemas_map, _ = TypeAdapter.json_schemas(+ [('config', 'validation', ta_config), ('no_config', 'validation', ta_no_config)]+ )+ assert json_schemas_map == {+ ('config', 'validation'): {'type': 'string', 'format': 'base64url'},+ ('no_config', 'validation'): {'type': 'string', 'format': 'binary'},+ }
Merged fix · PR #13676+10 −1
pydantic/type_adapter.pyindex 447ac34bc..1c6824acb 100644@@ -736,7 +736,10 @@ class TypeAdapter(Generic[T]): if isinstance(self.core_schema, _mock_val_ser.MockCoreSchema): self.core_schema.rebuild() assert not isinstance(self.core_schema, _mock_val_ser.MockCoreSchema), 'this is a bug! please report it'- return schema_generator_instance.generate(self.core_schema, mode=mode)+ # The configuration provided to the type adapter (if any) is not part of the core schema,+ # so we need to explicitly make it available to the JSON Schema generator:+ with schema_generator_instance._config_wrapper_stack.push(self._config):+ return schema_generator_instance.generate(self.core_schema, mode=mode) @staticmethod def json_schemas(tests/test_type_adapter.pyindex ad7464513..1038a8291 100644@@ -555,6 +555,12 @@ def test_ta_config_with_annotated_type() -> None: ] +def test_ta_config_used_for_json_schema() -> None:+ """https://github.com/pydantic/pydantic/issues/13675"""+ ta = TypeAdapter(list[bytes], config=ConfigDict(ser_json_bytes='base64'))+ assert ta.json_schema() == {'type': 'array', 'items': {'type': 'string', 'format': 'base64url'}}++ def defer_build_test_models(config: ConfigDict) -> list[Any]: class Model(BaseModel): model_config = config