OpenAIClient
OpenAI-compatible chat client implementing ChatClient and ToolCapable.
Supports chat completions, streaming, tool execution, and model listing. Works with any OpenAI-compatible API (OpenAI, Ollama, Groq, etc.).
Parameters
The API key for authentication.
The base URL of the API (default: "https://api.openai.com/v1").
The HTTP client to use. Defaults to a platform-appropriate client: on JVM, the available engine is loaded via ServiceLoader; on Native, the default system engine is used. Bearer authentication is automatically applied.
Samples
// Create OpenAI client.
val client = try {
OpenAIClient(apiKey = ApiKey.fromEnv()) // Set AI_API_KEY in your environment.
} catch (kNfEx: KeyNotFoundException) {
error("No api key provided.\nDetail message: ${kNfEx.message}")
} catch (e: Exception) {
error("Error: ${e.message}")
}
// With custom base url.
val clientWithCustomUrl = try {
OpenAIClient(apiKey = ApiKey.fromEnv())
} catch (e: Exception) {
error("Error: ${e.message}")
}
val baseUrl = "https://openai/compatible.domain/v1"
val apiKey = ApiKey.fromEnv()
// And with kory http client.
val clientWithCustomHttpClient = OpenAIClient(
apiKey = apiKey,
baseUrl = baseUrl,
httpClient = KoryHttpClient.create(
KoryHttpClientConfig(
baseUrl = baseUrl, // https://openai/compatible.domain/v1
auth = KoryAuth.Bearer(apiKey.value) // Authentication
)
)
)// Get OpenAI API Key from Env.
val openAIApiKey = ApiKey.fromEnv("OPEN_AI_API_KEY") { e ->
error(e)
}
// Create OpenAI client.
val client = OpenAIClient(
apiKey = openAIApiKey
)
runBlocking {
val response =
client.chat { // Call this function from coroutine or another suspend function(function automatic set Dispatchers.IO pool)
chat(model = "gpt-5.6-sol") {
user("Ping!")
}
}
for (choice in response.choices) { // AI Responses can be [Choice(1, content), Choice(2, content)].
for (content in choice.contents) { // Get all response contents from choices(Content.Text, Content.Reasoning...)
when (content) {
is Content.Parts -> println("Parts: $content")
is Content.Reasoning -> println("Reasoning: $content.value")
is Content.Text -> println("Output: $content.text")
is Content.ToolCall -> println("Tool Call: ${content.name} on ${content.argumentsJson}")
}
}
}
}Constructors
Functions
Cancels an in-progress response from the OpenAI Responses API.
Sends a chat request to the OpenAI and returns a non-streaming response.
Sends a chat request to the OpenAI API and returns a non-streaming response.
Sends a raw OpenAI chat completion request.
Sends a raw OpenAI chat completion request using the request DSL.
Streams raw OpenAI chat completion chunks.
Streams raw OpenAI chat completion chunks using the request DSL.
Sends a chat request to the OpenAI API and returns a streaming response.
Sends a chat request and returns a streaming response using full DSL configuration.
Sends a chat request and returns a streaming response using DSL message builder.
Sends a chat request with tool support and returns a streaming response.
Sends a chat request with tool support and returns a streaming response using full DSL configuration.
Sends a chat request with tool support and returns a non-streaming response.
Sends a chat request with tool support using full DSL configuration.
Deletes a single file by its ID.
Deletes multiple files with controlled concurrency.
Deletes a file from the OpenAI Files API.
Downloads a file's content and writes it to a KoryFile.
Downloads a file from the OpenAI Files API and writes its content to a KoryFile.
Retrieves the raw content of a file as a byte array.
Retrieves the list of available models from the OpenAI API.
Retrieves a paginated list of files uploaded to the OpenAI Files API.
Lists all available OpenAI models.
Retrieves the raw content of a file from the OpenAI Files API.
Streams a file's content as a Flow of byte arrays.
Streams the content of a file from the OpenAI Files API as a Flow of byte arrays.
Uploads a single file to the OpenAI Files API.
Uploads a single file to the OpenAI Files API using the request DSL.
Uploads multiple files using the request DSL.
Uploads multiple files with controlled concurrency.
Uploads a single file to the OpenAI Files API.
Uploads a single file to the OpenAI Files API using the request DSL.
Uploads multiple files to the OpenAI Files API using the DSL.
Uploads multiple files to the OpenAI Files API with controlled concurrency.
Uploads multiple files and emits progress/completion events as a Flow using the DSL.
Uploads multiple files and emits progress/completion events as a Flow.