Skip to content

schema_type_to_python drops anyOf/oneOf constraints to Any #1950

Description

@Solaris-star

What

schema_type_to_python handles type, enum, const, and JSON Schema type arrays, but not anyOf or oneOf. A property expressed with either union keyword therefore falls through to Any.

That loses constraints when json_schema_dict_to_pydantic feeds the converted type back into structured generation.

Reproduction

from outlines.types.json_schema_utils import schema_type_to_python

print(schema_type_to_python({
    "anyOf": [{"type": "integer"}, {"type": "string"}],
}, "pydantic"))
# typing.Any (expected Union[int, str])

print(schema_type_to_python({
    "oneOf": [{"type": "boolean"}, {"type": "null"}],
}, "pydantic"))
# typing.Any (expected Optional[bool])

The resulting Pydantic schema has no value constraint at all:

M = json_schema_dict_to_pydantic({
    "type": "object",
    "properties": {
        "value": {"anyOf": [{"type": "integer"}, {"type": "string"}]},
    },
    "required": ["value"],
})
print(M.model_json_schema()["properties"]["value"])
# {"title": "Value"}

Expected

Map each branch recursively and combine them with Union, just as the existing JSON Schema type-array path does. For Python typing/Pydantic, oneOf cannot preserve exclusivity for overlapping branches, but retaining the union of allowed value types is still strictly better than widening to Any.

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