Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Chat & Structured Output

Chat with AI models using aiChat, stream responses, and get typed, validated structured output from classes and templates.

💬 aiChat

aiChat() is the simplest way to talk to a model. Pass a message, get an answer.

answer = aiChat( "What is the capital of France?" )
println( answer )

Signature:

aiChat( message, params = {}, options = {} )
Argument
Purpose

message

A string, or an array of messages for a conversation

params

Model parameters such as temperature and max_tokens

options

Module options such as provider, apiKey, and returnFormat

Tune the response:

poem = aiChat( "Write a haiku about coding", { temperature: 0.9 } )

📡 Streaming

Stream a response as it is generated, which is ideal for chat interfaces:

aiChatStream(
    "Tell me a story about a robot",
    ( chunk ) => {
        content = chunk.choices?.first()?.delta?.content ?: ""
        print( content )
    }
)

🧱 Structured Output

Stop parsing free text. Give BoxLang a class or a template and get a validated, typed result back.

From a Class

From a Struct Template

For quick prototypes:

Use classes for production code, and struct templates for prototypes.

🎯 Why It Matters

  • Type safety with real objects instead of generic structs

  • Automatic validation against the schema

  • Fewer hallucinated fields, because the shape is constrained

  • No manual parsing of AI text

🔍 Go Deeper

Last updated

Was this helpful?