Output modes and templates
Set the output once on the conversation, or for one message:
{ "agent": "atlas", "output": { "mode": "json", "json_schema": { "type": "object", "properties": { "summary": { "type": "string" } }, "required": ["summary"] }, "template": { "source": "<p>{{output.summary}}</p>", "format": "html" } } }
POST /conversations/{id}/messages accepts the same output object to override it for that message only.
mode | You get |
|---|---|
views | Text deltas plus view events |
markdown | Text only. Views are turned into their Markdown fallback and streamed as text. Good for chat apps, email, Slack. |
json | Text while the agent works, then an outputjson_schema |
An agent's supported modes are in its output_modes; asking for another one is refused.
JSON
In json mode the agent answers as usual (it can still call tools and ask for approvals), then the server turns its
answer into an object that must validate against your schema.
- The answer is converted to JSON and validated.
- If it fails, the model gets the validation errors and tries again.
- After 3 attempts the server stops and sends what it has.
The output event, and the message's output, tell you how it went:
{ "type": "output", "conversation_id": "agconv_1", "message_id": "agmsg_5", "format": "json", "data": { "summary": "3 courses are below 30% completion", "courses": [{ "uuid": "course_x", "rate": 22 }] }, "valid": true, "attempts": 1, "errors": [] }
When valid is false, errors lists what was still wrong after the last attempt, and an
error event with code output_invalid comes before message.done. Always
check valid before using data.
Supported schema keywords
The schema is a subset of JSON Schema:
| Keyword | Notes |
|---|---|
type | objectarraystringnumberintegerbooleannull |
propertiesrequiredadditionalProperties | additionalProperties |
items | One schema for every item |
enumconst | |
minLengthmaxLengthpattern | |
minimummaximum | |
minItemsmaxItems | |
anyOf | |
$defs$ref | "$ref": "#/$defs/Course" |
descriptiontitledefault | Descriptions are shown to the model: use them to explain fields |
A schema over 16 KB, nested deeper than 8 levels, or using another keyword is refused with 422 invalid_schema.
{ "type": "object", "additionalProperties": false, "required": ["summary", "courses"], "properties": { "summary": { "type": "string", "maxLength": 280, "description": "One sentence for a dashboard tile" }, "courses": { "type": "array", "maxItems": 10, "items": { "$ref": "#/$defs/Course" } } }, "$defs": { "Course": { "type": "object", "required": ["uuid", "rate"], "properties": { "uuid": { "type": "string", "pattern": "^course_" }, "rate": { "type": "number", "minimum": 0, "maximum": 100 }, "risk": { "enum": ["low", "medium", "high"] } } } } }
Templates
A template turns a reply into HTML, Markdown or plain text on the server, so you can drop it into an email, a widget or
a CMS without writing a renderer. The result comes as a rendered event and in the message's rendered.
{ "source": "<h2>{{conversation.title}}</h2>{{#output.courses}}<p>{{uuid}}: {{rate}}%</p>{{/output.courses}}", "format": "html" }
Syntax
Templates use a small part of Mustache. Nothing in a template can run code.
| Tag | Does |
|---|---|
{{name}} | The value, escaped for the format |
{{a.b.c}} | A nested value |
{{.}} | The current item inside a section |
{{#list}}…{{/list}} | Repeats for each item of a list. With an object, renders once with it as context. With any other truthy value, renders once. |
{{^x}}…{{/x}} | Renders when x |
{{! note }} | A comment, not rendered |
There are no partials, no lambdas, and no unescaped output: {{{x}}} and {{&x}} are refused with
422 invalid_template.
Escaping
Every value is escaped for the template's format:
format | Escaping |
|---|---|
html | HTML entities (<>& |
markdown | Markdown special characters are backslash-escaped, so a value can't add links, images or formatting |
text | None |
Only the template's own text is trusted. Text the model wrote (or a learner typed) can't inject tags. A text result is
not escaped at all: don't put it into HTML yourself.
Data
| Variable | |
|---|---|
text | The reply's text (Markdown) |
views | The views, as in |
citations | The citations |
output | The JSON output in jsonnull |
plan | The plan steps, or null |
suggestions | Follow-up prompts |
conversation | idtitlemetadataexternal_user_id |
message | idstatus |
Limits
The source can be up to 20 KB, the result up to 200 KB, and sections can nest 8 deep.
HTML example
<div class="lh-answer"> <p>{{text}}</p> {{#citations}} <a href="https://learn.your-school.com{{url}}">[{{index}}] {{title}}</a> {{/citations}} {{^citations}}<p>No sources.</p>{{/citations}} </div>
text is inserted as escaped text, not as rendered Markdown. If you want formatting in HTML, render text with your
own Markdown renderer instead, or use json mode with fields you lay out yourself.
Markdown example
## Weekly report for {{conversation.metadata.workspace}} {{output.summary}} {{#output.courses}} - {{uuid}}: {{rate}}% ({{risk}}) {{/output.courses}}
Values like a course name with * or [ in it stay literal: they are escaped.
Previewing
POST /templates/render renders a template with data you pass, without calling the agent and without using credits.
Use it to build and test templates:
curl -X POST $LH/templates/render \ -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \ -d '{ "template": { "source": "Hello {{name}}!", "format": "html" }, "data": { "name": "<Ana>" } }' # {"format": "html", "content": "Hello <Ana>!"}
Errors in the template answer 422 invalid_template with the reason in message.
The same template can be set on the conversation and changed per message through output.template.