Pulling structured data out of a language model's reply.
Models rarely return bare JSON. They wrap it in a ``` fence, prefix it with
"Here is the JSON you asked for:", or trail a closing remark. Naively calling
jsonDecode on the response fails for all three, which is why this exists.
final data = PrismStructuredOutput.extractMap(reply);
print(data?['sentiment']);
This is deliberately dependency-free and forgiving. It never throws: a malformed response yields null, and callers decide whether to retry.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
extract(
String text) → Object? -
Extracts the first JSON object or array from
text. -
extractList(
String text) → List< Object?> ? - Extracts a JSON array, or null. A single object is wrapped into a list, mirroring extractMap's tolerance in the other direction.
-
extractMap(
String text) → Map< String, Object?> ? - Extracts a JSON object, or null when the response holds no object.
-
extractMapList(
String text) → List< Map< String, Object?> > - Extracts and maps a list of objects, skipping any entry that is not a map.
-
instructJson(
String prompt, {Map< String, Object?> ? schema, bool array = false}) → String - Rewrites a prompt to ask for JSON, and describes the shape you expect.