agentx-python 0.4.12__py3-none-any.whl → 0.4.14__py3-none-any.whl

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/agentx.py CHANGED
@@ -10,7 +10,7 @@ from agentx.resources.workforce import Workforce
10
10
 
11
11
  class AgentX:
12
12
 
13
- def __init__(self, api_key: str = None, base_url: str = None):
13
+ def __init__(self, api_key: str = None, base_url: str = None, workspace_id: str = None):
14
14
  self.api_key = api_key or os.getenv("AGENTX_API_KEY")
15
15
  if self.api_key and not os.getenv("AGENTX_API_KEY"):
16
16
  os.environ["AGENTX_API_KEY"] = self.api_key
@@ -20,6 +20,8 @@ class AgentX:
20
20
  if self.base_url:
21
21
  os.environ["AGENTX_API_BASE_URL"] = self.base_url
22
22
 
23
+ self.workspace_id = workspace_id or os.getenv("AGENTX_WORKSPACE_ID")
24
+
23
25
  from agentx.evaluations.client import EvaluationsClient
24
26
  from agentx.evaluations.runner import EvaluationsRunner
25
27
  from agentx.tracing.ingest_client import IngestClient
@@ -37,6 +39,7 @@ class AgentX:
37
39
  api_key=self.api_key,
38
40
  sdk_version=VERSION,
39
41
  base_url=self.base_url,
42
+ workspace_id=self.workspace_id,
40
43
  )
41
44
  self.tracer = Tracer(_ingest_client)
42
45
 
@@ -11,22 +11,111 @@ Usage::
11
11
  from agents import add_trace_processor
12
12
  add_trace_processor(processor)
13
13
 
14
- Requires: ``pip install agentx[openai-agents]``
14
+ Requires: ``pip install "agentx-python[openai-agents]"``
15
15
  """
16
16
  from __future__ import annotations
17
17
 
18
18
  import time
19
- from typing import Any, Dict, Optional
19
+ from datetime import datetime, timezone
20
+ from typing import Any, Dict, List, Optional
20
21
 
21
22
  from agentx.tracing.tracer import Tracer, _safe_serialize
22
23
 
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
+
24
113
  class AgentXTracingProcessor:
25
114
  """
26
115
  Implements the ``TracingProcessor`` interface expected by the OpenAI Agents SDK
27
116
  (``agents.add_trace_processor``).
28
117
 
