SDK Configuration

This guide covers configuration for both Python and TypeScript SDKs, including basic setup, advanced options, and debugging capabilities.

Getting Started

Python SDK

The recommended approach to configuring the Python SDK is to use the opik configure command. This will prompt you to set up your API key and Opik instance URL (if applicable) to ensure proper routing and authentication. All details will be saved to a configuration file.

If you are using the Cloud version of the platform, you can configure the SDK by running:

1import opik
2
3opik.configure(use_local=False)

You can also configure the SDK by calling configure from the Command line:

$opik configure

The configure methods will prompt you for the necessary information and save it to a configuration file (~/.opik.config). When using the command line version, you can use the -y or --yes flag to automatically approve any confirmation prompts:

$opik configure --yes

TypeScript SDK

For the TypeScript SDK, configuration is done through environment variables, constructor options, or configuration files.

Installation:

$npm install opik

Basic Configuration:

You can configure the Opik client using environment variables in a .env file:

$OPIK_API_KEY="your-api-key"
>OPIK_URL_OVERRIDE="https://www.comet.com/opik/api"
>OPIK_PROJECT_NAME="your-project-name"
>OPIK_WORKSPACE="your-workspace-name"

Or pass configuration directly to the constructor:

1import { Opik } from "opik";
2
3const client = new Opik({
4 apiKey: "<your-api-key>",
5 apiUrl: "https://www.comet.com/opik/api",
6 projectName: "<your-project-name>",
7 workspaceName: "<your-workspace-name>",
8});

Configuration Methods

Both SDKs support multiple configuration approaches with different precedence orders.

Configuration Precedence

Python SDK: Constructor options → Environment variables → Configuration file → Defaults

TypeScript SDK: Constructor options → Environment variables → Configuration file (~/.opik.config) → Defaults

Environment Variables

Both SDKs support environment variables for configuration. Here’s a comparison of available options:

ConfigurationPython Env VariableTypeScript Env VariableDescription
API KeyOPIK_API_KEYOPIK_API_KEYAPI key for Opik Cloud
URL OverrideOPIK_URL_OVERRIDEOPIK_URL_OVERRIDEOpik server URL
Project NameOPIK_PROJECT_NAMEOPIK_PROJECT_NAMEProject name
WorkspaceOPIK_WORKSPACEOPIK_WORKSPACEWorkspace name
Config PathOPIK_CONFIG_PATHOPIK_CONFIG_PATHCustom config file location
Track DisableOPIK_TRACK_DISABLEN/ADisable tracking (Python only)
Flush TimeoutOPIK_DEFAULT_FLUSH_TIMEOUTN/ADefault flush timeout (Python only)
TLS CertificateOPIK_CHECK_TLS_CERTIFICATEN/ACheck TLS certificates (Python only)
Batch DelayN/AOPIK_BATCH_DELAY_MSBatching delay in ms (TypeScript only)
Hold Until FlushN/AOPIK_HOLD_UNTIL_FLUSHHold data until manual flush (TypeScript only)
Log LevelOPIK_FILE_LOGGING_LEVELOPIK_LOG_LEVELLogging level

Using .env Files

Both SDKs support .env files for managing environment variables. This is a good practice to avoid hardcoding secrets and to make your configuration more portable.

For Python projects, install python-dotenv:

$pip install python-dotenv

For TypeScript projects, dotenv is automatically loaded by the SDK.

Create a .env file in your project’s root directory:

1# Opik Configuration
2OPIK_API_KEY="YOUR_OPIK_API_KEY"
3OPIK_URL_OVERRIDE="https://www.comet.com/opik/api"
4OPIK_PROJECT_NAME="your-project-name"
5OPIK_WORKSPACE="your-workspace-name"
6
7# LLM Provider API Keys (if needed)
8OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
9
10# Debug Mode (see Debug Mode section below)
11OPIK_FILE_LOGGING_LEVEL="DEBUG" # Python
12OPIK_LOG_LEVEL="DEBUG" # TypeScript

