OpenAIFunctionTool

abstract class OpenAIFunctionTool<ARGS, R>(val name: String, val description: String? = null, val strict: Boolean? = null, val allowedCallers: List<OpenAIAllowedCallers?> = emptyList(), val async: Boolean? = null, val deferLoading: Boolean? = null, val outputSchema: JsonObject? = null) : OpenAIResponsesTool<OpenAIResponsesToolDto.FunctionTool> , BaseTool<ARGS, R>

OpenAI function tool for the Responses API.

Type Parameters

ARGS

The type of the tool's input arguments.

R

The return type of the tool's execution.

See also

Samples

@Serializable
data class CreateTaskToolArgs(
    @ToolParam(description = "Title of the task to be saved") val taskName: String,
    @ToolParam(description = "Description of the task to be saved") val taskDescription: String,
)

class CreateTaskTool() : OpenAIFunctionTool<CreateTaskToolArgs, String>(
    name = "create_task",
    description = "Create a new task",
) {
    // Get serializers for args and result.
    override val argsSerializer: KSerializer<CreateTaskToolArgs> = serializer()
    override val resultSerializer: KSerializer<String> = serializer()

    override suspend fun execute(args: CreateTaskToolArgs): String {
        // Save task...
        return "Task with name ${args.taskName} successfully created!"
    }
}

// Use CreateTaskTool...

Constructors

Link copied to clipboard
constructor(name: String, description: String? = null, strict: Boolean? = null, allowedCallers: List<OpenAIAllowedCallers?> = emptyList(), async: Boolean? = null, deferLoading: Boolean? = null, outputSchema: JsonObject? = null)

Properties

Link copied to clipboard

List of allowed callers, or empty for all.

Link copied to clipboard
abstract val argsSerializer: KSerializer<ARGS>
Link copied to clipboard

Whether the tool executes asynchronously.

Link copied to clipboard

Whether to defer tool loading.

Link copied to clipboard
open override val description: String?
Link copied to clipboard
open override val name: String
Link copied to clipboard
val outputSchema: JsonObject?

Optional JSON schema for the tool's output.

Link copied to clipboard
abstract val resultSerializer: KSerializer<R>
Link copied to clipboard

Whether to enforce strict schema adherence.

Functions

Link copied to clipboard
abstract suspend fun execute(args: ARGS): R
Link copied to clipboard
open suspend fun executeRaw(argsJson: String, json: Json): String
Link copied to clipboard