29
- Sends one AgentX trace per top-level agent run span.
118
+ Sends one AgentX trace per top-level agent run.
30
119
  """
31
120
 
32
121
  def __init__(
@@ -38,7 +127,7 @@ class AgentXTracingProcessor:
38
127
  self._tracer = tracer
39
128
  self._metadata = metadata
40
129
  self._session_id = session_id
41
- # span_id → state for in-flight agent spans
130
+ # trace_idaccumulated state
42
131
  self._spans: Dict[str, Dict[str, Any]] = {}
43
132
 
44
133
  # ------------------------------------------------------------------
@@ -46,18 +135,17 @@ class AgentXTracingProcessor:
46
135
  # ------------------------------------------------------------------
47
136
 
48
137
  def on_trace_start(self, trace: Any) -> None:
49
- """Called when a new trace (top-level run) begins."""
50
138
  trace_id = getattr(trace, "trace_id", None) or str(id(trace))
51
139
  self._spans[trace_id] = {
52
140
  "start": time.time(),
53
141
  "name": getattr(trace, "name", "openai-agent"),
54
142
  "input": None,
143
+ "output": None,
55
144
  "tool_calls": [],
56
145
  "model": None,
57
146
  }
58
147
 
59
148
  def on_trace_end(self, trace: Any) -> None:
60
- """Called when the trace ends (agent run complete)."""
61
149
  trace_id = getattr(trace, "trace_id", None) or str(id(trace))
62
150
  state = self._spans.pop(trace_id, None)
63
151
  if state is None:
@@ -66,7 +154,7 @@ class AgentXTracingProcessor:
66
154
  self._tracer._send(
67
155
  name=state["name"],
68
156
  input=state.get("input"),
69
- output=_safe_serialize(getattr(trace, "output", None)),
157
+ output=state.get("output"),
70
158
  latency_ms=latency_ms,
71
159
  framework="openai-agents",
72
160
  model=state.get("model"),
@@ -76,47 +164,58 @@ class AgentXTracingProcessor:
76
164
  )
77
165
 
78
166
  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)
167
+ pass # All data is captured in on_span_end when fields are fully populated
95
168
 
96
169
  def on_span_end(self, span: Any) -> None:
97
- """Called when a nested span ends; captures tool-call results."""
98
170
  span_data = getattr(span, "span_data", None)
99
171
  if span_data is None:
100
172
  return
101
173
 
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)
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)
120
219
 
121
220
  def force_flush(self) -> None:
122
221
  self._tracer.flush()
@@ -41,10 +41,13 @@ class IngestClient:
41
41
  api_key: str,
42
42
  sdk_version: str = "unknown",
43
43
  base_url: Optional[str] = None,
44
+ workspace_id: Optional[str] = None,
44
45
  ) -> None:
45
46
  if not api_key:
46
47
  raise ValueError("AGENTX_API_KEY is required")
47
48
 
49
+ self._workspace_id = workspace_id or os.getenv("AGENTX_WORKSPACE_ID")
50
+
48
51
  self._api_key = api_key
49
52
  self._sdk_version = sdk_version
50
53
 
@@ -76,6 +79,8 @@ class IngestClient:
76
79
 
77
80
  def enqueue(self, payload: Dict[str, Any]) -> None:
78
81
  """Add a trace payload to the send queue. Never blocks; drops silently on overflow."""
82
+ if self._workspace_id:
83
+ payload = {**payload, "workspaceId": self._workspace_id}
79
84
  try:
80
85
  self._queue.put_nowait(payload)
81
86
  except queue.Full:
@@ -105,6 +110,8 @@ class IngestClient:
105
110
  payload: Dict[str, Any] = {"datasetId": dataset_id}
106
111
  if question_index is not None:
107
112
  payload["question_index"] = question_index
113
+ if self._workspace_id:
114
+ payload["workspaceId"] = self._workspace_id
108
115
 
109
116
  resp = self._session.post(url, json=payload, timeout=60)
110
117
  resp.raise_for_status()
@@ -131,8 +138,9 @@ class IngestClient:
131
138
  payload["pass_rate_threshold"] = pass_rate_threshold
132
139
  if git_context:
133
140
  payload["git_context"] = git_context
134
- if workspace_id:
135
- payload["workspaceId"] = workspace_id
141
+ resolved_workspace = workspace_id or self._workspace_id
142
+ if resolved_workspace:
143
+ payload["workspaceId"] = resolved_workspace
136
144
 
137
145
  url = f"{self._base_url}/ingest/ci-runs"
138
146
  resp = self._session.post(url, json=payload, timeout=30)
@@ -212,8 +220,9 @@ class IngestClient:
212
220
  ) -> Dict[str, Any]:
213
221
  """Fetch test case queries for a dataset without creating a CI run."""
214
222
  params: Dict[str, str] = {}
215
- if workspace_id:
216
- params["workspaceId"] = workspace_id
223
+ resolved_workspace = workspace_id or self._workspace_id
224
+ if resolved_workspace:
225
+ params["workspaceId"] = resolved_workspace
217
226
  url = f"{self._base_url}/ingest/datasets/{dataset_id}/test-cases"
218
227
  resp = self._session.get(url, params=params, timeout=15)
219
228
  self._raise_for_ci_status(resp)
agentx/version.py CHANGED
@@ -1 +1 @@
1
- VERSION = "0.4.12"
1
+ VERSION = "0.4.14"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agentx-python
3
- Version: 0.4.12
3
+ Version: 0.4.14
4
4
  Summary: Official Python SDK for AgentX (https://www.agentx.so/)
5
5
  Home-page: https://github.com/AgentX-ai/AgentX-python
6
6
  Author: Robin Wang and AgentX Team
@@ -14,8 +14,7 @@ License-File: LICENSE
14
14
  Requires-Dist: urllib3>=1.26.11
15
15
  Requires-Dist: certifi
16
16
  Requires-Dist: requests
17
- Requires-Dist: pydantic
18
- Requires-Dist: pydantic_core
17
+ Requires-Dist: pydantic>=2.0.0
19
18
  Provides-Extra: langchain
20
19
  Requires-Dist: langchain-core>=0.1.0; extra == "langchain"
21
20
  Provides-Extra: crewai
@@ -1,8 +1,8 @@
1
1
  agentx/__init__.py,sha256=4iMiGLNU4S-I-WyX0Z7l0BFUY_krVJ3seMQPqyO-ReA,602
2
- agentx/agentx.py,sha256=rWfeFBS4wV7b9HzJlkO651pgPWo58tZFmPDgPfq6eQo,3321
2
+ agentx/agentx.py,sha256=tw3CUnkvTM1eopoO7u8TgKnAz9t0HNSxrVcjIJsytAg,3469
3
3
  agentx/exceptions.py,sha256=TKooc1_pQ2P_4DqpLfZx_ap-6cfrjFLW8JS2qaGx_1M,1237
4
4
  agentx/util.py,sha256=yOV3VVdc0KUbw5pEkSRiO2lZzEtL1vOsnB_f5tFk948,665
5
- agentx/version.py,sha256=M4jHf2piUpaEzqXOe34S2F5941xUUdbYw1L0mpXlyj8,19
5
+ agentx/version.py,sha256=_pWcb0KD7RwUQ8uiQjs-gUiG1ej2mVAyGLgZjVwFiUc,19
6
6
  agentx/evaluations/__init__.py,sha256=OmdCHiBO3Qcsck0UkOyVqSvfRs6nXbSi-2FXzyRfsog,89
7
7
  agentx/evaluations/_term.py,sha256=S4blZgH66wHvkt2hR29h8JE5xnakBeD7dlZdUW9Wfoo,2065
8
8
  agentx/evaluations/client.py,sha256=3mC0uSBRk9uO4F3a7kIMM_UUsyypF99_97E5MSpYFzM,7736
@@ -21,17 +21,17 @@ agentx/integrations/__init__.py,sha256=ZKOwYsvHXo9ipJUv5rPF1DOA0H5Q5YBG0WRI1f0lv
21
21
  agentx/integrations/anthropic.py,sha256=VzRdeICYm_zfgNBy6PEu6bYSUa9iKuSoRX6HSivWQpM,4802
22
22
  agentx/integrations/crewai.py,sha256=o7tgFE1I2T7fxOc2lG7aY1nAAwihejV3y-EJII6z1j0,3488
23
23
  agentx/integrations/langchain.py,sha256=_W62CUnzqlzSgIcNim0xDGXc3WsPoWYLQSHpTD4Xp5E,7844
24
- agentx/integrations/openai_agents.py,sha256=XDN_tNfx1ztLUlwNHgFk6QSiYbUDLo-HPljrHjZoVWk,4668
24
+ agentx/integrations/openai_agents.py,sha256=JscJELxs8sTj3toi9ixY5TOpH-lCVBtpgxYgahIaezE,8162
25
25
  agentx/resources/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
26
  agentx/resources/agent.py,sha256=ZKpxDYzJbKgOX4-W86U__b4gko7XlbwB4q6L8Az9uo0,2011
27
27
  agentx/resources/conversation.py,sha256=HK7gEGK6qIUz0KvG-ItUip08otWZdkP5S6qHiznvgZM,4443
28
28
  agentx/resources/workforce.py,sha256=lfGVkoV9lcOp-lZScjTvwRarJMkhzfMfLm4syJDujGc,4168
29
29
  agentx/tracing/__init__.py,sha256=l2mIRILE-wWrCD0fekgrIOQUEvtDObzdrkgpQUtXroU,408
30
30
  agentx/tracing/ci_types.py,sha256=zFVcZGvc1qbVDdOlEPQ1i5R6terqJHxCzFF4ZjouxxI,1423
31
- agentx/tracing/ingest_client.py,sha256=p-rqzYEvFgUwRIcY54omXz4tPvw-hHzysVCCA2PSpuo,11025
31
+ agentx/tracing/ingest_client.py,sha256=8aLabvWecgZsn0pkajZkI1it2TOk5boP0kC3o6TO52o,11487
32
32
  agentx/tracing/tracer.py,sha256=nJILoeQg6HV4SR05xJPy6EiSiZCpkp4yFBTrN1BO_1w,16951
33
- agentx_python-0.4.12.dist-info/licenses/LICENSE,sha256=6ZbiPNFmv3xBb44LGhAa3PZYK0ROAztsd5LRFZDlGFE,1074
34
- agentx_python-0.4.12.dist-info/METADATA,sha256=l_nlLJPCx7NjWvDOlJXEmDlG8m0U9QxJ1eJBOkPDg1U,6652
35
- agentx_python-0.4.12.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
36
- agentx_python-0.4.12.dist-info/top_level.txt,sha256=s-q-HB9Gb_QdrZNacSeQyF_c25gQooMy7DlxzgLOHPk,7
37
- agentx_python-0.4.12.dist-info/RECORD,,
33
+ agentx_python-0.4.14.dist-info/licenses/LICENSE,sha256=6ZbiPNFmv3xBb44LGhAa3PZYK0ROAztsd5LRFZDlGFE,1074
34
+ agentx_python-0.4.14.dist-info/METADATA,sha256=b2VooejMy2en_rD9tGJhcHLdrzmuC2Tx5gjfSEKYsi8,6630
35
+ agentx_python-0.4.14.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
36
+ agentx_python-0.4.14.dist-info/top_level.txt,sha256=s-q-HB9Gb_QdrZNacSeQyF_c25gQooMy7DlxzgLOHPk,7
37
+ agentx_python-0.4.14.dist-info/RECORD,,