LangChain4jChatModel

class LangChain4jChatModel(model: ChatModel, streamingModel: StreamingChatModel? = null) : ChatModel

A ChatModel that calls any LangChain4j chat model, so an agent built with toolAgent or toolLoop works with every provider LangChain4j supports.

val openAi = OpenAiChatModel.builder().apiKey(key).modelName("gpt-5").build()
val agent = toolAgent(LangChain4jChatModel(openAi), tools = listOf(forecast))

LangChain4j has a second kind of model for answers that arrive piece by piece. Pass one as streamingModel and stream uses it, so a toolAgent whose run is collected with stream shows the text while the model writes it:

val streaming = OpenAiStreamingChatModel.builder().apiKey(key).modelName("gpt-5").build()
val agent = toolAgent(LangChain4jChatModel(openAi, streaming), tools = listOf(forecast))

LangChain4j calls block on network I/O, so each call runs on Dispatchers.IO. A model's thinking and other details that LangChain4j keeps in its AiMessage travel in ChatMessage.Assistant.providerContent.

Parameters

model

the LangChain4j chat model to call.

streamingModel

the LangChain4j model that stream calls. Without one, stream asks model and delivers the text of its answer in one piece.

Constructors

Link copied to clipboard
constructor(model: ChatModel, streamingModel: StreamingChatModel? = null)

Functions

Link copied to clipboard
open suspend override fun chat(request: ChatRequest): ChatResponse

Sends request to the LangChain4j model and returns its answer.

Link copied to clipboard
open override fun stream(request: ChatRequest): Flow<ChatEvent>

Sends request to the streaming model and returns its answer as it arrives: the text piece by piece, then the whole answer. Without a streaming model, the text arrives in one piece.