agentx-python 0.4.13__tar.gz → 0.4.14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agentx_python-0.4.13 → agentx_python-0.4.14}/PKG-INFO +1 -1
- agentx_python-0.4.14/agentx/integrations/openai_agents.py +224 -0
- agentx_python-0.4.14/agentx/version.py +1 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx_python.egg-info/PKG-INFO +1 -1
- agentx_python-0.4.13/agentx/integrations/openai_agents.py +0 -125
- agentx_python-0.4.13/agentx/version.py +0 -1
- {agentx_python-0.4.13 → agentx_python-0.4.14}/LICENSE +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/README.md +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/agentx.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/_term.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/adapters/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/adapters/http_endpoint.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/adapters/precomputed.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/adapters/raw.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/client.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/datasets.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/models.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/redaction.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/reporting.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/results.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/runner.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/evaluations/tracing.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/exceptions.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/integrations/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/integrations/anthropic.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/integrations/crewai.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/integrations/langchain.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/resources/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/resources/agent.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/resources/conversation.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/resources/workforce.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/tracing/__init__.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/tracing/ci_types.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/tracing/ingest_client.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/tracing/tracer.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx/util.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx_python.egg-info/SOURCES.txt +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx_python.egg-info/dependency_links.txt +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx_python.egg-info/requires.txt +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/agentx_python.egg-info/top_level.txt +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/setup.cfg +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/setup.py +0 -0
- {agentx_python-0.4.13 → agentx_python-0.4.14}/tests/test_integration.py +0 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""
|
|
2
|
+
OpenAI Agents SDK integration for AgentX production tracing.
|
|
3
|
+
|
|
4
|
+
Usage::
|
|
5
|
+
|
|
6
|
+
from agentx.integrations.openai_agents import AgentXTracingProcessor
|
|
7
|
+
|
|
8
|
+
processor = AgentXTracingProcessor(agentx.tracer)
|
|
9
|
+
|
|
10
|
+
# Register once at startup — affects all agent runs in the process
|
|
11
|
+
from agents import add_trace_processor
|
|
12
|
+
add_trace_processor(processor)
|
|
13
|
+
|
|
14
|
+
Requires: ``pip install "agentx-python[openai-agents]"``
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import time
|
|
19
|
+
from datetime import datetime, timezone
|
|
20
|
+
from typing import Any, Dict, List, Optional
|
|
21
|
+
|
|
22
|
+
from agentx.tracing.tracer import Tracer, _safe_serialize
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _iso_to_ts(iso: Optional[str]) -> Optional[float]:
|
|
26
|
+
"""Parse an ISO-8601 timestamp string to a Unix timestamp float."""
|
|
27
|
+
if not iso:
|
|
28
|
+
return None
|
|
29
|
+
try:
|
|
30
|
+
return datetime.fromisoformat(iso.replace("Z", "+00:00")).timestamp()
|
|
31
|
+
except Exception:
|
|
32
|
+
return None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _span_latency_ms(span: Any) -> Optional[int]:
|
|
36
|
+
"""Return span duration in ms from ISO started_at / ended_at strings."""
|
|
37
|
+
t0 = _iso_to_ts(getattr(span, "started_at", None))
|
|
38
|
+
t1 = _iso_to_ts(getattr(span, "ended_at", None))
|
|
39
|
+
if t0 is not None and t1 is not None:
|
|
40
|
+
return max(0, int((t1 - t0) * 1000))
|
|
41
|
+
return None
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _extract_text(output: Any) -> Optional[str]:
|
|
45
|
+
"""
|
|
46
|
+
Best-effort extraction of a human-readable string from a GenerationSpanData
|
|
47
|
+
or ResponseSpanData output, which can be a list of message dicts in either
|
|
48
|
+
the Chat Completions or Responses API format.
|
|
49
|
+
"""
|
|
50
|
+
if output is None:
|
|
51
|
+
return None
|
|
52
|
+
if isinstance(output, str):
|
|
53
|
+
return output
|
|
54
|
+
if not isinstance(output, (list, tuple)):
|
|
55
|
+
return _safe_serialize(output)
|
|
56
|
+
|
|
57
|
+
for item in reversed(output):
|
|
58
|
+
if not isinstance(item, dict):
|
|
59
|
+
# Could be a pydantic model — try .text or .content
|
|
60
|
+
text = getattr(item, "text", None) or getattr(item, "content", None)
|
|
61
|
+
if text and isinstance(text, str):
|
|
62
|
+
return text
|
|
63
|
+
continue
|
|
64
|
+
|
|
65
|
+
# Responses API: {"type": "message", "content": [{"type": "output_text", "text": "..."}]}
|
|
66
|
+
if item.get("type") == "message":
|
|
67
|
+
content = item.get("content", [])
|
|
68
|
+
if isinstance(content, list):
|
|
69
|
+
for part in content:
|
|
70
|
+
if isinstance(part, dict) and part.get("type") == "output_text":
|
|
71
|
+
return part.get("text")
|
|
72
|
+
|
|
73
|
+
# Chat Completions API: {"role": "assistant", "content": "..."}
|
|
74
|
+
role = item.get("role", "")
|
|
75
|
+
if role == "assistant":
|
|
76
|
+
content = item.get("content")
|
|
77
|
+
if isinstance(content, str):
|
|
78
|
+
return content
|
|
79
|
+
if isinstance(content, list):
|
|
80
|
+
for part in content:
|
|
81
|
+
if isinstance(part, dict) and part.get("type") == "text":
|
|
82
|
+
return part.get("text")
|
|
83
|
+
|
|
84
|
+
return _safe_serialize(output)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _extract_input_text(input_data: Any) -> Optional[str]:
|
|
88
|
+
"""Extract the user's input query from a generation span's input messages."""
|
|
89
|
+
if input_data is None:
|
|
90
|
+
return None
|
|
91
|
+
if isinstance(input_data, str):
|
|
92
|
+
return input_data
|
|
93
|
+
if not isinstance(input_data, (list, tuple)):
|
|
94
|
+
return _safe_serialize(input_data)
|
|
95
|
+
|
|
96
|
+
# Walk messages and grab the last user message content
|
|
97
|
+
user_text = None
|
|
98
|
+
for item in input_data:
|
|
99
|
+
if not isinstance(item, dict):
|
|
100
|
+
continue
|
|
101
|
+
if item.get("role") == "user":
|
|
102
|
+
content = item.get("content")
|
|
103
|
+
if isinstance(content, str):
|
|
104
|
+
user_text = content
|
|
105
|
+
elif isinstance(content, list):
|
|
106
|
+
for part in content:
|
|
107
|
+
if isinstance(part, dict) and part.get("type") in ("text", "input_text"):
|
|
108
|
+
user_text = part.get("text")
|
|
109
|
+
|
|
110
|
+
return user_text or _safe_serialize(input_data)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
class AgentXTracingProcessor:
|
|
114
|
+
"""
|
|
115
|
+
Implements the ``TracingProcessor`` interface expected by the OpenAI Agents SDK
|
|
116
|
+
(``agents.add_trace_processor``).
|
|
117
|
+
|
|
118
|
+
Sends one AgentX trace per top-level agent run.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
def __init__(
|
|
122
|
+
self,
|
|
123
|
+
tracer: Tracer,
|
|
124
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
125
|
+
session_id: Optional[str] = None,
|
|
126
|
+
) -> None:
|
|
127
|
+
self._tracer = tracer
|
|
128
|
+
self._metadata = metadata
|
|
129
|
+
self._session_id = session_id
|
|
130
|
+
# trace_id → accumulated state
|
|
131
|
+
self._spans: Dict[str, Dict[str, Any]] = {}
|
|
132
|
+
|
|
133
|
+
# ------------------------------------------------------------------
|
|
134
|
+
# TracingProcessor protocol
|
|
135
|
+
# ------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
def on_trace_start(self, trace: Any) -> None:
|
|
138
|
+
trace_id = getattr(trace, "trace_id", None) or str(id(trace))
|
|
139
|
+
self._spans[trace_id] = {
|
|
140
|
+
"start": time.time(),
|
|
141
|
+
"name": getattr(trace, "name", "openai-agent"),
|
|
142
|
+
"input": None,
|
|
143
|
+
"output": None,
|
|
144
|
+
"tool_calls": [],
|
|
145
|
+
"model": None,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
def on_trace_end(self, trace: Any) -> None:
|
|
149
|
+
trace_id = getattr(trace, "trace_id", None) or str(id(trace))
|
|
150
|
+
state = self._spans.pop(trace_id, None)
|
|
151
|
+
if state is None:
|
|
152
|
+
return
|
|
153
|
+
latency_ms = int((time.time() - state["start"]) * 1000)
|
|
154
|
+
self._tracer._send(
|
|
155
|
+
name=state["name"],
|
|
156
|
+
input=state.get("input"),
|
|
157
|
+
output=state.get("output"),
|
|
158
|
+
latency_ms=latency_ms,
|
|
159
|
+
framework="openai-agents",
|
|
160
|
+
model=state.get("model"),
|
|
161
|
+
tool_calls=state["tool_calls"] or None,
|
|
162
|
+
metadata=self._metadata,
|
|
163
|
+
session_id=self._session_id,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def on_span_start(self, span: Any) -> None:
|
|
167
|
+
pass # All data is captured in on_span_end when fields are fully populated
|
|
168
|
+
|
|
169
|
+
def on_span_end(self, span: Any) -> None:
|
|
170
|
+
span_data = getattr(span, "span_data", None)
|
|
171
|
+
if span_data is None:
|
|
172
|
+
return
|
|
173
|
+
|
|
174
|
+
trace_id = getattr(span, "trace_id", None)
|
|
175
|
+
if not trace_id or trace_id not in self._spans:
|
|
176
|
+
return
|
|
177
|
+
|
|
178
|
+
state = self._spans[trace_id]
|
|
179
|
+
span_type = getattr(span_data, "type", None)
|
|
180
|
+
|
|
181
|
+
if span_type == "generation":
|
|
182
|
+
# Capture input from the first generation span
|
|
183
|
+
if state["input"] is None and span_data.input:
|
|
184
|
+
state["input"] = _extract_input_text(span_data.input)
|
|
185
|
+
# Always update output to the latest generation (last one wins = final reply)
|
|
186
|
+
if span_data.output:
|
|
187
|
+
state["output"] = _extract_text(span_data.output)
|
|
188
|
+
# Capture model name
|
|
189
|
+
if not state["model"] and span_data.model:
|
|
190
|
+
state["model"] = str(span_data.model)
|
|
191
|
+
|
|
192
|
+
elif span_type == "response":
|
|
193
|
+
# Responses API path — extract from the response object
|
|
194
|
+
response = getattr(span_data, "response", None)
|
|
195
|
+
if response is not None:
|
|
196
|
+
if state["input"] is None:
|
|
197
|
+
raw_input = getattr(span_data, "input", None)
|
|
198
|
+
if raw_input:
|
|
199
|
+
state["input"] = _extract_input_text(raw_input) if isinstance(raw_input, list) else str(raw_input)
|
|
200
|
+
output_items = getattr(response, "output", None)
|
|
201
|
+
if output_items:
|
|
202
|
+
state["output"] = _extract_text(output_items)
|
|
203
|
+
if not state["model"]:
|
|
204
|
+
model = getattr(response, "model", None)
|
|
205
|
+
if model:
|
|
206
|
+
state["model"] = str(model)
|
|
207
|
+
|
|
208
|
+
elif span_type == "function":
|
|
209
|
+
# Tool / function call
|
|
210
|
+
tool_entry: Dict[str, Any] = {
|
|
211
|
+
"name": span_data.name,
|
|
212
|
+
"input": span_data.input,
|
|
213
|
+
"output": str(span_data.output)[:500] if span_data.output is not None else None,
|
|
214
|
+
}
|
|
215
|
+
latency = _span_latency_ms(span)
|
|
216
|
+
if latency is not None:
|
|
217
|
+
tool_entry["latency_ms"] = latency
|
|
218
|
+
state["tool_calls"].append(tool_entry)
|
|
219
|
+
|
|
220
|
+
def force_flush(self) -> None:
|
|
221
|
+
self._tracer.flush()
|
|
222
|
+
|
|
223
|
+
def shutdown(self) -> None:
|
|
224
|
+
self._tracer.flush()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
VERSION = "0.4.14"
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
OpenAI Agents SDK integration for AgentX production tracing.
|
|
3
|
-
|
|
4
|
-
Usage::
|
|
5
|
-
|
|
6
|
-
from agentx.integrations.openai_agents import AgentXTracingProcessor
|
|
7
|
-
|
|
8
|
-
processor = AgentXTracingProcessor(agentx.tracer)
|
|
9
|
-
|
|
10
|
-
# Register once at startup — affects all agent runs in the process
|
|
11
|
-
from agents import add_trace_processor
|
|
12
|
-
add_trace_processor(processor)
|
|
13
|
-
|
|
14
|
-
Requires: ``pip install agentx[openai-agents]``
|
|
15
|
-
"""
|
|
16
|
-
from __future__ import annotations
|
|
17
|
-
|
|
18
|
-
import time
|
|
19
|
-
from typing import Any, Dict, Optional
|
|
20
|
-
|
|
21
|
-
from agentx.tracing.tracer import Tracer, _safe_serialize
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
class AgentXTracingProcessor:
|
|
25
|
-
"""
|
|
26
|
-
Implements the ``TracingProcessor`` interface expected by the OpenAI Agents SDK
|
|
27
|
-
(``agents.add_trace_processor``).
|
|
28
|
-
|
|
29
|
-
Sends one AgentX trace per top-level agent run span.
|
|
30
|
-
"""
|
|
31
|
-
|
|
32
|
-
def __init__(
|
|
33
|
-
self,
|
|
34
|
-
tracer: Tracer,
|
|
35
|
-
metadata: Optional[Dict[str, Any]] = None,
|
|
36
|
-
session_id: Optional[str] = None,
|
|
37
|
-
) -> None:
|
|
38
|
-
self._tracer = tracer
|
|
39
|
-
self._metadata = metadata
|
|
40
|
-
self._session_id = session_id
|
|
41
|
-
# span_id → state for in-flight agent spans
|
|
42
|
-
self._spans: Dict[str, Dict[str, Any]] = {}
|
|
43
|
-
|
|
44
|
-
# ------------------------------------------------------------------
|
|
45
|
-
# TracingProcessor protocol
|
|
46
|
-
# ------------------------------------------------------------------
|
|
47
|
-
|
|
48
|
-
def on_trace_start(self, trace: Any) -> None:
|
|
49
|
-
"""Called when a new trace (top-level run) begins."""
|
|
50
|
-
trace_id = getattr(trace, "trace_id", None) or str(id(trace))
|
|
51
|
-
self._spans[trace_id] = {
|
|
52
|
-
"start": time.time(),
|
|
53
|
-
"name": getattr(trace, "name", "openai-agent"),
|
|
54
|
-
"input": None,
|
|
55
|
-
"tool_calls": [],
|
|
56
|
-
"model": None,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
def on_trace_end(self, trace: Any) -> None:
|
|
60
|
-
"""Called when the trace ends (agent run complete)."""
|
|
61
|
-
trace_id = getattr(trace, "trace_id", None) or str(id(trace))
|
|
62
|
-
state = self._spans.pop(trace_id, None)
|
|
63
|
-
if state is None:
|
|
64
|
-
return
|
|
65
|
-
latency_ms = int((time.time() - state["start"]) * 1000)
|
|
66
|
-
self._tracer._send(
|
|
67
|
-
name=state["name"],
|
|
68
|
-
input=state.get("input"),
|
|
69
|
-
output=_safe_serialize(getattr(trace, "output", None)),
|
|
70
|
-
latency_ms=latency_ms,
|
|
71
|
-
framework="openai-agents",
|
|
72
|
-
model=state.get("model"),
|
|
73
|
-
tool_calls=state["tool_calls"] or None,
|
|
74
|
-
metadata=self._metadata,
|
|
75
|
-
session_id=self._session_id,
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
def on_span_start(self, span: Any) -> None:
|
|
79
|
-
"""Called for each nested span (LLM call, tool call, etc.)."""
|
|
80
|
-
span_type = getattr(span, "span_data", None)
|
|
81
|
-
if span_type is None:
|
|
82
|
-
return
|
|
83
|
-
|
|
84
|
-
# Capture model from LLM generation spans
|
|
85
|
-
if hasattr(span_type, "model"):
|
|
86
|
-
trace_id = getattr(span, "trace_id", None)
|
|
87
|
-
if trace_id and trace_id in self._spans and not self._spans[trace_id].get("model"):
|
|
88
|
-
self._spans[trace_id]["model"] = str(span_type.model)
|
|
89
|
-
|
|
90
|
-
# Capture input on the root span
|
|
91
|
-
if hasattr(span_type, "input") and span_type.input:
|
|
92
|
-
trace_id = getattr(span, "trace_id", None)
|
|
93
|
-
if trace_id and trace_id in self._spans and not self._spans[trace_id].get("input"):
|
|
94
|
-
self._spans[trace_id]["input"] = _safe_serialize(span_type.input)
|
|
95
|
-
|
|
96
|
-
def on_span_end(self, span: Any) -> None:
|
|
97
|
-
"""Called when a nested span ends; captures tool-call results."""
|
|
98
|
-
span_data = getattr(span, "span_data", None)
|
|
99
|
-
if span_data is None:
|
|
100
|
-
return
|
|
101
|
-
|
|
102
|
-
# Tool / function calls
|
|
103
|
-
if hasattr(span_data, "tool_name") or hasattr(span_data, "name"):
|
|
104
|
-
trace_id = getattr(span, "trace_id", None)
|
|
105
|
-
if trace_id and trace_id in self._spans:
|
|
106
|
-
tool_entry: Dict[str, Any] = {
|
|
107
|
-
"name": getattr(span_data, "tool_name", None)
|
|
108
|
-
or getattr(span_data, "name", "tool"),
|
|
109
|
-
"input": _safe_serialize(getattr(span_data, "input", None)),
|
|
110
|
-
"output": str(getattr(span_data, "output", ""))[:500],
|
|
111
|
-
}
|
|
112
|
-
if hasattr(span, "started_at") and hasattr(span, "ended_at"):
|
|
113
|
-
try:
|
|
114
|
-
tool_entry["latency_ms"] = int(
|
|
115
|
-
(span.ended_at - span.started_at) * 1000
|
|
116
|
-
)
|
|
117
|
-
except Exception:
|
|
118
|
-
pass
|
|
119
|
-
self._spans[trace_id]["tool_calls"].append(tool_entry)
|
|
120
|
-
|
|
121
|
-
def force_flush(self) -> None:
|
|
122
|
-
self._tracer.flush()
|
|
123
|
-
|
|
124
|
-
def shutdown(self) -> None:
|
|
125
|
-
self._tracer.flush()
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
VERSION = "0.4.13"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|