Skip to content

Commit

Permalink
Allow json-loads-able strings to be passed as schema
Browse files Browse the repository at this point in the history
  • Loading branch information
hudson-ai committed Oct 22, 2024
1 parent 855ce5b commit 67f0b4b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions guidance/library/_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from json import dumps as json_dumps
from json import dumps as json_dumps, loads as json_loads
from enum import Enum
import math
from typing import (
Expand Down Expand Up @@ -876,6 +876,7 @@ def json(
*,
schema: Union[
None,
str,
JSONSchema,
Type["pydantic.BaseModel"],
"pydantic.TypeAdapter",
Expand Down Expand Up @@ -937,7 +938,9 @@ def json(
# Default schema is empty, "anything goes" schema
# TODO: consider default being `{"type": "object"}`
schema = {}
elif isinstance(schema, (Mapping, bool)):
elif isinstance(schema, (Mapping, bool, str)):
if isinstance(schema, str):
schema = cast(JSONSchema, json_loads(schema))
# Raises jsonschema.exceptions.SchemaError or ValueError
# if schema is not valid
jsonschema.validators.Draft202012Validator.check_schema(schema)
Expand Down

0 comments on commit 67f0b4b

Please sign in to comment.