aiChatAsync

Asynchronous version of aiChat() that returns a BoxLang Future for non-blocking AI requests.

Syntax

aiChatAsync(messages, params, options)

Parameters

Parameter
Type
Required
Description

messages

any

Yes

The messages to pass to the AI model (string, struct, array, or AiMessage)

params

struct

No

Request parameters for the AI provider

options

struct

No

Request options (provider, apiKey, returnFormat, timeout, logging)

Parameters are identical to aiChat().

Returns

Returns a BoxLang Future object that will eventually contain the AI response. Use .get() to retrieve the result (blocking) or .then() for callbacks.

Examples

Basic Async Request

// Start async request
future = aiChatAsync( "What is BoxLang?" );

// Do other work...
doSomethingElse();

// Get result (blocks until ready)
response = future.get();
println( response );

Multiple Concurrent Requests

With Callback

Error Handling

With Timeout

Parallel Processing Pipeline

Background Processing

Future Methods

.get(timeout)

Blocks until result available, returns the AI response.

.then(callback)

Executes callback when result ready, returns new Future.

.onError(callback)

Handles errors in the Future chain.

.isDone()

Check if computation complete without blocking.

.cancel()

Attempt to cancel the computation.

Use Cases

✅ Multiple Independent Requests

✅ Long-Running Operations

✅ Background Tasks

❌ Simple Single Request

Notes

  • Extends aiChat(): All aiChat() parameters and options supported

  • Non-blocking: Returns immediately, computation runs in background

  • Thread pool: Uses BoxLang's async executor

  • Error propagation: Errors in async code bubble up to .get() or .onError()

  • Same return formats: Supports all returnFormat options

  • Chaining: Can chain multiple .then() calls for pipeline processing

Performance Tips

Last updated