guidance.json
- guidance.json(name: str | None = None, *, schema: None | str | bool | Mapping[str, Any] | type[BaseModel] | TypeAdapter[Any] = None, temperature: float | None = None, max_tokens: int | None = None, separators: tuple[str, str] | None = None, whitespace_flexible: bool = False, whitespace_pattern: str | None = None, lenient: bool = False)
Generate valid JSON according to the supplied JSON schema or pydantic model.
Not all parts of JSON schema are supported. Indeed some parts (such as bounds on numbers) cannot really be supported in the context of LLM generation.
Using a JSON schema:
>>> schema = ''{ "type": "object", "properties": { "a" : {"type": "integer"} } }' >>> schema_obj = json.loads(schema) >>> lm += json(name="generated_object", schema=schema_obj) >>> print(json.loads(lm["generated_object"])) { 'a' : 2 }
Using a
pydantic.BaseModel:>>> class Schema(BaseModel): ... b: bool >>> lm += json(name="generated_object", schema=Schema) >>> print(json.loads(lm["generated_object"])) { 'b' : False }
Using a
pydantic.TypeAdapter:>>> schema = TypeAdapter(list[int]) >>> lm += json(name="generated_object", schema=schema) >>> print(json.loads(lm["generated_object"])) [1, 2, 3]
- Parameters:
- namestr or None
If this is not None then the the results of the generation will be saved as a variable on the Model object (so you can access the result as
lm["var_name"]).- schemaUnion[None, Mapping[str, Any], Type[pydantic.BaseModel], pydantic.TypeAdapter]
- One of:
None, in which case any valid JSON will be generated
A string representing a JSON schema which will be parsed using
json.loads()A JSON schema object. This is a JSON schema string which has been passed to
json.loads()A subclass of
pydantic.BaseModelAn instance of
pydantic.TypeAdapter