Formatron v0.4.9
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.formats.json.JsonExtractor Class Reference

An extractor that loads json data to an object from a string. More...

Inheritance diagram for formatron.formats.json.JsonExtractor:
formatron.extractor.NonterminalExtractor formatron.extractor.Extractor

Public Member Functions

 __init__ (self, str nonterminal, typing.Optional[str] capture_name, schemas.schema.Schema|collections.abc.Sequence schema, typing.Callable[[str], schemas.schema.Schema] to_object)
 Create a json extractor from a given schema or a list of supported types.
 
typing.Optional[tuple[str, schemas.schema.Schema]] extract (self, str input_str)
 Extract a schema instance from a string.
 
- Public Member Functions inherited from formatron.extractor.NonterminalExtractor
str nonterminal (self)
 Get the nonterminal of the extractor.
 
str kbnf_reference (self)
 
- Public Member Functions inherited from formatron.extractor.Extractor
 __str__ (self)
 
typing.Optional[str] capture_name (self)
 Get the name of the capture, or None if the extractor does not capture.
 

Protected Attributes

 _to_object
 
 _rule_str
 
- Protected Attributes inherited from formatron.extractor.NonterminalExtractor
 _nonterminal
 
- Protected Attributes inherited from formatron.extractor.Extractor
 _capture_name
 

Properties

 kbnf_definition (self)
 

Detailed Description

An extractor that loads json data to an object from a string.

Definition at line 426 of file json.py.

Constructor & Destructor Documentation

◆ __init__()

formatron.formats.json.JsonExtractor.__init__ ( self,
str nonterminal,
typing.Optional[str] capture_name,
schemas.schema.Schema|collections.abc.Sequence schema,
typing.Callable[[str], schemas.schema.Schema] to_object )

Create a json extractor from a given schema or a list of supported types.

   Currently, the following data types are supported:
- bool
- int
  • positive int
  • negative int
  • nonnegative int
  • nonpositive int
    - float
  • positive float
  • negative float
  • nonnegative float
  • nonpositive float
    - str
  • optionally with min_length, max_length and pattern constraints
    • length is measured in UTF-8 character number after json parsing
    • Warning: too large difference between min_length and max_length can lead to enormous memory consumption!
    • pattern is mutually exclusive with min_length and max_length
    • pattern will be compiled to a regular expression so all caveats of regular expressions apply
    • pattern currently is automatically anchored at both ends
    • the generated json could be invalid if the pattern allows invalid content between the json string's quotes.
      • for example, pattern=".*" will allow '"' to appear in the json string which is forbidden by JSON standard.
  • also supports substring_of constraint which constrains the string to be a substring of a given string
    • the generated json could be invalid if the given string contains invalid content when put into the json string's quotes.
      • for example, substring_of="abc\"" will allow '"' to appear in the json string which is forbidden by JSON standard.
        - NoneType
        - typing.Any

Subclasses of collections.abc.Mapping[str,T] and typing.Mapping[str,T] where T is a supported type,

  • Subclasses of collections.abc.Sequence[T] and typing.Sequence[T] where T is a supported type.
    • optionally with minItems, maxItems, prefixItems constraints
    • Warning: too large difference between minItems and maxItems can lead to very slow performance!
    • Warning: By json schema definition, prefixItems by default allows additional items and missing items in the prefixItems, which may not be the desired behavior and can lead to very slow performance if prefixItems is long!
  • tuple[T1,T2,...] where T1,T2,... are supported types. The order, type and number of elements will be preserved.
  • typing.Literal[x1,x2,...] where x1, x2, ... are instances of int, string, bool or NoneType, or another typing.Literal[y1,y2,...]
  • typing.Union[T1,T2,...] where T1,T2,... are supported types.
  • schemas.Schema where all its fields' data types are supported. Recursive schema definitions are supported as well.
    • Warning: while not required field is supported, they can lead to very slow performance and/or enormous memory consumption if there are too many of them!
Parameters
nonterminalThe nonterminal representing the extractor.
capture_nameThe capture name of the extractor, or None if the extractor does not capture.
schemaThe schema.
to_objectA callable to convert the extracted string to a schema instance.

Reimplemented from formatron.extractor.NonterminalExtractor.

Definition at line 483 of file json.py.

Member Function Documentation

◆ extract()

typing.Optional[tuple[str, schemas.schema.Schema]] formatron.formats.json.JsonExtractor.extract ( self,
str input_str )

Extract a schema instance from a string.

Parameters
input_strThe input string to extract from.
Returns
A tuple of the remaining string and the extracted schema instance, or None if extraction failed.

Reimplemented from formatron.extractor.Extractor.

Definition at line 497 of file json.py.

◆ kbnf_definition()

formatron.formats.json.JsonExtractor.kbnf_definition ( self)

Reimplemented from formatron.extractor.Extractor.

Definition at line 551 of file json.py.

Member Data Documentation

◆ _rule_str

formatron.formats.json.JsonExtractor._rule_str
protected

Definition at line 487 of file json.py.

◆ _to_object

formatron.formats.json.JsonExtractor._to_object
protected

Definition at line 486 of file json.py.


The documentation for this class was generated from the following file: