diff --git a/guidance/library/_json.py b/guidance/library/_json.py index a7d3354b4..205598de8 100644 --- a/guidance/library/_json.py +++ b/guidance/library/_json.py @@ -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 ( @@ -876,6 +876,7 @@ def json( *, schema: Union[ None, + str, JSONSchema, Type["pydantic.BaseModel"], "pydantic.TypeAdapter", @@ -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)