chore: import upstream snapshot with attribution
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / render-equality (push) Has been cancelled
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Has been cancelled
Python BE E2E Tests / Python BE E2E (push) Has been cancelled
Python Backend Tests / run-python-backend-tests (push) Has been cancelled
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
SDK E2E Libraries Integration Tests / Check Secrets (push) Has been cancelled
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Has been cancelled
SDK E2E Libraries Integration Tests / build-opik (push) Has been cancelled
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Has been cancelled
TypeScript SDK Build & Publish / build-and-publish (push) Has been cancelled
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Has been cancelled
Backend Tests / discover-tests (push) Has been cancelled
Backend Tests / ${{ matrix.name }} (push) Has been cancelled
Build and Publish SDK / build-and-publish (push) Has been cancelled
Build Opik Docker Images / set-version (push) Has been cancelled
Build Opik Docker Images / build-backend (push) Has been cancelled
Build Opik Docker Images / build-sandbox-executor-python (push) Has been cancelled
Build Opik Docker Images / build-python-backend (push) Has been cancelled
Build Opik Docker Images / build-frontend (push) Has been cancelled
Build Opik Docker Images / create-git-tag (push) Has been cancelled
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Has been cancelled
Docs - Publish / run (push) Has been cancelled
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Has been cancelled
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Has been cancelled
Frontend Unit Tests / Test on Node 20 (push) Has been cancelled
Guardrails E2E Tests / Select Python version matrix (push) Has been cancelled
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Guardrails E2E Tests / 📢 Slack Notification (push) Has been cancelled
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Has been cancelled
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Has been cancelled
Lint Opik Helm Chart / unittest-helm-chart (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:25:44 +08:00
commit 5a558eb09e
11579 changed files with 1795921 additions and 0 deletions
@@ -0,0 +1,90 @@
distributed_headers
===================
.. autofunction:: opik.decorator.context_manager.distributed_headers
Examples
--------
Basic usage in a server endpoint
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
from opik.decorator.context_manager import distributed_headers
from fastapi import FastAPI, Request
app = FastAPI()
@app.post("/generate_response")
def generate_llm_response(request: Request) -> str:
# Extract distributed headers from the incoming request
headers = {
"opik_trace_id": request.headers.get("opik_trace_id"),
"opik_parent_span_id": request.headers.get("opik_parent_span_id"),
}
# Use the context manager to handle distributed headers
with distributed_headers(headers):
result = my_llm_application()
return result
With flush enabled
~~~~~~~~~~~~~~~~~~
.. code-block:: python
from opik.decorator.context_manager import distributed_headers
def process_request(headers_dict):
# Flush data immediately after the root span is processed
with distributed_headers(headers_dict, flush=True):
# Your processing logic here
pass
Using with the track decorator
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
from opik import track
from opik.decorator.context_manager import distributed_headers
from flask import Flask, request
app = Flask(__name__)
@track()
def my_llm_function(prompt: str) -> str:
# Your LLM logic here
return "response"
@app.route("/api/generate", methods=["POST"])
def api_endpoint():
# Extract headers from the request
headers = {
"opik_trace_id": request.headers.get("opik_trace_id"),
"opik_parent_span_id": request.headers.get("opik_parent_span_id"),
}
# Create distributed trace context
with distributed_headers(headers):
result = my_llm_function(prompt=request.json.get("prompt"))
return {"result": result}
Error handling
~~~~~~~~~~~~~~
.. code-block:: python
from opik.decorator.context_manager import distributed_headers
try:
with distributed_headers(incoming_headers):
# Code that might fail
result = risky_operation()
except Exception as e:
# The context manager automatically logs the error
# and attaches error information to the root span
print(f"Operation failed: {e}")
@@ -0,0 +1,67 @@
Context Managers
================
Opik provides context managers for creating and managing traces and spans in your application. These context managers allow you to easily instrument your code with tracing capabilities while ensuring proper cleanup and error handling.
Context managers are particularly useful when you need fine-grained control over trace and span creation, or when working with code that doesn't fit well with the `@track` decorator pattern.
Available Context Managers
---------------------------
.. toctree::
:maxdepth: 1
:titlesonly:
start_as_current_span
start_as_current_trace
distributed_headers
Key Features
------------
- **Automatic Error Handling**: Context managers automatically capture and log errors that occur within their scope
- **Distributed Tracing**: Support for distributed tracing headers to maintain trace context across service boundaries
- **Flexible Configuration**: Rich set of parameters for customizing trace and span behavior
- **Resource Management**: Automatic cleanup and flushing of trace data
- **Nested Support**: Context managers can be nested to create hierarchical trace structures
Basic Usage Pattern
-------------------
.. code-block:: python
import opik
with opik.start_as_current_trace("my-trace", project_name="my-project") as trace:
# Your application logic here
trace.input = {"user_query": "Explain quantum computing"}
trace.output = {"response": "Quantum computing is..."}
trace.tags = ["chat"]
trace.metadata = {"model": "gpt-4", "temperature": 0.7}
# Basic span creation
with opik.start_as_current_span("llm-call", type="llm", project_name="my-project") as span:
# Your LLM call here
span.input = {"prompt": "Explain quantum computing"}
span.output = {"response": "Quantum computing is..."}
span.model = "gpt-4"
span.provider = "openai"
span.usage = {
"prompt_tokens": 10,
"completion_tokens": 50,
"total_tokens": 60
}
When to Use Context Managers
----------------------------
Use context managers when:
- You need explicit control over trace/span lifecycle
- Working with code that can't be easily decorated
- Implementing custom error handling patterns
- Building distributed tracing across service boundaries
- Creating complex nested trace hierarchies
For simpler use cases, consider using the `@track` decorator instead.
@@ -0,0 +1,71 @@
start_as_current_span
=====================
.. autofunction:: opik.start_as_current_span
Examples
--------
Basic usage
~~~~~~~~~~~
.. code-block:: python
import opik
with opik.start_as_current_span("my_operation") as span:
# Your code here
span.metadata["custom_key"] = "custom_value"
print("Executing operation...")
With input and output data
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
import opik
with opik.start_as_current_span(
name="llm_completion",
type="llm",
input={"prompt": "What is the capital of France?"},
output={"response": "The capital of France is Paris."},
tags=["llm", "completion"],
metadata={"model": "gpt-3.5-turbo"}
) as span:
# Your LLM code here
pass
With distributed tracing
~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
import os
import opik
# extract headers from environment
distributed_trace_headers = os.environ.get("opik_distributed_trace_headers")
with opik.start_as_current_span(
"process_request",
opik_distributed_trace_headers=distributed_trace_headers
) as span:
# Your code here
pass
Error handling
~~~~~~~~~~~~~~
.. code-block:: python
import opik
try:
with opik.start_as_current_span("risky_operation") as span:
# Code that might fail
raise ValueError("Something went wrong")
except ValueError as e:
# The span will automatically capture error information
print(f"Operation failed: {e}")
# Error details are stored in span.error_info
@@ -0,0 +1,87 @@
start_as_current_trace
======================
.. autofunction:: opik.start_as_current_trace
Examples
--------
Basic usage
~~~~~~~~~~~
.. code-block:: python
import opik
with opik.start_as_current_trace("my_trace") as trace:
# Your code here
trace.metadata["custom_key"] = "custom_value"
print("Executing trace...")
With input and output data
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
import opik
with opik.start_as_current_trace(
name="user_query_processing",
input={"user_question": "What is machine learning?"},
output={"answer": "Machine learning is a subset of AI..."},
tags=["user_query", "ai"],
metadata={"session_id": "abc123"}
) as trace:
# Your processing code here
pass
With conversational threads support using `thread_id` identifier
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
import opik
import threading
with opik.start_as_current_trace(
"chatbot_conversation",
project_name="my_project",
thread_id="00f067aa0ba902b7",
) as trace:
# Your processing code here
pass
Error handling
~~~~~~~~~~~~~~
.. code-block:: python
import opik
try:
with opik.start_as_current_trace("risky_trace") as trace:
# Code that might fail
raise RuntimeError("Something went wrong")
except RuntimeError as e:
# The trace will automatically capture error information
print(f"Trace failed: {e}")
# Error details are stored in trace.error_info
Nested spans within a trace
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: python
import opik
with opik.start_as_current_trace("main_workflow") as trace:
# Main workflow code
with opik.start_as_current_span("sub_operation_1") as span1:
# First sub-operation
pass
with opik.start_as_current_span("sub_operation_2") as span2:
# Second sub-operation
pass