Python usage with .env file:

1from dotenv import load_dotenv
2
3load_dotenv() # Load before importing opik
4
5import opik
6
7# Your Opik code here

TypeScript usage with .env file:

The TypeScript SDK automatically loads .env files, so no additional setup is required:

1import { Opik } from "opik";
2
3// Configuration is automatically loaded from .env
4const client = new Opik();

Using Configuration Files

Both SDKs support configuration files for persistent settings.

Python SDK Configuration File

The Python SDK uses the TOML format. The configure method creates this file automatically, but you can also create it manually:

1[opik]
2url_override = https://www.comet.com/opik/api
3api_key = <API Key>
4workspace = <Workspace name>
5project_name = <Project Name>

TypeScript SDK Configuration File

The TypeScript SDK also supports the same ~/.opik.config file format as the Python SDK. The configuration file uses INI format internally but shares the same structure:

1[opik]
2url_override = https://www.comet.com/opik/api
3api_key = <API Key>
4workspace = <Workspace name>
5project_name = <Project Name>

By default, both SDKs look for the configuration file in your home directory (~/.opik.config). You can specify a different location by setting the OPIK_CONFIG_PATH environment variable.

Debug Mode and Logging

Both SDKs provide debug capabilities for troubleshooting integration issues.

Python SDK Debug Mode

To enable debug mode for the Python SDK, set these environment variables before importing opik:

$export OPIK_FILE_LOGGING_LEVEL="DEBUG"
>export OPIK_LOGGING_FILE=".tmp/opik-debug-$(date +%Y%m%d).log"

Using with .env file:

1# Debug Mode
2OPIK_FILE_LOGGING_LEVEL="DEBUG"
3OPIK_LOGGING_FILE=".tmp/opik-debug.log"

Then in your Python script:

1from dotenv import load_dotenv
2
3load_dotenv() # Load before importing opik
4
5import opik
6
7# Your Opik code here

TypeScript SDK Debug Mode

The TypeScript SDK uses structured logging with configurable levels:

Available log levels: SILLY, TRACE, DEBUG, INFO (default), WARN, ERROR, FATAL

Enable debug logging:

$export OPIK_LOG_LEVEL="DEBUG"

Or in .env file:

1OPIK_LOG_LEVEL="DEBUG"

Programmatic control:

1import { setLoggerLevel, disableLogger } from "opik";
2
3// Set log level
4setLoggerLevel("DEBUG");
5
6// Disable logging entirely
7disableLogger();

Advanced Configuration

Python SDK Advanced Options

HTTP Client Configuration

The Opik Python SDK uses the httpx library to make HTTP requests. The default configuration applied to the HTTP client is suitable for most use cases, but you can customize it by registering a custom httpx client hook as in following example:

1import opik.hooks
2
3def custom_auth_client_hook(client: httpx.Client) -> None:
4 client.auth = CustomAuth()
5
6hook = opik.hooks.HttpxClientHook(
7 client_init_arguments={"trust_env": False},
8 client_modifier=custom_auth_client_hook,
9)
10opik.hooks.add_httpx_client_hook(hook)
11
12# Use the Opik SDK as usual

Make sure to add the hook before using the Opik SDK.

TypeScript SDK Advanced Options

Batching Configuration

The TypeScript SDK uses batching for optimal performance. You can configure batching behavior:

1import { Opik } from "opik";
2
3const client = new Opik({
4 // Custom batching delay (default: 300ms)
5 batchDelayMs: 1000,
6
7 // Hold data until manual flush (default: false)
8 holdUntilFlush: true,
9
10 // Custom headers
11 headers: {
12 "Custom-Header": "value",
13 },
14});
15
16// Manual flushing
17await client.flush();

Global Flush Control

1import { flushAll } from "opik";
2
3// Flush all instantiated clients
4await flushAll();

Configuration Reference

Python SDK Configuration Values

Configuration NameEnvironment VariableDescription
url_overrideOPIK_URL_OVERRIDEThe URL of the Opik server - Defaults to https://www.comet.com/opik/api
api_keyOPIK_API_KEYThe API key - Only required for Opik Cloud
workspaceOPIK_WORKSPACEThe workspace name - Only required for Opik Cloud
project_nameOPIK_PROJECT_NAMEThe project name to use
opik_track_disableOPIK_TRACK_DISABLEDisable tracking of traces and spans - Defaults to false
default_flush_timeoutOPIK_DEFAULT_FLUSH_TIMEOUTDefault flush timeout - Defaults to no timeout
opik_check_tls_certificateOPIK_CHECK_TLS_CERTIFICATECheck TLS certificate - Defaults to true
console_logging_levelOPIK_CONSOLE_LOGGING_LEVELConsole logging level - Defaults to INFO
file_logging_levelOPIK_FILE_LOGGING_LEVELFile logging level - Not configured by default
logging_fileOPIK_LOGGING_FILEFile to write logs to - Defaults to opik.log

TypeScript SDK Configuration Values

Configuration NameEnvironment VariableDescription
apiUrlOPIK_URL_OVERRIDEThe URL of the Opik server - Defaults to http://localhost:5173/api
apiKeyOPIK_API_KEYThe API key - Required for Opik Cloud
workspaceNameOPIK_WORKSPACEThe workspace name - Required for Opik Cloud
projectNameOPIK_PROJECT_NAMEThe project name - Defaults to Default Project
batchDelayMsOPIK_BATCH_DELAY_MSBatching delay in milliseconds - Defaults to 300
holdUntilFlushOPIK_HOLD_UNTIL_FLUSHHold data until manual flush - Defaults to false
N/AOPIK_LOG_LEVELLogging level - Defaults to INFO
N/AOPIK_CONFIG_PATHCustom config file location

Troubleshooting

Python SDK Troubleshooting

SSL Certificate Error

If you encounter the following error:

[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1006)

You can resolve it by either:

  • Disable the TLS certificate check by setting the OPIK_CHECK_TLS_CERTIFICATE environment variable to false
  • Add the Opik server’s certificate to your trusted certificates by setting the REQUESTS_CA_BUNDLE environment variable

Health Check Command

If you are experiencing problems with the Python SDK, such as receiving 400 or 500 errors from the backend, or being unable to connect at all, run the health check command:

$opik healthcheck

This command will analyze your configuration and backend connectivity, providing useful insights into potential issues.

Reviewing the health check output can help pinpoint the source of the problem and suggest possible resolutions.

TypeScript SDK Troubleshooting

Configuration Validation Errors

The TypeScript SDK validates configuration at startup. Common errors:

  • “OPIK_URL_OVERRIDE is not set”: Set the OPIK_URL_OVERRIDE environment variable
  • “OPIK_API_KEY is not set”: Required for Opik Cloud deployments
  • “OPIK_WORKSPACE is not set”: Required for Opik Cloud deployments

Debug Logging

Enable debug logging to troubleshoot issues:

$export OPIK_LOG_LEVEL="DEBUG"

Or programmatically:

1import { setLoggerLevel } from "opik";
2setLoggerLevel("DEBUG");

Batch Queue Issues

If data isn’t appearing in Opik:

  1. Check if data is batched: Call await client.flush() to force sending
  2. Verify configuration: Ensure correct API URL and credentials
  3. Check network connectivity: Verify firewall and proxy settings

General Troubleshooting

Environment Variables Not Loading

  1. Python: Ensure load_dotenv() is called before importing opik
  2. TypeScript: The SDK automatically loads .env files
  3. Verify file location: .env file should be in project root
  4. Check file format: No spaces around = in .env files

Configuration File Issues

  1. File location: Default is ~/.opik.config
  2. Custom location: Use OPIK_CONFIG_PATH environment variable
  3. File format: Python uses TOML, TypeScript uses INI format
  4. Permissions: Ensure file is readable by your application