sdks/python/README.md
The Opik Python SDK allows you to integrate your Python applications with the Opik platform, enabling comprehensive tracing, evaluation, and monitoring of your LLM systems. Opik helps you build, evaluate, and optimize LLM systems that run better, faster, and cheaper.
Opik is an open-source LLM evaluation platform by Comet. For more information about the broader Opik ecosystem, visit our main GitHub repository, Website, or Documentation.
Get started quickly with Opik using our interactive notebook:
<a href="https://colab.research.google.com/github/comet-ml/opik/blob/master/apps/opik-documentation/documentation/docs/cookbook/opik_quickstart.ipynb"> </a>Install the opik package using pip or uv:
# using pip
pip install opik
# using uv (faster)
uv pip install opik
Configure the Python SDK by running the opik configure command. This will prompt you for your Opik server address (for self-hosted instances) or your API key and workspace (for Comet.com):
opik configure
You can also configure the SDK programmatically in your Python code:
import opik
# For Comet.com Cloud
opik.configure(
api_key="YOUR_API_KEY",
workspace="YOUR_WORKSPACE", # Usually found in your Comet URL: https://www.comet.com/YOUR_WORKSPACE/...
project_name="optional-project-name" # Optional: set a default project for traces
)
# For self-hosted Opik instances
# opik.configure(use_local=True, project_name="optional-project-name")
Refer to the Python SDK documentation for more configuration options.
Control tracing behavior at runtime without code changes:
import opik
# Disable tracing globally
opik.set_tracing_active(False)
# Check current state
print(opik.is_tracing_active()) # False
# Re-enable tracing
opik.set_tracing_active(True)
# Reset to configuration default
opik.reset_tracing_to_config_default()
This is useful for:
See examples/dynamic_tracing_cookbook.py for comprehensive usage patterns.
The easiest way to log traces is to use the @opik.track decorator:
import opik
# Ensure Opik is configured (see Configuration section above)
# opik.configure(...)
@opik.track
def my_llm_function(user_question: str) -> str:
# Your LLM call or business logic here
# For example:
# response = openai.ChatCompletion.create(...)
response = f"Echoing: {user_question}"
# You can add metadata to your trace
opik.set_tags(["example", "basic-usage"])
opik.log_metadata({"question_length": len(user_question)})
return response
my_llm_function("Hello, Opik!")
Traces will appear in your configured Opik project. Opik also offers many direct integrations for popular LLM frameworks.
Opik provides powerful CLI commands for exporting and importing data between projects:
For detailed information about the CLI export/import functionality, see Import/Export Commands.
For a more general contribution guide (backend + frontend + SDK) see our root Contribution guide.
This guide is still in progress, however, it already contains useful information that you should know before submitting your PR.
We care a lot about the code maintainability. Well-organized logic which is easy to extend, re-factor and, most importantly - read, is what we are striving for.
_protected.Import module - not name. Instead of this:
from threading import Thread # bad!
thread = Thread()
do this:
import threading # good!
thread = threading.Thread
If the import statement is too big, you can do the following
from opik.rest_api.core import error as rest_api_error # ok!
If you are working in the namespace, you likely don't need to keep most of the parent namespaces
# inside opik.api_objects.dataset
from . import dataset_item # ok!
Of course, there might be exceptions from this rule, for example, some common types can be imported as is.
from typing import Dict, List # ok!
from opik.types import FeedbackScoreDict # ok!
for d in dataset_items: # bad!
for item in dataset_items: # ok!
...
for dataset_item in dataset_items # ok!
...
utils.py, helpers.py, misc.py etc. Especially in the big namespaces. They can quickly become dumps where people put everything that they haven't been able to create a better place for in 10 seconds after they started thinking about it. You can create those files though, but they should be localized in their namespaces designed for some specific features. In vast majority of cases there are better module names.We highly encourage writing tests and we develop a lot of features in a test-driven way.
if-statements in your code or some non-trivial boiler-plate code - it's probably a reason to think about add some unit tests for that. The more complex your code, the higher chance you'll be asked to provide unit tests for it.fake_backend fixture together with a special Opik assertions DSL(domain-specific language) for a lot of unit tests and library integration tests. We encourage you to use it as well! There is plenty of examples, you can take a look at tests/unit/decorator/test_tracker_outputs.py or tests/library_integration/openai/test_openai.py. It provides a pretty simple API for specifying the traces content you expect your feature to log.