telemetry-dev-openai 0.2.5__tar.gz → 0.2.7__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.
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/PKG-INFO +2 -2
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/README.md +1 -1
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/pyproject.toml +1 -1
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/pyproject.toml.orig +1 -1
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/src/telemetry_dev_openai/__init__.py +81 -30
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/LICENSE +0 -0
- {telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/src/telemetry_dev_openai/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: telemetry-dev-openai
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.7
|
|
4
4
|
Summary: OpenAI integration for telemetry.dev Python SDK
|
|
5
5
|
Keywords: telemetry,opentelemetry,openai,llm,genai,tracing
|
|
6
6
|
Author: telemetry.dev
|
|
@@ -103,7 +103,7 @@ Chat completion streams are traced. Requests are sent unchanged by default, so t
|
|
|
103
103
|
|
|
104
104
|
Streamed chat output is kept up to 16 MiB, and all streams in flight share the `max_stream_bytes` bound of the core (64 MiB by default). These bounds need a `telemetry-dev` core with `max_stream_bytes`; with an older core, each stream keeps up to 64 KiB and 1,024 items. Each streamed finish reason is cut to its first 128 characters, without setting `telemetry.dev.capture.truncated`, and the finish reasons of a stream have their own budget, which fits at least 1,000 of them; the configured `max_attribute_length` does not shrink these retention bounds; the core applies it to the exported attribute after the mask runs, with its `...[truncated]` marker, without setting `telemetry.dev.capture.truncated`. A chat stream inspects at most 1,000 choices and 1,000 tool calls in each chunk; past these caps the rest is not kept and `telemetry.dev.capture.truncated` is set. When present, `gen_ai.output.messages` contains bounded partial output. If output and/or finish-reason capture is incomplete, including when a choice ends without `finish_reason`, `telemetry.dev.capture.truncated` is `true`. With a mask configured, incomplete output is omitted because the mask cannot inspect the complete value. When output capture is disabled or omitted, response IDs and models, finish reasons, usage, timing, and errors may still be recorded.
|
|
105
105
|
|
|
106
|
-
Capture flags do not gate stop sequences or caller-supplied metadata or raw attributes; redact those separately when needed. Exception messages and stack traces are recorded only when both `capture_input` and `capture_output` are on, and pass through `mask`.
|
|
106
|
+
Capture flags do not gate stop sequences or caller-supplied metadata or raw attributes; redact those separately when needed. Exception messages and stack traces are recorded only when both `capture_input` and `capture_output` are on, and pass through `mask`. Model spans list the request's tools in `gen_ai.tool.definitions`: each tool's type and name, and with `capture_input` on the description and parameters (a custom tool's input format) of each function and custom tool, which pass through `mask`.
|
|
107
107
|
|
|
108
108
|
```py
|
|
109
109
|
client = wrap_openai(OpenAI(), inject_stream_usage=True)
|
|
@@ -80,7 +80,7 @@ Chat completion streams are traced. Requests are sent unchanged by default, so t
|
|
|
80
80
|
|
|
81
81
|
Streamed chat output is kept up to 16 MiB, and all streams in flight share the `max_stream_bytes` bound of the core (64 MiB by default). These bounds need a `telemetry-dev` core with `max_stream_bytes`; with an older core, each stream keeps up to 64 KiB and 1,024 items. Each streamed finish reason is cut to its first 128 characters, without setting `telemetry.dev.capture.truncated`, and the finish reasons of a stream have their own budget, which fits at least 1,000 of them; the configured `max_attribute_length` does not shrink these retention bounds; the core applies it to the exported attribute after the mask runs, with its `...[truncated]` marker, without setting `telemetry.dev.capture.truncated`. A chat stream inspects at most 1,000 choices and 1,000 tool calls in each chunk; past these caps the rest is not kept and `telemetry.dev.capture.truncated` is set. When present, `gen_ai.output.messages` contains bounded partial output. If output and/or finish-reason capture is incomplete, including when a choice ends without `finish_reason`, `telemetry.dev.capture.truncated` is `true`. With a mask configured, incomplete output is omitted because the mask cannot inspect the complete value. When output capture is disabled or omitted, response IDs and models, finish reasons, usage, timing, and errors may still be recorded.
|
|
82
82
|
|
|
83
|
-
Capture flags do not gate stop sequences or caller-supplied metadata or raw attributes; redact those separately when needed. Exception messages and stack traces are recorded only when both `capture_input` and `capture_output` are on, and pass through `mask`.
|
|
83
|
+
Capture flags do not gate stop sequences or caller-supplied metadata or raw attributes; redact those separately when needed. Exception messages and stack traces are recorded only when both `capture_input` and `capture_output` are on, and pass through `mask`. Model spans list the request's tools in `gen_ai.tool.definitions`: each tool's type and name, and with `capture_input` on the description and parameters (a custom tool's input format) of each function and custom tool, which pass through `mask`.
|
|
84
84
|
|
|
85
85
|
```py
|
|
86
86
|
client = wrap_openai(OpenAI(), inject_stream_usage=True)
|
{telemetry_dev_openai-0.2.5 → telemetry_dev_openai-0.2.7}/src/telemetry_dev_openai/__init__.py
RENAMED
|
@@ -22,8 +22,9 @@ from openai.resources.chat.completions.completions import AsyncCompletions, Comp
|
|
|
22
22
|
from openai.resources.embeddings import AsyncEmbeddings, Embeddings
|
|
23
23
|
from openai.resources.images import AsyncImages, Images
|
|
24
24
|
from openai.resources.responses.responses import AsyncResponses, Responses
|
|
25
|
+
from telemetry_dev._serialize import json_dumps
|
|
25
26
|
|
|
26
|
-
__version__ = "0.2.
|
|
27
|
+
__version__ = "0.2.7"
|
|
27
28
|
|
|
28
29
|
ProviderResolver = Callable[[object | None], str]
|
|
29
30
|
RequestMapper = Callable[[Mapping[str, Any]], tuple[str, dict[str, Any]]]
|
|
@@ -435,9 +436,45 @@ def _prompt_attributes(messages: object) -> dict[str, Any]:
|
|
|
435
436
|
return cast("dict[str, Any]", read(messages)) if callable(read) else {}
|
|
436
437
|
|
|
437
438
|
|
|
439
|
+
def _tool_definitions(tools: Any) -> dict[str, str] | None:
|
|
440
|
+
"""The request tools as ``gen_ai.tool.definitions``: each tool's type and name, and with input
|
|
441
|
+
capture on a function or custom tool's description and its parameters (custom tools: their
|
|
442
|
+
input format). Chat Completions nests a tool under ``function`` or ``custom``; Responses puts
|
|
443
|
+
it at the top. A built-in tool without a name, such as ``web_search``, is listed by its type.
|
|
444
|
+
The core SDK also strips descriptions and schemas with capture off; this check keeps them out
|
|
445
|
+
with a core older than that rule."""
|
|
446
|
+
client = telemetry_dev.get_client()
|
|
447
|
+
content = client is not None and client.capture_input
|
|
448
|
+
definitions: list[dict[str, Any]] = []
|
|
449
|
+
for tool in _sequence_items(tools):
|
|
450
|
+
kind = _string(_field(tool, "type"))
|
|
451
|
+
if kind is None:
|
|
452
|
+
continue
|
|
453
|
+
spec = _field(tool, "custom" if kind == "custom" else "function") or tool
|
|
454
|
+
name = _string(_field(spec, "name"))
|
|
455
|
+
if kind not in ("function", "custom"):
|
|
456
|
+
definitions.append({"type": kind} if name is None else {"type": kind, "name": name})
|
|
457
|
+
continue
|
|
458
|
+
if name is None:
|
|
459
|
+
continue
|
|
460
|
+
definition: dict[str, Any] = {"type": kind, "name": name}
|
|
461
|
+
if content:
|
|
462
|
+
description = _string(_field(spec, "description"))
|
|
463
|
+
if description is not None:
|
|
464
|
+
definition["description"] = description
|
|
465
|
+
parameters = _field(spec, "format" if kind == "custom" else "parameters")
|
|
466
|
+
if parameters is not None:
|
|
467
|
+
definition["parameters"] = parameters
|
|
468
|
+
definitions.append(definition)
|
|
469
|
+
return {"gen_ai.tool.definitions": _tools_json(definitions)} if definitions else None
|
|
470
|
+
|
|
471
|
+
|
|
438
472
|
def _chat_request(params: Mapping[str, Any]) -> tuple[str, dict[str, Any]]:
|
|
439
473
|
model = _string(params.get("model"))
|
|
440
|
-
attributes =
|
|
474
|
+
attributes = {
|
|
475
|
+
**(_tool_definitions(params.get("tools")) or {}),
|
|
476
|
+
**_prompt_attributes(params.get("messages")),
|
|
477
|
+
}
|
|
441
478
|
captured, truncated = _bounded_responses_capture(params.get("messages"), "input")
|
|
442
479
|
if truncated:
|
|
443
480
|
attributes["telemetry.dev.capture.truncated"] = True
|
|
@@ -521,7 +558,10 @@ def _chat_response(response: Any) -> dict[str, Any]:
|
|
|
521
558
|
|
|
522
559
|
def _responses_request(params: Mapping[str, Any]) -> tuple[str, dict[str, Any]]:
|
|
523
560
|
model = _string(params.get("model"))
|
|
524
|
-
attributes =
|
|
561
|
+
attributes = {
|
|
562
|
+
**(_tool_definitions(params.get("tools")) or {}),
|
|
563
|
+
**_prompt_attributes(params.get("input")),
|
|
564
|
+
}
|
|
525
565
|
captured, truncated = _bounded_responses_capture(params.get("input"), "input")
|
|
526
566
|
if truncated:
|
|
527
567
|
attributes["telemetry.dev.capture.truncated"] = True
|
|
@@ -1550,34 +1590,22 @@ def _response_event_has_output(event: Any) -> bool:
|
|
|
1550
1590
|
return isinstance(delta, str) and bool(delta)
|
|
1551
1591
|
|
|
1552
1592
|
|
|
1553
|
-
def _response_failed_error(response: Any) ->
|
|
1593
|
+
def _response_failed_error(response: Any) -> Exception:
|
|
1554
1594
|
error = _field(response, "error")
|
|
1555
|
-
if error is None
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
return RuntimeError(f"response.failed: {code}")
|
|
1563
|
-
if message:
|
|
1564
|
-
return RuntimeError(f"response.failed: {message}")
|
|
1565
|
-
return RuntimeError("response.failed")
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
def _response_stream_error(event: Any) -> RuntimeError:
|
|
1595
|
+
code = _string(_field(error, "code")) if error is not None else None
|
|
1596
|
+
message = _string(_field(error, "message")) if error is not None else None
|
|
1597
|
+
text = ": ".join(part for part in ("response.failed", code, message) if part)
|
|
1598
|
+
return _provider_failure(text, code, "response.failed")
|
|
1599
|
+
|
|
1600
|
+
|
|
1601
|
+
def _response_stream_error(event: Any) -> Exception:
|
|
1569
1602
|
code = _string(_field(event, "code"))
|
|
1570
1603
|
message = _string(_field(event, "message"))
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
if code:
|
|
1574
|
-
return RuntimeError(f"response.error: {code}")
|
|
1575
|
-
if message:
|
|
1576
|
-
return RuntimeError(f"response.error: {message}")
|
|
1577
|
-
return RuntimeError("response.error")
|
|
1604
|
+
text = ": ".join(part for part in ("response.error", code, message) if part)
|
|
1605
|
+
return _provider_failure(text, code, "response.error")
|
|
1578
1606
|
|
|
1579
1607
|
|
|
1580
|
-
def _responses_event_failure(event: Any) ->
|
|
1608
|
+
def _responses_event_failure(event: Any) -> Exception | None:
|
|
1581
1609
|
try:
|
|
1582
1610
|
event_type = _field(event, "type")
|
|
1583
1611
|
except Exception:
|
|
@@ -1586,15 +1614,25 @@ def _responses_event_failure(event: Any) -> RuntimeError | None:
|
|
|
1586
1614
|
try:
|
|
1587
1615
|
return _response_failed_error(_field(event, "response"))
|
|
1588
1616
|
except Exception:
|
|
1589
|
-
return
|
|
1617
|
+
return _provider_failure("response.failed", None, "response.failed")
|
|
1590
1618
|
if event_type == "error":
|
|
1591
1619
|
try:
|
|
1592
1620
|
return _response_stream_error(event)
|
|
1593
1621
|
except Exception:
|
|
1594
|
-
return
|
|
1622
|
+
return _provider_failure("response.error", None, "response.error")
|
|
1595
1623
|
return None
|
|
1596
1624
|
|
|
1597
1625
|
|
|
1626
|
+
def _provider_failure(message: str, code: str | int | None, kind: str | None) -> Exception:
|
|
1627
|
+
# A failure that the provider reports in a response body or a stream event, not as an HTTP
|
|
1628
|
+
# error. The core SDK records its `code` in error.type (a number counts as an HTTP status, such
|
|
1629
|
+
# as 429), else its `type`, because a plain Exception names no cause; see _error_type in
|
|
1630
|
+
# telemetry_dev/_spans.py.
|
|
1631
|
+
error = Exception(message)
|
|
1632
|
+
vars(error).update(code=code, type=kind)
|
|
1633
|
+
return error
|
|
1634
|
+
|
|
1635
|
+
|
|
1598
1636
|
def _hook_response_close(inner: Any, finish: Callable[[], None]) -> None:
|
|
1599
1637
|
"""End the span when the transport response is closed behind our back.
|
|
1600
1638
|
|
|
@@ -2433,7 +2471,7 @@ def _media_stream_error(event: Any, event_type: str) -> BaseException | None:
|
|
|
2433
2471
|
or f"OpenAI media stream ended with {event_type}"
|
|
2434
2472
|
)
|
|
2435
2473
|
code = _string(_field(raw_error, "code")) or _string(_field(event, "code"))
|
|
2436
|
-
return
|
|
2474
|
+
return _provider_failure(f"{code}: {message}" if code else message, code, event_type)
|
|
2437
2475
|
|
|
2438
2476
|
|
|
2439
2477
|
def _media_event_failure(event: Any) -> BaseException | None:
|
|
@@ -2447,7 +2485,9 @@ def _media_event_failure(event: Any) -> BaseException | None:
|
|
|
2447
2485
|
return _media_stream_error(event, event_type)
|
|
2448
2486
|
except Exception:
|
|
2449
2487
|
if event_type.endswith(".failed"):
|
|
2450
|
-
return
|
|
2488
|
+
return _provider_failure(
|
|
2489
|
+
f"OpenAI media stream ended with {event_type}", None, event_type
|
|
2490
|
+
)
|
|
2451
2491
|
return None
|
|
2452
2492
|
|
|
2453
2493
|
|
|
@@ -3231,3 +3271,14 @@ def uninstrument_openai() -> None:
|
|
|
3231
3271
|
|
|
3232
3272
|
|
|
3233
3273
|
__all__ = ["__version__", "instrument_openai", "uninstrument_openai", "wrap_openai"]
|
|
3274
|
+
|
|
3275
|
+
|
|
3276
|
+
def _tools_json(definitions: list[dict[str, Any]]) -> str:
|
|
3277
|
+
"""The definitions as JSON, or only their types and names when a schema cannot be
|
|
3278
|
+
serialized, such as one holding a cycle: the wrapper maps the request outside any catch."""
|
|
3279
|
+
try:
|
|
3280
|
+
return json_dumps(definitions)
|
|
3281
|
+
except (TypeError, ValueError):
|
|
3282
|
+
return json_dumps(
|
|
3283
|
+
[{key: tool[key] for key in ("type", "name") if key in tool} for tool in definitions]
|
|
3284
|
+
)
|
|
File without changes
|
|
File without changes
|