json_schema_utils
Utilities for handling JSON schema compatibility.
preprocess_schema_for_union_types(schema, ensure_ascii=True)
Preprocess a JSON schema to handle union types (array type specifications).
This is a temporary workaround for the limitation in outlines-core 0.1.26 which doesn't support JSON schema type arrays like ["string", "null"]. This function converts such arrays into the equivalent anyOf format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema
|
Union[str, dict]
|
The JSON schema as a string or dictionary |
required |
ensure_ascii
|
bool
|
Whether to ensure the output JSON is ASCII-only |
True
|
Returns:
Type | Description |
---|---|
str
|
The preprocessed JSON schema string |
Examples:
>>> schema = {"type": ["string", "null"]}
>>> preprocess_schema_for_union_types(schema)
'{"anyOf":[{"type":"string"},{"type":"null"}]}'