JSON response templates
Build deterministic JSON objects from request data and resolved AnswerSet answers without another provider call.
JSON response templates let LakeShark assemble a predictable JSON object from information already present in a request and the AnswerSet’s resolved response. This formatting does not call AI. It runs after Knowledge reuse, provider fallback, or an enabled deterministic resolution path.
Use this when an application needs consistent JSON keys while the answer values change per question.
Use $ref directly in strict JSON outputs
You can put one expression-backed JSON object directly in Only approved values. This is a strict output structure: LakeShark resolves the answer normally, then builds the declared JSON fields without another provider call.
{
"strict_answers": [
{
"color": {"$ref": "answer.text"}
}
],
"json_template": null
}
For the question What color is grass?, when the resolved answer is green, the API’s answer_value is:
{"color":"green"}
Important: A dynamic strict structure restricts the JSON fields and structure, not the set of permitted color values. Static strict values still require an exact match. For this initial release, use one expression-backed JSON object per strict list. Mixing it with other alternatives is rejected because selecting between multiple dynamic structures would be ambiguous. References must use approved namespaces and pass the same size, depth, and governance checks as ordinary templates.
Use Output rules → JSON → Only approved values and enter the JSON object above without the enclosing strict_answers array. Save and choose Try it out to test it.
Create a template
- Open Answers, select an existing AnswerSet, and choose Output rules.
- Select Edit output rules, then set Response format to JSON.
- Select Any valid response. Enable Use a JSON response template.
- Enter a complete JSON object like the example below. Invalid JSON and unsupported references are rejected before saving.
- Save the new AnswerSet revision, then open Test to try an example question and caller context.
{
"user": {"$ref": "context.username"},
"question": {"$ref": "question.text"},
"color": {"$ref": "answer.text"},
"source": {"$ref": "answer.source"}
}
Each {"$ref":"..."} is a whole-value reference. Unlike string substitution, it preserves the referenced JSON type, including objects, arrays, numbers, and null when present.
Send test request data
The username comes from the application’s explicit context object. LakeShark does not guess or extract it from the question in this first version.
{
"question": "What color should I use?",
"context": {
"username": "operator"
}
}
Assuming the approved Knowledge answer is blue, answer_value is assembled as:
{
"user": "operator",
"question": "What color should I use?",
"color": "blue",
"source": "known"
}
The top-level API response still includes run_id, answer_type, answer, answer_value, source, and other execution evidence. The answer_value field contains the structured object; the legacy answer field contains its serialized JSON text.
Supported references
| Reference | Meaning |
|---|---|
context.username |
A named field supplied by the calling application. Nested objects such as context.customer.id also work. |
question.text |
The question after applicable input governance. |
answer.text |
The text resolved by the AnswerSet. |
answer.value |
The resolved underlying value. Typically text for Knowledge/provider answers; deterministic resolution may provide an object. |
answer.source |
How the answer was obtained, for example known, provider, or execution. |
Only those namespaces are available. No functions, conditionals, arithmetic, lookups, regular expressions, or automatic question.find.* extraction are supported yet. The template does not execute JavaScript or Python.
Plain strings with embedded references
Within an ordinary JSON string, you can interpolate a single scalar:
{
"summary": "The selected color is ${answer.text}.",
"requester": "${context.username}"
}
String interpolation produces a string. For typed objects, arrays, or numeric results, use the whole-value $ref form instead. The renderer serializes JSON safely, so quotes and newlines in request values cannot break the response structure.
Validation and security
- The response must be a JSON object. References must use supported paths. Field names beginning with
_and reserved keys are disallowed. - Missing context fields fail the request; they are not guessed or filled with invented values.
- Template definitions are limited to 16 KiB of normalized JSON, 256 nodes, and 16 levels of nesting. Rendered JSON is limited to 64 KiB.
- The dedicated
json_templatepolicy works with Any valid response. Expression-backed objects are also supported directly in the strict JSON allowed-value list. Do not set bothjson_templateandstrict_answerson the same AnswerSet. - Caller
contextis untrusted input. Supplyingcontext.usernamedoes not authenticate the username or establish an authorization decision. Use your application’s verified identity mechanism when applicable. - Output GovernanceSets are still applied to rendered values. JSON templates do not relax tenant boundaries, Question Key authorization, or Knowledge approval requirements.
- Each change creates a new AnswerSet revision. Templates are included in export/import and revision snapshots.
Troubleshooting
Missing request field means the template references a field your request did not supply. Update the request’s context or remove the reference.
Unknown reference means the path is outside the supported namespaces, for example question.find.username or system.timestamp.
Template unavailable in the editor means the backend has not yet advertised support for json_template in the AnswerSet settings response. This feature should appear after the compatible backend and database schema are deployed.
See Output rules for fixed-value restrictions and Answers API for request and response details.