Skip to content

JSON Schema converter treats optional properties as required #833

Description

@yaodong-shen

Problem

json_schema_to_model() ignores the schema required array and passes Field(...) for every property. As a result, properties that JSON Schema allows callers to omit are rejected by the generated Pydantic model.

Reproduction

Using a schema with required_name in required and optional_note omitted from required:

Model = json_schema_to_model({
    "type": "object",
    "properties": {
        "required_name": {"type": "string"},
        "optional_note": {"type": "string"},
    },
    "required": ["required_name"],
})
Model(required_name="ok")

Current result:

ValidationError: optional_note - Field required

Both generated fields report is_required() == True.

Expected behavior

Only fields named in the JSON Schema required array should be mandatory. Optional fields should remain omittable, while explicit null should still be rejected unless the property schema permits null. The field-schema helper used for skill outputs can continue marking all of its supplied fields as required for backward compatibility.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions