Repository navigation
Keyword explain
explain is the keyword for self-explainable decisioning.
It evaluates another compute document and returns both the answer and the evidence used to reach that answer. The caller gets the final value, a compact summary string for that value, and a thoughts trace listing the nested keywords that ran, where they ran, what input each one received, and what they returned.
summary is not a natural-language explanation. In trace mode, the explanation is the structured thoughts list. Thought input preserves the original compute-shaped JSON, including {returns}, so a UI or audit log can show the exact rule that produced each output. A natural-language explanation field is only attempted when mode is foundation_model and the local model is available.
Use explain when a decision should be transparent to a user, reviewer, support tool, audit log, or UI surface.
{
"{returns}": {
"explain": {
"value": {
"{returns}": {
"item": ["has_ruby_slippers"]
}
},
"mode": "foundation_model",
"context": {
"surface": "Emerald City gate",
"decision": "May Dorothy enter?"
}
}
}
}mode is optional. Supported values:
-
trace: returns the computed value and trace. -
foundation_model: returns the same structured trace and also attempts to produce a natural-languageexplanationon supported Apple platforms when the local model is available.
On success:
{
"ok": true,
"value": true,
"summary": "true",
"thoughts": []
}On failure:
{
"ok": false,
"value": null,
"error": "message",
"thoughts": []
}This document answers: may Dorothy enter the Emerald City?
{
"{returns}": {
"explain": {
"mode": "foundation_model",
"context": {
"surface": "Emerald City gate",
"decision": "May Dorothy enter?"
},
"value": {
"{returns}": {
"yes": {
"if": [
{ "{returns}": { "item": ["has_ruby_slippers"] } },
{
"{returns}": {
"not": {
"{returns}": {
"item": ["under_sleep_spell"]
}
}
}
}
]
}
}
}
}
}
}With item context:
{
"has_ruby_slippers": true,
"under_sleep_spell": false
}Actual output:
{
"explanation": "You are seeing this because you have ruby slippers and are not under a sleep spell.",
"ok": true,
"summary": "true",
"thoughts": [
{
"depth": 1,
"input": {
"{returns}": {
"item": ["has_ruby_slippers"]
}
},
"keyword": "item",
"kind": "compute",
"output": true,
"route": [
"{returns}",
"explain",
"value",
"{returns}",
"yes",
"if",
"0"
]
},
{
"depth": 1,
"input": {
"{returns}": {
"item": ["under_sleep_spell"]
}
},
"keyword": "item",
"kind": "compute",
"output": false,
"route": [
"{returns}",
"explain",
"value",
"{returns}",
"yes",
"if",
"1",
"{returns}",
"not"
]
},
{
"depth": 1,
"input": {
"{returns}": {
"not": {
"{returns}": {
"item": ["under_sleep_spell"]
}
}
}
},
"keyword": "not",
"kind": "compute",
"output": true,
"route": [
"{returns}",
"explain",
"value",
"{returns}",
"yes",
"if",
"1"
]
},
{
"depth": 1,
"input": {
"{returns}": {
"yes": {
"if": [
{ "{returns}": { "item": ["has_ruby_slippers"] } },
{
"{returns}": {
"not": {
"{returns}": { "item": ["under_sleep_spell"] }
}
}
}
]
}
}
},
"keyword": "yes",
"kind": "compute",
"output": true,
"route": [
"{returns}",
"explain",
"value"
]
}
],
"value": true
}The answer is true, and the trace shows why:
-
has_ruby_slippersresolved totrue. -
under_sleep_spellresolved tofalse. -
notturned that intotrue. -
yescombined the checks and returnedtrue.
This document explains a filtered list: which companions are ready to enter?
{
"{returns}": {
"explain": {
"mode": "foundation_model",
"context": {
"surface": "Emerald City companion list",
"decision": "Which companions are ready to enter?"
},
"value": {
"{returns}": {
"array_filter": {
"array": [
{
"name": "Dorothy",
"has_ticket": true,
"under_sleep_spell": false,
"steps_on_road": 21
},
{
"name": "Scarecrow",
"has_ticket": true,
"under_sleep_spell": false,
"steps_on_road": 9
},
{
"name": "Tin Woodman",
"has_ticket": false,
"under_sleep_spell": false,
"steps_on_road": 18
}
],
"predicate": {
"{returns}": {
"yes": {
"if": [
{ "{returns}": { "item": ["has_ticket"] } },
{
"{returns}": {
"comparison": {
"greater_or_equal": {
"lhs": { "{returns}": { "item": ["steps_on_road"] } },
"rhs": 10
}
}
}
}
],
"unless": [
{ "{returns}": { "item": ["under_sleep_spell"] } }
]
}
}
}
}
}
}
}
}
}Actual output excerpt:
The real response includes every nested thought. This excerpt keeps the fields that explain why each companion was included or excluded.
{
"explanation": "You are seeing this because Dorothy is the only companion who has a ticket, walked at least 10 steps on the road, and is not under a sleep spell.",
"ok": true,
"summary": "[{\"has_ticket\":true,\"name\":\"Dorothy\",\"steps_on_road\":21,\"under_sleep_spell\":false}]",
"value": [
{
"has_ticket": true,
"name": "Dorothy",
"steps_on_road": 21,
"under_sleep_spell": false
}
],
"thoughts": [
{
"depth": 1,
"input": { "{returns}": { "item": ["has_ticket"] } },
"keyword": "item",
"kind": "compute",
"output": true,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "0", "{returns}", "yes", "if", "0"]
},
{
"depth": 1,
"input": {
"{returns}": {
"comparison": {
"greater_or_equal": {
"lhs": { "{returns}": { "item": ["steps_on_road"] } },
"rhs": 10
}
}
}
},
"keyword": "comparison",
"kind": "compute",
"output": true,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "0", "{returns}", "yes", "if", "1"]
},
{
"depth": 1,
"input": {
"{returns}": {
"yes": {
"if": [
{ "{returns}": { "item": ["has_ticket"] } },
{
"{returns}": {
"comparison": {
"greater_or_equal": {
"lhs": { "{returns}": { "item": ["steps_on_road"] } },
"rhs": 10
}
}
}
}
],
"unless": [
{ "{returns}": { "item": ["under_sleep_spell"] } }
]
}
}
},
"keyword": "yes",
"kind": "compute",
"output": true,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "0"]
},
{
"depth": 1,
"input": {
"{returns}": {
"comparison": {
"greater_or_equal": {
"lhs": { "{returns}": { "item": ["steps_on_road"] } },
"rhs": 10
}
}
}
},
"keyword": "comparison",
"kind": "compute",
"output": false,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "1", "{returns}", "yes", "if", "1"]
},
{
"depth": 1,
"input": { "{returns}": { "yes": "..." } },
"keyword": "yes",
"kind": "compute",
"output": false,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "1"]
},
{
"depth": 1,
"input": { "{returns}": { "item": ["has_ticket"] } },
"keyword": "item",
"kind": "compute",
"output": false,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "2", "{returns}", "yes", "if", "0"]
},
{
"depth": 1,
"input": { "{returns}": { "yes": "..." } },
"keyword": "yes",
"kind": "compute",
"output": false,
"route": ["{returns}", "explain", "value", "{returns}", "array_filter", "predicate", "2"]
},
{
"depth": 1,
"input": {
"{returns}": {
"array_filter": {
"array": "...",
"predicate": "..."
}
}
},
"keyword": "array_filter",
"kind": "compute",
"output": [
{
"has_ticket": true,
"name": "Dorothy",
"steps_on_road": 21,
"under_sleep_spell": false
}
],
"route": ["{returns}", "explain", "value"]
}
]
}The trace makes the decision inspectable:
- Dorothy passed the ticket check and the road-progress check, so her predicate was
true. - Scarecrow had a ticket, but
steps_on_roadwas below the threshold, so his predicate wasfalse. - Tin Woodman had enough road progress but failed the ticket check, so his predicate was
false. -
array_filterreturned only Dorothy.
This is the practical value of explain: a user-facing product decision can carry its own evidence. The app can render that evidence directly, or use foundation_model mode to ask a supported client for an additional generated sentence.