struct-sdk 0.4.16__tar.gz → 0.4.17__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.
Files changed (26) hide show
  1. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/PKG-INFO +1 -1
  2. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/pyproject.toml +1 -1
  3. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/_genai_content.py +7 -1
  4. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/ag2.py +3 -7
  5. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/anthropic.py +5 -17
  6. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/autogen.py +2 -5
  7. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/core.py +59 -12
  8. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/crewai.py +6 -1
  9. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/google_genai.py +2 -6
  10. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/langchain.py +8 -10
  11. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/litellm.py +2 -5
  12. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/mcp.py +17 -9
  13. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/openai.py +10 -20
  14. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/openai_agents.py +7 -1
  15. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/.gitignore +0 -0
  16. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/LICENSE +0 -0
  17. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/README.md +0 -0
  18. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/__init__.py +0 -0
  19. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/_auto.py +0 -0
  20. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/_distro.py +0 -0
  21. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/_operation_details.py +0 -0
  22. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/claude_agent.py +0 -0
  23. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/gcp.py +0 -0
  24. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/llamaindex.py +0 -0
  25. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/pydantic_ai.py +0 -0
  26. {struct_sdk-0.4.16 → struct_sdk-0.4.17}/src/struct_sdk/temporal.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: struct-sdk
3
- Version: 0.4.16
3
+ Version: 0.4.17
4
4
  Summary: Struct agent observability SDK — auto-instruments AI agent frameworks with OpenTelemetry
5
5
  Project-URL: Homepage, https://struct.ai
6
6
  Project-URL: Documentation, https://struct.ai/docs
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "struct-sdk"
7
- version = "0.4.16"
7
+ version = "0.4.17"
8
8
  description = "Struct agent observability SDK — auto-instruments AI agent frameworks with OpenTelemetry"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -35,7 +35,7 @@ from urllib.parse import urlparse
35
35
  from opentelemetry import trace
36
36
  from opentelemetry.trace import StatusCode
37
37
 
38
- from struct_sdk.core import _contain_logged
38
+ from struct_sdk.core import _contain_logged, _record_span_error, _span_error_description
39
39
 
40
40
  logger = logging.getLogger("struct_sdk._genai_content")
41
41
 
@@ -50,6 +50,12 @@ _MAX_FIELD_SIZE = 16384 # 16KB per field
50
50
  # Truncation / serialization
51
51
  # ---------------------------------------------------------------------------
52
52
 
53
+ # Shared with every integration: a failed span's error, with its text only in span-content
54
+ # modes (SDK-PY-47).
55
+ record_span_error = _record_span_error
56
+ span_error_description = _span_error_description
57
+
58
+
53
59
  def truncate_field(value: "str | Any", max_len: int = _MAX_FIELD_SIZE) -> "str | Any":
54
60
  """Truncate a single string field if it exceeds ``max_len``."""
55
61
  if isinstance(value, str) and len(value) > max_len:
@@ -75,6 +75,7 @@ from struct_sdk._genai_content import (
75
75
  guarded_client_span,
76
76
  propagate_provider_to_parent,
77
77
  propagate_user_prompt_to_parent,
78
+ record_span_error,
78
79
  safe_json,
79
80
  stop_collecting_native_usage,
80
81
  truncate_and_serialize,
@@ -311,11 +312,6 @@ def _session_id() -> Optional[str]:
311
312
  return None
312
313
 
313
314
 
314
- def _mark_error(span: Any, exc: BaseException, site: str) -> None:
315
- err_type = type(exc).__name__
316
- _contain(lambda: span.set_attribute("error.type", err_type), f"{site}.error_type")
317
- _contain(lambda: span.set_status(StatusCode.ERROR, str(exc)), f"{site}.error_status")
318
- _contain(lambda: span.record_exception(exc), f"{site}.record_exception")
319
315
 
320
316
 
321
317
  # ---------------------------------------------------------------------------
@@ -382,7 +378,7 @@ def _chat_common(f: Any, args: tuple, kwargs: dict, tracer: trace.Tracer, sdk: S
382
378
  except Exception as e:
383
379
  # The input goes on the span (and its record) before the span ends.
384
380
  _safe(lambda: _set_request_attrs(span, sdk, request, fallback), site="ag2.chat_request")
385
- _mark_error(span, e, "ag2.chat")
381
+ record_span_error(span, e, sdk, "ag2.chat")
386
382
  raise
387
383
  finally:
388
384
  if "usage" in tokens:
@@ -663,7 +659,7 @@ def _tool_common(f: Any, args: tuple, kwargs: dict, tracer: trace.Tracer, sdk: S
663
659
  result = yield f, args, kwargs
664
660
  except BaseException as e:
665
661
  if isinstance(e, Exception):
666
- _mark_error(tool_span, e, "ag2.tool")
662
+ record_span_error(tool_span, e, sdk, "ag2.tool")
667
663
  else:
668
664
  err_type = type(e).__name__
669
665
  _contain(lambda: tool_span.set_attribute("error.type", err_type), "ag2.tool_base_error")
@@ -45,6 +45,7 @@ from struct_sdk._genai_content import (
45
45
  guarded_client_span,
46
46
  propagate_provider_to_parent,
47
47
  propagate_user_prompt_to_parent,
48
+ record_span_error,
48
49
  safe_json,
49
50
  truncate_and_serialize,
50
51
  truncate_field,
@@ -285,21 +286,10 @@ def _create_common(
285
286
  # (``str(exc)``; ``record_exception`` formats it internally) — a
286
287
  # hostile ``__str__`` raising a BASE exception would escape
287
288
  # ``_safe`` (Exception-only) and REPLACE the original exception
288
- # we're re-raising. ``_contain`` is the host-call-path guard.
289
+ # we're re-raising. ``record_span_error`` contains each step.
289
290
  exc = e
290
- err_type = type(exc).__name__
291
- _contain(
292
- lambda: span.set_attribute("error.type", err_type),
293
- site="anthropic.create.error_type",
294
- )
295
- _contain(
296
- lambda: span.set_status(StatusCode.ERROR, str(exc)),
297
- site="anthropic.create.error_status",
298
- )
299
- _contain(
300
- lambda: span.record_exception(exc),
301
- site="anthropic.create.record_exception",
302
- )
291
+ # Exception text only in span-content modes (SDK-PY-47).
292
+ record_span_error(span, exc, sdk, "anthropic.create")
303
293
  raise
304
294
  if kwargs.get("stream") and span is not None:
305
295
  # The raw event stream: the response (and its usage) is still to
@@ -769,9 +759,7 @@ def _finish_stream_span_with_final(
769
759
  _set_response_attrs(span, sdk, model, final_msg, otel_logger, provider,
770
760
  pending_tool_calls=pending_tool_calls)
771
761
  if exc and exc[0] is not None:
772
- span.set_attribute("error.type", exc[0].__name__)
773
- span.set_status(StatusCode.ERROR, str(exc[1]))
774
- span.record_exception(exc[1])
762
+ record_span_error(span, exc[1], sdk, "anthropic.stream.finish")
775
763
  else:
776
764
  span.set_status(StatusCode.OK)
777
765
 
@@ -57,6 +57,7 @@ from struct_sdk._genai_content import (
57
57
  guarded_client_span,
58
58
  propagate_provider_to_parent,
59
59
  propagate_user_prompt_to_parent,
60
+ record_span_error,
60
61
  safe_json,
61
62
  truncate_and_serialize,
62
63
  )
@@ -209,11 +210,7 @@ def _create_common(f: Any, args: tuple, kwargs: dict, tracer: trace.Tracer, sdk:
209
210
  try:
210
211
  result = yield f, args, kwargs
211
212
  except Exception as e:
212
- exc = e
213
- err_type = type(exc).__name__
214
- _contain(lambda: span.set_attribute("error.type", err_type), "autogen.error_type")
215
- _contain(lambda: span.set_status(StatusCode.ERROR, str(exc)), "autogen.error_status")
216
- _contain(lambda: span.record_exception(exc), "autogen.record_exception")
213
+ record_span_error(span, e, sdk, "autogen")
217
214
  raise
218
215
  finally:
219
216
  # Released before the span's own context detaches (LIFO).
@@ -346,6 +346,63 @@ def _contain_logged(fn: Callable[[], None], *, site: str) -> None:
346
346
  pass
347
347
 
348
348
 
349
+ def _error_text_on_spans(sdk: Any) -> bool:
350
+ """Whether a failed span may carry the exception's text (SDK-PY-47): its message, stack
351
+ trace and status description. Error text can quote prompts, tool arguments or answers,
352
+ so it goes where message content goes: on spans only in the span-content modes."""
353
+ return bool(getattr(sdk, "emit_span_content", False))
354
+
355
+
356
+ def _exception_type(exc: BaseException) -> str:
357
+ """``exception.type`` as OpenTelemetry's ``record_exception`` writes it."""
358
+ module = type(exc).__module__
359
+ qualname = type(exc).__qualname__
360
+ return f"{module}.{qualname}" if module and module != "builtins" else qualname
361
+
362
+
363
+ def _exception_type_name(exc: BaseException) -> str:
364
+ """The exception's class name, or ``_OTHER`` when reading it raises (a hostile metaclass):
365
+ the host's own exception must still propagate (SDK-PY-19)."""
366
+ try:
367
+ name = type(exc).__name__
368
+ return name if isinstance(name, str) and name else "_OTHER"
369
+ except BaseException: # noqa: BLE001 — containment is the contract on the host path
370
+ return "_OTHER"
371
+
372
+
373
+ def _record_span_error(
374
+ span: Any, exc: BaseException, sdk: Any, site: str, *, error_type: Optional[str] = None
375
+ ) -> None:
376
+ """Mark ``span`` failed by ``exc`` (SDK-PY-47): ``error.type`` and status ERROR always.
377
+ In span-content modes the status description is ``str(exc)`` and ``record_exception``
378
+ adds the message and stack trace; otherwise the description is the type name and the
379
+ ``exception`` event carries only ``exception.type``. Each step is contained on its own,
380
+ BaseException included: these run on the host's error path, where a hostile ``__str__``
381
+ must not replace the host's exception (SDK-PY-19)."""
382
+ type_name = _exception_type_name(exc)
383
+ err_type = error_type or type_name
384
+ _contain_logged(lambda: span.set_attribute("error.type", err_type), site=f"{site}.error_type")
385
+ if _error_text_on_spans(sdk):
386
+ description = [type_name] # kept when ``str(exc)`` raises
387
+ _contain_logged(lambda: description.__setitem__(0, str(exc)), site=f"{site}.error_text")
388
+ _contain_logged(lambda: span.set_status(StatusCode.ERROR, description[0]), site=f"{site}.error_status")
389
+ _contain_logged(lambda: span.record_exception(exc), site=f"{site}.record_exception")
390
+ return
391
+ _contain_logged(lambda: span.set_status(StatusCode.ERROR, type_name), site=f"{site}.error_status")
392
+ _contain_logged(
393
+ lambda: span.add_event("exception", {"exception.type": _exception_type(exc)}),
394
+ site=f"{site}.record_exception",
395
+ )
396
+
397
+
398
+ def _span_error_description(sdk: Any, text: str, fallback: str) -> str:
399
+ """A status description from error text that isn't an exception (a provider's failure
400
+ message, a framework's error string): ``text`` in span-content modes, else ``fallback``
401
+ (SDK-PY-47). ``text`` is passed through as given, never stringified: a ``str`` subclass
402
+ whose ``__str__`` raises must not interrupt the host (SDK-PY-19)."""
403
+ return text if _error_text_on_spans(sdk) else fallback
404
+
405
+
349
406
  # Passed explicitly: the OTLP exporters otherwise read the host's
350
407
  # OTEL_EXPORTER_OTLP_* env, where an invalid value makes construction raise.
351
408
  _EXPORT_TIMEOUT_SECONDS = 10.0
@@ -1577,12 +1634,7 @@ class _AgentContext:
1577
1634
  span = self._span
1578
1635
  if span is not None:
1579
1636
  if exc_val:
1580
- _safe(lambda: span.set_attribute("error.type", type(exc_val).__name__),
1581
- site="agent.end_span.error_type")
1582
- _safe(lambda: span.set_status(StatusCode.ERROR, str(exc_val)),
1583
- site="agent.end_span.error_status")
1584
- _safe(lambda: span.record_exception(exc_val),
1585
- site="agent.end_span.record_exception")
1637
+ _record_span_error(span, exc_val, self._sdk, "agent.end_span")
1586
1638
  else:
1587
1639
  _safe(lambda: span.set_status(StatusCode.OK),
1588
1640
  site="agent.end_span.set_ok")
@@ -1796,12 +1848,7 @@ class _ToolContext:
1796
1848
  if exc_val and exc_val is inner:
1797
1849
  pass # the inner call already recorded this exception (its code and status)
1798
1850
  elif exc_val:
1799
- _safe(lambda: span.set_attribute("error.type", type(exc_val).__name__),
1800
- site="tool.end_span.error_type")
1801
- _safe(lambda: span.set_status(StatusCode.ERROR, str(exc_val)),
1802
- site="tool.end_span.error_status")
1803
- _safe(lambda: span.record_exception(exc_val),
1804
- site="tool.end_span.record_exception")
1851
+ _record_span_error(span, exc_val, self._sdk, "tool.end_span")
1805
1852
  elif self._result_is_error:
1806
1853
  _safe(lambda: span.set_attribute("error.type", "tool_error"),
1807
1854
  site="tool.end_span.tool_error_type")
@@ -78,6 +78,7 @@ from struct_sdk._genai_content import (
78
78
  propagate_provider_to_parent,
79
79
  propagate_user_prompt_to_parent,
80
80
  safe_json,
81
+ span_error_description,
81
82
  stamp_provider_once,
82
83
  truncate_and_serialize,
83
84
  )
@@ -561,7 +562,11 @@ def _end(started: str, event: Any, failed: bool) -> None:
561
562
  if failed:
562
563
  _contain(lambda: span.set_attribute("error.type", "_OTHER"), "crewai.error_type")
563
564
  message = getattr(event, "error", None)
564
- status = str(message)[:256] if isinstance(message, str) else "error"
565
+ # CrewAI's error text can quote the prompt or tool input: span text only in
566
+ # span-content modes (SDK-PY-47).
567
+ status = (
568
+ span_error_description(_sdk, message[:256], "error") if isinstance(message, str) else "error"
569
+ )
565
570
  _contain(lambda: span.set_status(StatusCode.ERROR, status), "crewai.error_status")
566
571
  else:
567
572
  _safe(lambda: _finish(record, event), site="crewai.finish")
@@ -59,12 +59,12 @@ from opentelemetry import trace
59
59
  from opentelemetry.trace import StatusCode
60
60
 
61
61
  from struct_sdk._genai_content import (
62
- _contain,
63
62
  drive_instrumented_call,
64
63
  drive_instrumented_call_async,
65
64
  guarded_client_span,
66
65
  propagate_provider_to_parent,
67
66
  propagate_user_prompt_to_parent,
67
+ record_span_error,
68
68
  safe_json,
69
69
  truncate_and_serialize,
70
70
  )
@@ -205,11 +205,7 @@ def _generate_common(f: Any, args: tuple, kwargs: dict, tracer: trace.Tracer, sd
205
205
  try:
206
206
  result = yield f, args, kwargs
207
207
  except Exception as e:
208
- exc = e
209
- err_type = type(exc).__name__
210
- _contain(lambda: span.set_attribute("error.type", err_type), "google_genai.error_type")
211
- _contain(lambda: span.set_status(StatusCode.ERROR, str(exc)), "google_genai.error_status")
212
- _contain(lambda: span.record_exception(exc), "google_genai.record_exception")
208
+ record_span_error(span, e, sdk, "google_genai")
213
209
  raise
214
210
  _safe(
215
211
  lambda: _set_response_attrs(span, sdk, result),
@@ -38,7 +38,7 @@ from opentelemetry import trace
38
38
  from opentelemetry.trace import StatusCode
39
39
 
40
40
  from struct_sdk._operation_details import record_content
41
- from struct_sdk._genai_content import stamp_provider_once, truncate_and_serialize
41
+ from struct_sdk._genai_content import record_span_error, stamp_provider_once, truncate_and_serialize
42
42
 
43
43
  if TYPE_CHECKING:
44
44
  from struct_sdk.core import StructSDK
@@ -929,7 +929,7 @@ class StructCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
929
929
  return
930
930
  span = r.span
931
931
  if not _is_control_flow(error):
932
- _safe(lambda: _record_error(span, error),
932
+ _safe(lambda: _record_error(span, error, self._sdk),
933
933
  site="langchain.on_chain_error.record_error")
934
934
  _safe(span.end, site="langchain.on_chain_error.span_end")
935
935
 
@@ -1169,7 +1169,7 @@ class StructCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
1169
1169
  if not r.span:
1170
1170
  return
1171
1171
  span = r.span
1172
- _safe(lambda: _record_error(span, error),
1172
+ _safe(lambda: _record_error(span, error, self._sdk),
1173
1173
  site="langchain.on_llm_error.record_error")
1174
1174
  _safe(span.end, site="langchain.on_llm_error.span_end")
1175
1175
 
@@ -1334,7 +1334,7 @@ class StructCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
1334
1334
  # An interrupt() or a Command to a parent graph is LangGraph control
1335
1335
  # flow (human-in-the-loop pause, handoff), not a tool failure.
1336
1336
  if not _is_control_flow(error):
1337
- _safe(lambda: _record_error(span, error),
1337
+ _safe(lambda: _record_error(span, error, self._sdk),
1338
1338
  site="langchain.on_tool_error.record_error")
1339
1339
  _safe(span.end, site="langchain.on_tool_error.span_end")
1340
1340
 
@@ -1370,7 +1370,7 @@ class StructCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
1370
1370
  if span is None:
1371
1371
  return
1372
1372
  if error is not None and not _is_control_flow(error):
1373
- _safe(lambda: _record_error(span, error),
1373
+ _safe(lambda: _record_error(span, error, self._sdk),
1374
1374
  site="langchain.close_unreported_tool.record_error")
1375
1375
  _safe(span.end, site="langchain.close_unreported_tool.span_end")
1376
1376
 
@@ -1486,7 +1486,7 @@ class StructCallbackHandler(BaseCallbackHandler): # type: ignore[misc]
1486
1486
  if not r or not r.span:
1487
1487
  return
1488
1488
  span = r.span
1489
- _safe(lambda: _record_error(span, error),
1489
+ _safe(lambda: _record_error(span, error, self._sdk),
1490
1490
  site="langchain.on_retriever_error.record_error")
1491
1491
  _safe(span.end, site="langchain.on_retriever_error.span_end")
1492
1492
 
@@ -1717,10 +1717,8 @@ def _release_current(pair: Any) -> None:
1717
1717
  _safe(lambda: _detach_current_span(token, enter_task), site="langchain.release_current")
1718
1718
 
1719
1719
 
1720
- def _record_error(span: trace.Span, err: BaseException) -> None:
1721
- span.set_attribute("error.type", type(err).__name__)
1722
- span.set_status(StatusCode.ERROR, str(err))
1723
- span.record_exception(err)
1720
+ def _record_error(span: trace.Span, err: BaseException, sdk: Any) -> None:
1721
+ record_span_error(span, err, sdk, "langchain.error")
1724
1722
 
1725
1723
 
1726
1724
  def _tool_output_signals_error(output: Any) -> bool:
@@ -79,6 +79,7 @@ from struct_sdk._genai_content import (
79
79
  guarded_client_span,
80
80
  propagate_provider_to_parent,
81
81
  propagate_user_prompt_to_parent,
82
+ record_span_error,
82
83
  safe_json,
83
84
  truncate_and_serialize,
84
85
  )
@@ -299,11 +300,7 @@ def _completion_common(
299
300
  try:
300
301
  result = yield f, args, kwargs
301
302
  except Exception as e:
302
- exc = e
303
- err_type = type(exc).__name__
304
- _contain(lambda: span.set_attribute("error.type", err_type), "litellm.error_type")
305
- _contain(lambda: span.set_status(StatusCode.ERROR, str(exc)), "litellm.error_status")
306
- _contain(lambda: span.record_exception(exc), "litellm.record_exception")
303
+ record_span_error(span, e, sdk, "litellm")
307
304
  raise
308
305
  finally:
309
306
  # Released before the span's own context detaches (LIFO).
@@ -45,7 +45,13 @@ from typing import TYPE_CHECKING, Any, Generator, Optional
45
45
  from opentelemetry import propagate, trace
46
46
  from opentelemetry.trace import StatusCode
47
47
 
48
- from struct_sdk._genai_content import _contain, drive_instrumented_call_async, guarded_client_span
48
+ from struct_sdk._genai_content import (
49
+ _contain,
50
+ drive_instrumented_call_async,
51
+ guarded_client_span,
52
+ record_span_error,
53
+ span_error_description,
54
+ )
49
55
 
50
56
  if TYPE_CHECKING:
51
57
  from struct_sdk.core import StructSDK
@@ -202,7 +208,7 @@ def _call_tool_telemetry(
202
208
  exc = e # ``except ... as e`` unbinds ``e`` before the closure runs
203
209
 
204
210
  def record_failure() -> None:
205
- _record_error(outer, exc)
211
+ _record_error(outer, exc, sdk)
206
212
  _record_inner_outcome(outer, exc)
207
213
 
208
214
  # On the host's error path, and stringifying a hostile exception
@@ -230,7 +236,7 @@ def _call_tool_telemetry(
230
236
  # These run on the host's error path, and stringifying a hostile
231
237
  # exception can raise anything: ``_contain``, not ``_safe``.
232
238
  exc = e
233
- _contain(lambda: _record_error(span, exc), site="mcp.call_tool.error")
239
+ _contain(lambda: _record_error(span, exc, sdk), site="mcp.call_tool.error")
234
240
  raise
235
241
  _safe(lambda: _record_result(span, sdk, result), site="mcp.call_tool.result")
236
242
  return result # noqa: B901
@@ -352,18 +358,20 @@ def _record_tool_error(span: trace.Span, sdk: StructSDK, result: Any) -> bool:
352
358
  return True
353
359
 
354
360
 
355
- def _record_error(span: trace.Span, exc: BaseException) -> None:
361
+ def _record_error(span: trace.Span, exc: BaseException, sdk: Any) -> None:
362
+ """A failed call: a JSON-RPC error's code as ``error.type``, else the exception's type.
363
+ Its text (the server's error message, the exception's message and stack trace) reaches
364
+ the span only in span-content modes (SDK-PY-47): it can quote the tool's arguments."""
356
365
  error = getattr(exc, "error", None)
357
366
  code = getattr(error, "code", None)
358
367
  if isinstance(code, int) and not isinstance(code, bool):
359
- span.set_attribute("error.type", str(code))
360
368
  span.set_attribute("rpc.response.status_code", str(code))
361
369
  message = getattr(error, "message", None)
362
- span.set_status(StatusCode.ERROR, message if isinstance(message, str) else str(exc))
370
+ record_span_error(span, exc, sdk, "mcp.error", error_type=str(code))
371
+ if isinstance(message, str):
372
+ span.set_status(StatusCode.ERROR, span_error_description(sdk, message, str(code)))
363
373
  else:
364
- span.set_attribute("error.type", type(exc).__name__)
365
- span.set_status(StatusCode.ERROR, str(exc))
366
- span.record_exception(exc)
374
+ record_span_error(span, exc, sdk, "mcp.error")
367
375
 
368
376
 
369
377
  def _serialize(result: Any) -> str:
@@ -116,7 +116,9 @@ from struct_sdk._genai_content import (
116
116
  guarded_client_span,
117
117
  propagate_provider_to_parent,
118
118
  propagate_user_prompt_to_parent,
119
+ record_span_error,
119
120
  safe_json,
121
+ span_error_description,
120
122
  truncate_and_serialize,
121
123
  )
122
124
 
@@ -324,22 +326,11 @@ def _create_common(
324
326
  # exception (``str(exc)``; ``record_exception`` formats it
325
327
  # internally) — a hostile ``__str__`` raising a BASE exception
326
328
  # would escape ``_safe`` (Exception-only) and REPLACE the original
327
- # exception we're re-raising. ``_contain`` is the host-call-path
328
- # guard.
329
+ # exception we're re-raising. ``record_span_error`` contains each
330
+ # step, and keeps the text off spans outside span-content modes
331
+ # (SDK-PY-47).
329
332
  exc = e
330
- err_type = type(exc).__name__
331
- _contain(
332
- lambda: span.set_attribute("error.type", err_type),
333
- site=f"{api.site}.error_type",
334
- )
335
- _contain(
336
- lambda: span.set_status(StatusCode.ERROR, str(exc)),
337
- site=f"{api.site}.error_status",
338
- )
339
- _contain(
340
- lambda: span.record_exception(exc),
341
- site=f"{api.site}.record_exception",
342
- )
333
+ record_span_error(span, exc, sdk, api.site)
343
334
  raise
344
335
  if not streaming:
345
336
  _safe(lambda: api.set_response_attrs(span, sdk, result, otel_logger, provider),
@@ -350,7 +341,7 @@ def _create_common(
350
341
  if failure is not None:
351
342
  err_type, err_msg = failure
352
343
  span.set_attribute("error.type", err_type)
353
- span.set_status(StatusCode.ERROR, err_msg)
344
+ span.set_status(StatusCode.ERROR, span_error_description(sdk, err_msg, err_type))
354
345
  else:
355
346
  span.set_status(StatusCode.OK)
356
347
 
@@ -662,15 +653,14 @@ class _StreamRecorder:
662
653
  if failure is not None:
663
654
  err_type, err_msg = failure
664
655
  span.set_attribute("error.type", err_type)
665
- span.set_status(StatusCode.ERROR, err_msg)
656
+ span.set_status(StatusCode.ERROR, span_error_description(self.sdk, err_msg, err_type))
666
657
  return
667
658
  span.set_status(StatusCode.OK)
668
659
  return
669
- span.set_attribute("error.type", type(exc).__name__)
670
660
  if isinstance(exc, Exception):
671
- span.set_status(StatusCode.ERROR, str(exc))
672
- span.record_exception(exc)
661
+ record_span_error(span, exc, self.sdk, "openai.stream.status")
673
662
  else:
663
+ span.set_attribute("error.type", type(exc).__name__)
674
664
  # CancelledError / KeyboardInterrupt: type name only.
675
665
  span.set_status(StatusCode.ERROR, type(exc).__name__)
676
666
 
@@ -73,6 +73,7 @@ from opentelemetry.trace import StatusCode
73
73
 
74
74
  from struct_sdk._genai_content import (
75
75
  safe_json,
76
+ span_error_description,
76
77
  stamp_provider_once,
77
78
  truncate_and_serialize,
78
79
  )
@@ -435,7 +436,12 @@ class StructTracingProcessor:
435
436
  error = getattr(agents_span, "error", None)
436
437
  if error:
437
438
  message = _string(error.get("message") if isinstance(error, dict) else None) or "error"
438
- _safe(lambda: _mark_error(span, message), site="openai_agents.span_end.error")
439
+ # The Agents SDK's error message can quote tool input: span text only in
440
+ # span-content modes (SDK-PY-47).
441
+ _safe(
442
+ lambda: _mark_error(span, span_error_description(self._sdk, message, "error")),
443
+ site="openai_agents.span_end.error",
444
+ )
439
445
  if entry.kind == "agent" and entry.nests_under_root and run is not None:
440
446
  run.failed = True
441
447
  elif tool_error:
File without changes
File without changes
File without changes