Skip to content

Commit 12dcdf4

Browse files
[OPIK-5767] [SDK] [DOCS] feat: add agent spec integration in python SDK
1 parent a981eac commit 12dcdf4

File tree

18 files changed

+1290
-2
lines changed

18 files changed

+1290
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ The easiest way to log traces is to use one of our direct integrations. Opik sup
194194
| --------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
195195
| ADK | Log traces for Google Agent Development Kit (ADK) | [Documentation](https://www.comet.com/docs/opik/integrations/adk?utm_source=opik&utm_medium=github&utm_content=google_adk_link&utm_campaign=opik) |
196196
| AG2 | Log traces for AG2 LLM calls | [Documentation](https://www.comet.com/docs/opik/integrations/ag2?utm_source=opik&utm_medium=github&utm_content=ag2_link&utm_campaign=opik) |
197+
| Agent Spec | Log traces for Agent Spec calls | [Documentation](https://www.comet.com/docs/opik/integrations/agentspec?utm_source=opik&utm_medium=github&utm_content=agentspec_link&utm_campaign=opik) |
197198
| AIsuite | Log traces for aisuite LLM calls | [Documentation](https://www.comet.com/docs/opik/integrations/aisuite?utm_source=opik&utm_medium=github&utm_content=aisuite_link&utm_campaign=opik) |
198199
| Agno | Log traces for Agno agent orchestration framework calls | [Documentation](https://www.comet.com/docs/opik/integrations/agno?utm_source=opik&utm_medium=github&utm_content=agno_link&utm_campaign=opik) |
199200
| Anthropic | Log traces for Anthropic LLM calls | [Documentation](https://www.comet.com/docs/opik/integrations/anthropic?utm_source=opik&utm_medium=github&utm_content=anthropic_link&utm_campaign=opik) |
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Using Opik with Agent Spec\n",
8+
"\n",
9+
"[Agent Spec](https://oracle.github.io/agent-spec/development/agentspec/index.html) is a portable configuration language for defining agentic systems (agents, tools, and structured workflows).\n",
10+
"\n",
11+
"In this notebook, we will build a simple Agent Spec agent and use Opik's `AgentSpecInstrumentor` to capture a trace of the agent's tool and LLM execution."
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"## Creating an account on Comet.com\n",
19+
"\n",
20+
"[Comet](https://www.comet.com/site?from=llm&utm_source=opik&utm_medium=colab&utm_content=agentspec&utm_campaign=opik) provides a hosted version of the Opik platform, [simply create an account](https://www.comet.com/signup?from=llm&utm_source=opik&utm_medium=colab&utm_content=agentspec&utm_campaign=opik) and grab your API Key.\n",
21+
"\n",
22+
"> You can also run the Opik platform locally, see the [installation guide](https://www.comet.com/docs/opik/self-host/overview/?from=llm&utm_source=opik&utm_medium=colab&utm_content=agentspec&utm_campaign=opik) for more information."
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"metadata": {},
29+
"outputs": [],
30+
"source": "%pip install --upgrade opik \"pyagentspec[langgraph]\" opentelemetry-sdk opentelemetry-instrumentation"
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"import opik\n",
39+
"\n",
40+
"opik.configure(use_local=False)"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"## Preparing our environment\n",
48+
"\n",
49+
"This demo uses OpenAI as the LLM provider. Set your OpenAI API key as an environment variable:"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"import os\n",
59+
"import getpass\n",
60+
"\n",
61+
"if \"OPENAI_API_KEY\" not in os.environ:\n",
62+
" os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"## Define an Agent Spec agent\n",
70+
"\n",
71+
"We'll define a small calculator agent with a couple of tools:"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {},
78+
"outputs": [],
79+
"source": [
80+
"from pyagentspec.agent import Agent\n",
81+
"from pyagentspec.llms import OpenAiConfig\n",
82+
"from pyagentspec.property import FloatProperty\n",
83+
"from pyagentspec.tools import ServerTool\n",
84+
"\n",
85+
"\n",
86+
"def build_agentspec_agent() -> Agent:\n",
87+
" tools = [\n",
88+
" ServerTool(\n",
89+
" name=\"sum\",\n",
90+
" description=\"Sum two numbers\",\n",
91+
" inputs=[FloatProperty(title=\"a\"), FloatProperty(title=\"b\")],\n",
92+
" outputs=[FloatProperty(title=\"result\")],\n",
93+
" ),\n",
94+
" ServerTool(\n",
95+
" name=\"subtract\",\n",
96+
" description=\"Subtract two numbers\",\n",
97+
" inputs=[FloatProperty(title=\"a\"), FloatProperty(title=\"b\")],\n",
98+
" outputs=[FloatProperty(title=\"result\")],\n",
99+
" ),\n",
100+
" ]\n",
101+
"\n",
102+
" return Agent(\n",
103+
" name=\"calculator_agent\",\n",
104+
" description=\"An agent that provides assistance with tool use.\",\n",
105+
" llm_config=OpenAiConfig(name=\"openai-gpt-5-mini\", model_id=\"gpt-5-mini\"),\n",
106+
" system_prompt=(\n",
107+
" \"You are a helpful calculator agent.\\n\"\n",
108+
" \"Your duty is to compute the result of the given operation using tools, \"\n",
109+
" \"and to output the result.\\n\"\n",
110+
" \"It's important that you reply with the result only.\\n\"\n",
111+
" ),\n",
112+
" tools=tools,\n",
113+
" )"
114+
]
115+
},
116+
{
117+
"cell_type": "markdown",
118+
"metadata": {},
119+
"source": [
120+
"## Run the agent with Opik tracing enabled\n",
121+
"\n",
122+
"Wrap the agent execution in `AgentSpecInstrumentor().instrument_context(...)` to capture traces in Opik.\n",
123+
"\n",
124+
"> Agent traces can include prompts, tool inputs/outputs, and messages. If you need to avoid logging sensitive information, set `mask_sensitive_information=True`."
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": null,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"from opik.integrations.agentspec import AgentSpecInstrumentor\n",
134+
"from pyagentspec.adapters.langgraph import AgentSpecLoader\n",
135+
"\n",
136+
"agent = build_agentspec_agent()\n",
137+
"\n",
138+
"tool_registry = {\n",
139+
" \"sum\": lambda a, b: a + b,\n",
140+
" \"subtract\": lambda a, b: a - b,\n",
141+
"}\n",
142+
"\n",
143+
"langgraph_agent = AgentSpecLoader(tool_registry=tool_registry).load_component(agent)\n",
144+
"\n",
145+
"with AgentSpecInstrumentor().instrument_context(\n",
146+
" project_name=\"agentspec-demo\",\n",
147+
" mask_sensitive_information=False,\n",
148+
"):\n",
149+
" messages = []\n",
150+
"\n",
151+
" messages.append({\"role\": \"user\", \"content\": \"Compute 13.5 + 2.25 using the sum tool.\"})\n",
152+
" response = langgraph_agent.invoke(\n",
153+
" input={\"messages\": messages},\n",
154+
" config={\"configurable\": {\"thread_id\": \"1\"}},\n",
155+
" )\n",
156+
" agent_answer = response[\"messages\"][-1].content.strip()\n",
157+
" print(\"AGENT >>>\", agent_answer)\n",
158+
" messages.append({\"role\": \"assistant\", \"content\": agent_answer})\n",
159+
"\n",
160+
" messages.append({\"role\": \"user\", \"content\": \"Now compute 10 - 3.5 using the subtract tool.\"})\n",
161+
" response = langgraph_agent.invoke(\n",
162+
" input={\"messages\": messages},\n",
163+
" config={\"configurable\": {\"thread_id\": \"1\"}},\n",
164+
" )\n",
165+
" agent_answer = response[\"messages\"][-1].content.strip()\n",
166+
" print(\"AGENT >>>\", agent_answer)"
167+
]
168+
},
169+
{
170+
"cell_type": "markdown",
171+
"metadata": {},
172+
"source": [
173+
"After running the cell above, open Opik and navigate to the `agentspec-demo` project to inspect the trace tree and debug tool usage and LLM generations."
174+
]
175+
}
176+
],
177+
"metadata": {
178+
"kernelspec": {
179+
"display_name": "py312_llm_eval",
180+
"language": "python",
181+
"name": "python3"
182+
},
183+
"language_info": {
184+
"codemirror_mode": {
185+
"name": "ipython",
186+
"version": 3
187+
},
188+
"file_extension": ".py",
189+
"mimetype": "text/x-python",
190+
"name": "python",
191+
"nbconvert_exporter": "python",
192+
"pygments_lexer": "ipython3",
193+
"version": "3.12.4"
194+
}
195+
},
196+
"nbformat": 4,
197+
"nbformat_minor": 4
198+
}

apps/opik-documentation/documentation/fern/docs.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ navigation:
493493
- page: AG2
494494
path: docs/tracing/integrations/ag2.mdx
495495
slug: ag2
496+
- page: Agent Spec
497+
path: docs/tracing/integrations/agentspec.mdx
498+
slug: agentspec
496499
- page: Agno
497500
path: docs/tracing/integrations/agno.mdx
498501
slug: agno
@@ -1152,6 +1155,9 @@ redirects:
11521155
- source: "/docs/opik/tracing/integrations/aisuite"
11531156
destination: "/docs/opik/integrations/aisuite"
11541157
permanent: true
1158+
- source: "/docs/opik/tracing/integrations/agentspec"
1159+
destination: "/docs/opik/integrations/agentspec"
1160+
permanent: true
11551161
- source: "/docs/opik/tracing/integrations/agno"
11561162
destination: "/docs/opik/integrations/agno"
11571163
permanent: true
@@ -1352,6 +1358,9 @@ redirects:
13521358
- source: "/docs/opik/cookbook/anthropic"
13531359
destination: "/docs/opik/integrations/anthropic"
13541360
permanent: true
1361+
- source: "/docs/opik/cookbook/agentspec"
1362+
destination: "/docs/opik/integrations/agentspec"
1363+
permanent: true
13551364
- source: "/docs/opik/cookbook/bedrock"
13561365
destination: "/docs/opik/integrations/bedrock"
13571366
permanent: true

apps/opik-documentation/documentation/fern/docs/faq.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Gemini, Groq, Mistral AI, Novita AI, Ollama, OpenAI (Python & JS/TS), Predibase,
170170
WatsonX, xAI Grok
171171

172172
**Frameworks:**
173-
AG2, Agno, Autogen, CrewAI, DSPy, Haystack, Instructor, LangChain (Python & JS/TS), LangGraph,
173+
AG2, Agent Spec, Agno, Autogen, CrewAI, DSPy, Haystack, Instructor, LangChain (Python & JS/TS), LangGraph,
174174
LlamaIndex, Mastra, Pydantic AI, Semantic Kernel, Smolagents, Spring AI, Strands Agents, VoltAgent,
175175
OpenAI Agents, Google Agent Development Kit, LiveKit Agents, BeeAI
176176

0 commit comments

Comments
 (0)