Skip to main content

OrtApi Structure Reference

The OrtApi structure contains function pointers for all ONNX Runtime C API operations. Access it through the OrtApiBase.

Accessing the API

API Version

The current API version. Use this when calling GetApi() to ensure compatibility.

Status and Error Handling

CreateStatus

Create an error status object. Parameters:
  • code: Error code from OrtErrorCode enum
  • msg: Null-terminated error message (will be copied)
Returns: New OrtStatus object (must be freed with ReleaseStatus) Example:

GetErrorCode

Get the error code from a status object. Parameters:
  • status: Status object to query
Returns: OrtErrorCode value Error Codes:

GetErrorMessage

Get the error message from a status object. Parameters:
  • status: Status object to query
Returns: Null-terminated error message string (do not free; valid until status is released)

ReleaseStatus

Free a status object. Parameters:
  • status: Status object to free (can be NULL)

Environment Management

CreateEnv

Create an ONNX Runtime environment. The environment must be created before using other ONNX Runtime functionality. Parameters:
  • log_severity_level: Minimum severity level for log messages
  • logid: Identifier for logging
  • out: Newly created environment (must be freed with ReleaseEnv)
Returns: NULL on success, error status otherwise Logging Levels:
Example:

CreateEnvWithCustomLogger

Create an environment with a custom logging function. Parameters:
  • logging_function: Custom logging callback
  • logger_param: User data passed to logging callback
  • log_severity_level: Minimum severity level
  • logid: Log identifier
  • out: Newly created environment
Logging Function Signature:

CreateEnvWithGlobalThreadPools

Create an environment with global thread pools shared across sessions. Parameters:
  • log_severity_level: Minimum severity level
  • logid: Log identifier
  • tp_options: Threading options for global thread pools
  • out: Newly created environment
Note: Use with DisablePerSessionThreads to share thread pools across sessions.

ReleaseEnv

Free an environment object. Parameters:
  • env: Environment to free (can be NULL)

Session Options

CreateSessionOptions

Create session options for configuring session creation. Parameters:
  • options: Newly created session options (must be freed with ReleaseSessionOptions)
Returns: NULL on success

SetIntraOpNumThreads

Set the number of threads used to parallelize execution within operators. Parameters:
  • options: Session options to modify
  • intra_op_num_threads: Number of threads (0 = use default)
Note: When built with OpenMP, this setting has no effect.

SetInterOpNumThreads

Set the number of threads used to parallelize execution of the graph. Parameters:
  • options: Session options to modify
  • inter_op_num_threads: Number of threads (0 = use default)

SetSessionGraphOptimizationLevel

Set the optimization level to apply when loading a graph. Parameters:
  • options: Session options to modify
  • graph_optimization_level: Optimization level
Optimization Levels:

SetSessionExecutionMode

Set the execution mode (sequential or parallel). Parameters:
  • options: Session options to modify
  • execution_mode: Execution mode
Execution Modes:

SetOptimizedModelFilePath

Set the path to save the optimized model after graph transformations. Parameters:
  • options: Session options
  • optimized_model_filepath: Path where optimized model will be saved

EnableProfiling

Enable profiling for the session. Parameters:
  • options: Session options
  • profile_file_prefix: Prefix for the profile data file

DisableProfiling

Disable profiling for the session.

EnableMemPattern

Enable memory pattern optimization. If input shapes are consistent, ORT can trace and reuse memory allocation patterns. Note: Only available when sequential execution mode is enabled.

DisableMemPattern

Disable memory pattern optimization.

EnableCpuMemArena

Enable the memory arena on CPU. Arena may pre-allocate memory for future usage.

DisableCpuMemArena

Disable the memory arena on CPU.

AddSessionConfigEntry

Add a session configuration entry as a key-value pair. Parameters:
  • options: Session options
  • config_key: Configuration key (null-terminated)
  • config_value: Configuration value (null-terminated)
See onnxruntime_session_options_config_keys.h for valid keys and values.

ReleaseSessionOptions

Free session options.

Session Creation and Management

See Session Management page for detailed session-related functions.

Tensor and Value Operations

See Tensor Operations page for tensor creation and manipulation functions.

Execution Provider Configuration

See Execution Providers page for provider-specific configuration functions.

Memory Management

GetAllocatorWithDefaultOptions

Get the default CPU allocator. Always returns the same allocator instance. Parameters:
  • out: Pointer to the default allocator (do not free)
Returns: NULL on success Example:

CreateAllocator

Create an allocator for a session following a memory info specification. Parameters:
  • session: Session to create allocator for
  • mem_info: Memory info specifying allocation behavior
  • out: Newly created allocator (must be freed with ReleaseAllocator)

ReleaseAllocator

Free an allocator created with CreateAllocator.

Threading Options

CreateThreadingOptions

Create threading options for global thread pools. Parameters:
  • out: Newly created threading options (must be freed with ReleaseThreadingOptions)

SetGlobalIntraOpNumThreads

Set global intra-op thread count for use with CreateEnvWithGlobalThreadPools. Parameters:
  • tp_options: Threading options
  • intra_op_num_threads: Number of threads (0 = default, 1 = use calling thread)

SetGlobalInterOpNumThreads

Set global inter-op thread count.

SetGlobalSpinControl

Control whether thread pools spin when queues are empty. Parameters:
  • tp_options: Threading options
  • allow_spinning: 0 = don’t spin (recommended for high CPU usage), 1 = spin

ReleaseThreadingOptions

Free threading options.

Utility Functions

GetAvailableProviders

Get names of all available execution providers. Parameters:
  • out_ptr: Array of provider name strings (must be freed with ReleaseAvailableProviders)
  • provider_length: Number of providers in the array
Note: Providers may not be usable if system dependencies are missing.

ReleaseAvailableProviders

Free the array returned by GetAvailableProviders.

See Also