msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

json schema: propertyNames only shows up under specific conditions

Open bimac opened this issue 4 months ago • 0 comments

Description

I found a weird inconsistency when generating JSON schema:

Variant 1

import msgspec
from typing import Annotated
import json

key_type = Annotated[str, msgspec.Meta(pattern=r'^A$')]
value_type = Annotated[str, msgspec.Meta(pattern=r'^B$')]
dict_type = Annotated[dict[key_type, value_type], msgspec.Meta(min_length=1)]

schema = msgspec.json.schema(dict_type)
print(json.dumps(schema, indent=2))

results in:

{
  "type": "object",
  "propertyNames": {
    "pattern": "^A$"
  },
  "additionalProperties": {
    "type": "string",
    "pattern": "^B$"
  },
  "minProperties": 1
}

✔️ works as expected

Variant 2

import msgspec
from typing import Annotated
import json

key_type = Annotated[str, msgspec.Meta(title='key', pattern=r'^A$')]
value_type = Annotated[str, msgspec.Meta(pattern=r'^B$')]
dict_type = Annotated[dict[key_type, value_type], msgspec.Meta(min_length=1)]

schema = msgspec.json.schema(dict_type)
print(json.dumps(schema, indent=2))

results in:

{
  "type": "object",
  "additionalProperties": {
    "type": "string",
    "pattern": "^B$"
  },
  "minProperties": 1
}

❌ Merely adding the title kwarg to key_type results in the propertyNames section being dropped entirely.

bimac avatar Aug 21 '25 10:08 bimac