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

414 lines
14 KiB
Python

import opik
import pytest
pytest.importorskip("pyagentspec")
from opik.integrations.agentspec import AgentSpecInstrumentor, OpikSpanProcessor
from pyagentspec.llms import OpenAiConfig
from pyagentspec.tools import ClientTool
from pyagentspec.tracing.events import (
LlmGenerationRequest,
LlmGenerationResponse,
ToolExecutionRequest,
ToolExecutionResponse,
)
from pyagentspec.tracing.messages.message import Message
from pyagentspec.tracing.spans import LlmGenerationSpan, ToolExecutionSpan
from pyagentspec.tracing.trace import Trace, get_trace
from ... import llm_constants
from ...testlib import (
ANY_BUT_NONE,
ANY_DICT,
ANY_LIST,
ANY_STRING,
SpanModel,
TraceModel,
assert_equal,
)
@pytest.fixture
def flush_tracker():
# Make sure that
# - traces don't leak across tests
# - traces are sent before being checked
try:
yield opik.flush_tracker
finally:
opik.flush_tracker()
def test_opik_span_processor_tool_and_llm_spans_are_forwarded_to_opik(
fake_backend,
flush_tracker,
):
project_name = "agentspec-integration-test"
tool = ClientTool(name="lookup_weather")
llm_config = OpenAiConfig(name="demo-model", model_id=llm_constants.OPENAI_GPT_NANO)
span_processor = OpikSpanProcessor(
project_name=project_name,
mask_sensitive_information=False,
)
with Trace(name="AgentSpec workflow", span_processors=[span_processor]):
with ToolExecutionSpan(
name="weather_tool",
tool=tool,
events=[
ToolExecutionRequest(
tool=tool,
inputs={"city": "Zurich"},
request_id="tool-request",
),
ToolExecutionResponse(
tool=tool,
outputs={"temperature": "18C"},
request_id="tool-request",
),
],
):
pass
with LlmGenerationSpan(
name="llm_generation",
llm_config=llm_config,
events=[
LlmGenerationRequest(
llm_config=llm_config,
prompt=[Message(content="my prompt", role="system", sender="me")],
tools=[],
request_id="llm-request",
),
LlmGenerationResponse(
llm_config=llm_config,
content="sunny",
request_id="llm-request",
input_tokens=11,
output_tokens=4,
),
],
):
pass
flush_tracker()
assert len(fake_backend.trace_trees) == 1
EXPECTED_TRACE_TREE = TraceModel(
id=ANY_BUT_NONE,
name="AgentSpec workflow",
project_name=project_name,
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
input=ANY_DICT.containing(
{
"request_id": "llm-request",
"prompt": [
{
"id": None,
"content": "my prompt",
"role": "system",
"sender": "me",
}
],
}
),
output={
"response": "sunny",
"tool_calls": [],
"completion_id": None,
},
last_updated_at=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="RootSpan",
type="general",
project_name=project_name,
input={},
output=None,
metadata=ANY_DICT.containing({"events": []}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="weather_tool",
type="tool",
project_name=project_name,
input={"city": "Zurich"},
output={"temperature": "18C"},
metadata=ANY_DICT.containing({"events": ANY_LIST}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[],
),
SpanModel(
id=ANY_BUT_NONE,
name="llm_generation",
type="llm",
project_name=project_name,
model="demo-model",
input=ANY_DICT.containing(
{
"request_id": "llm-request",
"prompt": [
{
"id": None,
"content": "my prompt",
"role": "system",
"sender": "me",
}
],
}
),
output={
"response": "sunny",
"tool_calls": [],
"completion_id": None,
},
usage=ANY_DICT.containing(
{
"prompt_tokens": 11,
"completion_tokens": 4,
"total_tokens": 15,
}
),
metadata=ANY_DICT.containing({"events": ANY_LIST}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[],
),
],
),
],
)
trace_tree = fake_backend.trace_trees[0]
assert_equal(EXPECTED_TRACE_TREE, trace_tree)
assert len(trace_tree.spans[0].spans[0].metadata["events"]) == 2
assert len(trace_tree.spans[0].spans[1].metadata["events"]) == 2
def test_agentspec_instrumentor_context_manager_records_spans_and_cleans_up(
fake_backend,
flush_tracker,
):
project_name = "agentspec-instrumentor-test"
tool = ClientTool(name="lookup_time")
instrumentor = AgentSpecInstrumentor()
with instrumentor.instrument_context(
project_name=project_name,
mask_sensitive_information=False,
):
assert get_trace() is not None
with ToolExecutionSpan(
name="time_tool",
tool=tool,
events=[
ToolExecutionRequest(
tool=tool,
inputs={"timezone": "Europe/Zurich"},
request_id="tool-request",
),
ToolExecutionResponse(
tool=tool,
outputs={"time": "09:30"},
request_id="tool-request",
),
],
):
pass
flush_tracker()
assert get_trace() is None
assert len(fake_backend.trace_trees) == 1
EXPECTED_TRACE_TREE = TraceModel(
id=ANY_BUT_NONE,
name="Trace",
project_name=project_name,
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
last_updated_at=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="RootSpan",
type="general",
project_name=project_name,
input={},
output=None,
metadata=ANY_DICT.containing({"events": []}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="time_tool",
type="tool",
project_name=project_name,
input={"timezone": "Europe/Zurich"},
output={"time": "09:30"},
metadata=ANY_DICT.containing({"events": ANY_LIST}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[],
)
],
)
],
)
trace_tree = fake_backend.trace_trees[0]
assert_equal(EXPECTED_TRACE_TREE, trace_tree)
assert len(trace_tree.spans[0].spans[0].metadata["events"]) == 2
def test_opik_span_processor_llm_response_is_preserved_when_span_ends_with_error(
fake_backend_without_batching,
flush_tracker,
):
project_name = "agentspec-llm-error-test"
llm_config = OpenAiConfig(name="demo-model", model_id=llm_constants.OPENAI_GPT_NANO)
span_processor = OpikSpanProcessor(
project_name=project_name,
mask_sensitive_information=False,
)
with Trace(name="AgentSpec workflow", span_processors=[span_processor]):
with pytest.raises(RuntimeError, match="llm failed after response"):
with LlmGenerationSpan(
name="llm_generation",
llm_config=llm_config,
events=[
LlmGenerationRequest(
llm_config=llm_config,
prompt=[
Message(
content="my prompt",
role="system",
sender="me",
)
],
tools=[],
request_id="llm-request",
),
LlmGenerationResponse(
llm_config=llm_config,
content="sunny",
request_id="llm-request",
input_tokens=11,
output_tokens=4,
),
],
):
raise RuntimeError("llm failed after response")
flush_tracker()
assert len(fake_backend_without_batching.trace_trees) == 1
EXPECTED_TRACE_TREE = TraceModel(
id=ANY_BUT_NONE,
name="AgentSpec workflow",
project_name=project_name,
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
input=ANY_DICT.containing(
{
"request_id": "llm-request",
"prompt": [
{
"id": None,
"content": "my prompt",
"role": "system",
"sender": "me",
}
],
}
),
output={
"response": "sunny",
"tool_calls": [],
"completion_id": None,
},
last_updated_at=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="RootSpan",
type="general",
project_name=project_name,
input={},
output=None,
metadata=ANY_DICT.containing({"events": []}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[
SpanModel(
id=ANY_BUT_NONE,
name="llm_generation",
type="llm",
project_name=project_name,
model="demo-model",
input=ANY_DICT.containing(
{
"request_id": "llm-request",
"prompt": [
{
"id": None,
"content": "my prompt",
"role": "system",
"sender": "me",
}
],
}
),
output={
"response": "sunny",
"tool_calls": [],
"completion_id": None,
},
usage=ANY_DICT.containing(
{
"prompt_tokens": 11,
"completion_tokens": 4,
"total_tokens": 15,
}
),
error_info={
"exception_type": "RuntimeError",
"message": "llm failed after response",
"traceback": ANY_STRING.containing(
"RuntimeError: llm failed after response"
),
},
metadata=ANY_DICT.containing({"events": ANY_LIST}),
start_time=ANY_BUT_NONE,
end_time=ANY_BUT_NONE,
spans=[],
)
],
),
],
)
trace_tree = fake_backend_without_batching.trace_trees[0]
assert_equal(EXPECTED_TRACE_TREE, trace_tree)
assert len(trace_tree.spans[0].spans[0].metadata["events"]) == 3
def test_agentspec_instrumentor_active_trace_exists_raises_value_error():
instrumentor = AgentSpecInstrumentor()
with Trace(name="existing trace"):
with pytest.raises(
ValueError,
match="Agent Spec Trace already active",
):
instrumentor.instrument(project_name="agentspec-instrumentor-test")