Files
wehub-resource-sync 5a558eb09e
Backend Tests / discover-tests (push) Waiting to run
Backend Tests / ${{ matrix.name }} (push) Blocked by required conditions
Build and Publish SDK / build-and-publish (push) Waiting to run
Build Opik Docker Images / set-version (push) Waiting to run
Build Opik Docker Images / build-backend (push) Blocked by required conditions
Build Opik Docker Images / build-sandbox-executor-python (push) Blocked by required conditions
Build Opik Docker Images / build-python-backend (push) Blocked by required conditions
Build Opik Docker Images / build-frontend (push) Blocked by required conditions
Build Opik Docker Images / create-git-tag (push) Blocked by required conditions
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Waiting to run
Docs - Publish / run (push) Waiting to run
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Waiting to run
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Blocked by required conditions
Frontend Unit Tests / Test on Node 20 (push) Waiting to run
Guardrails E2E Tests / Select Python version matrix (push) Waiting to run
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Blocked by required conditions
Guardrails E2E Tests / 📢 Slack Notification (push) Blocked by required conditions
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Waiting to run
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Blocked by required conditions
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Waiting to run
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Waiting to run
Lint Opik Helm Chart / unittest-helm-chart (push) Waiting to run
Lint Opik Helm Chart / render-equality (push) Waiting to run
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Waiting to run
Python BE E2E Tests / Python BE E2E (push) Waiting to run
Python Backend Tests / run-python-backend-tests (push) Waiting to run
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Waiting to run
Release Drafter / update_release_draft (push) Waiting to run
SDK E2E Libraries Integration Tests / Check Secrets (push) Waiting to run
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / build-opik (push) Blocked by required conditions
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Blocked by required conditions
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Waiting to run
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Waiting to run
TypeScript SDK Build & Publish / build-and-publish (push) Waiting to run
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Waiting to run
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
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

277 lines
8.8 KiB
Python

import uuid
from typing import Annotated, Optional
import pytest
import opik
from opik import opik_context
from opik.api_objects.agent_config.cache import get_global_registry
from opik.api_objects.agent_config.config import ConfigManager
from opik.api_objects.agent_config.context import agent_config_context
from opik.api_objects.prompt.text.prompt import Prompt
from opik.api_objects.prompt.chat.chat_prompt import ChatPrompt
from . import verifiers
from ..testlib import ANY_DICT, ANY_BUT_NONE
@pytest.fixture(autouse=True)
def clear_caches_after_test():
yield
get_global_registry().clear()
@pytest.fixture
def project_name(temporary_project_name: str) -> str:
"""Alias to the shared per-test project fixture so test bodies stay
readable (``project_name`` matches the SDK kwarg name)."""
return temporary_project_name
def test_multi_class_publishes_store_only_sent_values__happyflow(
opik_client: opik.Opik,
project_name: str,
):
"""Each published blueprint stores only the values that were sent — no carry-forward."""
class ConfigA(opik.Config):
temperature: float
model: str
class ConfigB(opik.Config):
retries: int
opik_client.create_config(
ConfigA(temperature=0.5, model="gpt-4"), project_name=project_name
)
opik_client.create_config(ConfigB(retries=3), project_name=project_name)
manager = ConfigManager(
project_name=project_name,
rest_client_=opik_client.rest_client,
)
latest = manager.get_blueprint()
assert latest is not None
assert sorted(latest.keys()) == ["retries"]
project_id = opik_client.rest_client.projects.retrieve_project(name=project_name).id
history = opik_client.rest_client.agent_configs.get_blueprint_history(
project_id=project_id
).content
assert len(history) == 2
def test_publish_version_and_retrieve__happyflow(
opik_client: opik.Opik,
project_name: str,
):
"""Core lifecycle: publish, dedup, new version, get by latest / version name / env."""
class MyConfig(opik.Config):
temperature: Annotated[float, "Sampling temperature"]
model: str
hint: Optional[str]
# Publish v1 with hint=None and verify the version name comes back.
v1_name = opik_client.create_config(
MyConfig(temperature=0.5, model="gpt-3.5", hint=None), project_name=project_name
)
assert isinstance(v1_name, str) and v1_name != ""
# Backend auto-tags the first blueprint as "prod" — verify without any manual deploy_to.
get_global_registry().clear()
@opik.track(project_name=project_name)
def fetch_auto_prod():
return opik_client.get_or_create_config(
fallback=MyConfig(temperature=0.0, model="fallback", hint=None),
project_name=project_name,
env="prod",
)
auto_prod = fetch_auto_prod()
assert auto_prod.temperature == pytest.approx(0.5)
assert auto_prod.model == "gpt-3.5"
# Publishing different values (hint filled in) creates a new version.
get_global_registry().clear()
v2_name = opik_client.create_config(
MyConfig(temperature=0.8, model="gpt-4", hint="use chain-of-thought"),
project_name=project_name,
)
assert v2_name != v1_name
# version="latest" returns v2; hint is now a real value.
get_global_registry().clear()
@opik.track(project_name=project_name)
def fetch_latest():
return opik_client.get_or_create_config(
fallback=MyConfig(temperature=0.0, model="fallback", hint=None),
project_name=project_name,
version="latest",
)
latest = fetch_latest()
assert latest.temperature == pytest.approx(0.8)
assert latest.model == "gpt-4"
assert latest.hint == "use chain-of-thought"
# version= by name returns v1; hint must be None as originally published.
get_global_registry().clear()
@opik.track(project_name=project_name)
def fetch_by_name():
return opik_client.get_or_create_config(
fallback=MyConfig(temperature=0.0, model="fallback", hint=None),
project_name=project_name,
version=v1_name,
)
by_name = fetch_by_name()
assert by_name.temperature == pytest.approx(0.5)
assert by_name.hint is None
# Deploy v1 to prod; env= fetch returns v1 despite v2 being latest.
opik_client.set_config_env(project_name=project_name, version=v1_name, env="prod")
get_global_registry().clear()
@opik.track(project_name=project_name)
def fetch_by_env():
return opik_client.get_or_create_config(
fallback=MyConfig(temperature=0.0, model="fallback", hint=None),
project_name=project_name,
env="prod",
)
by_env = fetch_by_env()
assert by_env.temperature == pytest.approx(0.5)
assert by_env.hint is None
def test_prompt_field_and_trace_metadata__happyflow(
opik_client: opik.Opik,
project_name: str,
):
"""Prompt-typed and ChatPrompt-typed fields survive the roundtrip with the correct
class; field access inside a tracked function injects agent_configuration into
trace and span metadata."""
prompt_name = f"e2e-prompt-{uuid.uuid4().hex[:8]}"
chat_prompt_name = f"e2e-chat-prompt-{uuid.uuid4().hex[:8]}"
prompt_v1 = opik_client.create_prompt(
name=prompt_name, prompt="Hello v1", project_name=project_name
)
chat_prompt_v1 = opik_client.create_chat_prompt(
name=chat_prompt_name,
messages=[{"role": "user", "content": "Hi v1"}],
project_name=project_name,
)
class PromptConfig(opik.Config):
system_prompt: Prompt
chat_template: ChatPrompt
temperature: float
opik_client.create_config(
PromptConfig(
system_prompt=prompt_v1,
chat_template=chat_prompt_v1,
temperature=0.3,
),
project_name=project_name,
)
get_global_registry().clear()
id_storage = {}
@opik.track(project_name=project_name)
def run():
cfg = opik_client.get_or_create_config(
fallback=PromptConfig(
system_prompt=prompt_v1,
chat_template=chat_prompt_v1,
temperature=0.0,
),
project_name=project_name,
version="latest",
)
id_storage["trace_id"] = opik_context.get_current_trace_data().id
id_storage["span_id"] = opik_context.get_current_span_data().id
id_storage["system_prompt"] = cfg.system_prompt
id_storage["system_prompt_version_id"] = cfg.system_prompt.version_id
id_storage["chat_template"] = cfg.chat_template
id_storage["chat_template_version_id"] = cfg.chat_template.version_id
_ = cfg.temperature
return cfg
run()
opik.flush_tracker()
# Prompt field roundtrip — must come back as Prompt, not ChatPrompt.
assert isinstance(id_storage["system_prompt"], Prompt)
assert not isinstance(id_storage["system_prompt"], ChatPrompt)
assert id_storage["system_prompt_version_id"] == prompt_v1.version_id
# ChatPrompt field roundtrip — must come back as ChatPrompt, not plain Prompt.
assert isinstance(id_storage["chat_template"], ChatPrompt)
assert id_storage["chat_template_version_id"] == chat_prompt_v1.version_id
expected_meta = {
"_blueprint_id": ANY_BUT_NONE,
"blueprint_version": ANY_BUT_NONE,
"values": ANY_DICT,
}
verifiers.verify_trace(
opik_client=opik_client,
trace_id=id_storage["trace_id"],
metadata={"agent_configuration": ANY_DICT.containing(expected_meta)},
)
verifiers.verify_span(
opik_client=opik_client,
span_id=id_storage["span_id"],
trace_id=id_storage["trace_id"],
parent_span_id=None,
metadata={"agent_configuration": ANY_DICT.containing(expected_meta)},
)
def test_mask_overrides_config__happyflow(
opik_client: opik.Opik,
project_name: str,
):
"""A mask overrides selected fields while leaving untouched fields intact."""
class MyConfig(opik.Config):
temperature: float
model: str
opik_client.create_config(
MyConfig(temperature=0.5, model="gpt-4"), project_name=project_name
)
get_global_registry().clear()
manager = ConfigManager(
project_name=project_name,
rest_client_=opik_client.rest_client,
)
mask_id = manager.create_mask(parameters={"temperature": 0.9})
get_global_registry().clear()
with agent_config_context(mask_id):
@opik.track(project_name=project_name)
def fetch_with_mask():
return opik_client.get_or_create_config(
fallback=MyConfig(temperature=0.0, model="fallback"),
project_name=project_name,
version="latest",
)
result = fetch_with_mask()
assert result.temperature == pytest.approx(0.9)
assert result.model == "gpt-4"