Skip to main content
The ONNX Runtime GenAI C API provides a complete interface for integrating text generation capabilities into C applications. This API is not thread-safe and is designed for single-threaded usage.

Key Features

  • Model loading and configuration
  • Text generation with customizable parameters
  • Tokenization and decoding
  • Multi-modal processing (images and audio)
  • Adapter support for model customization
  • Engine-based request scheduling

Memory Management

The C API follows a consistent pattern for memory management:

Object Lifecycle

  1. Creation: Objects are created through OgaCreate*() functions that return a pointer to the object
  2. Usage: Objects are passed to other API functions
  3. Destruction: Objects must be explicitly destroyed using corresponding OgaDestroy*() functions

Example Pattern

Error Handling

All API functions that can fail return an OgaResult* pointer:
  • Success: Returns NULL
  • Failure: Returns a pointer to an OgaResult object containing error information

Error Handling Pattern

function
Returns the error message from an OgaResult. The returned string is owned by the OgaResult and will be freed when the result is destroyed.
function
Destroys an OgaResult object and frees its resources.

Example

Basic Usage Pattern

Here’s a complete example of using the C API for text generation:

Library Initialization and Shutdown

function
Call this on process exit to cleanly shutdown the GenAI library and its ONNX Runtime usage. This should be the last GenAI function called in your application.

Logging Configuration

function
Set a boolean logging option. See the logging.h ‘struct LogItems’ for available options.Parameters:
  • name: The name of the logging option
  • value: The boolean value to set
function
Set a string logging option. When called with name “filename”, the library will log to that file. Pass an empty string to revert to the default destination (std::cerr).Parameters:
  • name: The name of the logging option
  • value: The string value to set
function
Register a callback function to receive log messages. The callback overrides any previously set logging destination. Pass nullptr to disable callback and revert to the default destination.Parameters:
  • callback: Function pointer to the logging callback (or nullptr to disable)

String Management

function
Destroys a string returned by the API. All strings returned by GenAI functions must be freed using this function.

GPU Device Management

function
Sets the current GPU device ID for operations.Parameters:
  • device_id: The GPU device ID to use
function
Gets the current GPU device ID.Parameters:
  • device_id: Pointer to store the current GPU device ID

Execution Provider Registration

function
Registers an execution provider library with ONNX Runtime.Parameters:
  • registration_name: Name for the registration
  • library_path: Path to the provider library
function
Unregisters a previously registered execution provider library.Parameters:
  • registration_name: Name of the registration to unregister

Next Steps

Model Functions

Learn about model creation, configuration, and management

Generator Functions

Explore text generation and inference capabilities