aid-runtime-python 0.1.153__py3-none-any.whl → 0.1.155__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.
aid_runtime/__init__.py CHANGED
@@ -337,7 +337,7 @@ from aid_runtime.types import (
337
337
  )
338
338
 
339
339
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
340
- __version__ = "0.1.153"
340
+ __version__ = "0.1.155"
341
341
 
342
342
  __all__ = [
343
343
  "AGENT_LABEL",
aid_runtime/gateway.py CHANGED
@@ -32,7 +32,14 @@ from aid_runtime.errors import (
32
32
  UnknownModelAliasError,
33
33
  )
34
34
  from aid_runtime.providers.base import Provider
35
- from aid_runtime.tracing import ATTRIBUTES, get_tracer, llm_span, record_cost, record_usage
35
+ from aid_runtime.tracing import (
36
+ ATTRIBUTES,
37
+ get_tracer,
38
+ llm_span,
39
+ record_content,
40
+ record_cost,
41
+ record_usage,
42
+ )
36
43
  from aid_runtime.types import (
37
44
  Capability,
38
45
  CompletionRequest,
@@ -55,6 +62,15 @@ _CACHE_CHUNK = 16
55
62
  CallHook = Callable[[LedgerEntry], None]
56
63
 
57
64
 
65
+ def _prompt_text(request: CompletionRequest) -> str:
66
+ """The prompt as one string, for a span that was asked to record payloads.
67
+
68
+ Role-prefixed rather than concatenated, so a captured prompt still shows which
69
+ turn a line came from once it is a row of JSONL rather than a live message list.
70
+ """
71
+ return "\n\n".join(f"{message.role}: {message.content}" for message in request.messages)
72
+
73
+
58
74
  def _sse(payload: dict[str, Any]) -> bytes:
59
75
  """Encode one Server-Sent Event ``data`` frame as UTF-8 bytes."""
60
76
  return f"data: {json.dumps(payload, separators=(',', ':'))}\n\n".encode()
@@ -270,6 +286,7 @@ class ModelGateway:
270
286
  cost = spec.cost.cost_usd(usage)
271
287
  record_usage(span, usage)
272
288
  record_cost(span, cost, alias=pinned.alias)
289
+ record_content(span, input_text=_prompt_text(request), output_text=text)
273
290
 
274
291
  entry = LedgerEntry(
275
292
  alias=pinned.alias,
@@ -341,6 +358,7 @@ class ModelGateway:
341
358
  cost = spec.cost.cost_usd(response.usage)
342
359
  record_usage(span, response.usage)
343
360
  record_cost(span, cost, alias=pinned.alias)
361
+ record_content(span, input_text=_prompt_text(request), output_text=response.text)
344
362
  if len(attempted) > 1:
345
363
  span.set_attribute(ATTRIBUTES["attempts"], list(attempted))
346
364
 
aid_runtime/tracing.py CHANGED
@@ -16,6 +16,7 @@ spans written here leave the process.
16
16
  from __future__ import annotations
17
17
 
18
18
  import contextlib
19
+ import os
19
20
  from collections.abc import Iterator, Sequence
20
21
  from typing import Any, Protocol
21
22
 
@@ -27,13 +28,17 @@ from aid_runtime.types import TokenUsage
27
28
 
28
29
  __all__ = [
29
30
  "ATTRIBUTES",
31
+ "CONTENT_ENV",
30
32
  "GEN_AI_OPERATION_CHAT",
33
+ "MAX_CONTENT_CHARS",
31
34
  "GuardrailEvent",
32
35
  "agent_span",
33
36
  "aid_attributes",
37
+ "content_capture_enabled",
34
38
  "get_tracer",
35
39
  "llm_span",
36
40
  "record_attributes",
41
+ "record_content",
37
42
  "record_cost",
38
43
  "record_exception",
39
44
  "record_guardrail",
@@ -87,8 +92,20 @@ ATTRIBUTES: dict[str, str] = {
87
92
  "guardrail_stage": "aid.guardrail.stage",
88
93
  "guardrail_action": "aid.guardrail.action",
89
94
  "guardrail_policies": "aid.guardrail.policies",
95
+ # Payload text. Written only when ``AID_TRACE_CONTENT`` asks for it; see
96
+ # :func:`record_content` for why the default is off.
97
+ "input_text": "aid.input.text",
98
+ "output_text": "aid.output.text",
90
99
  }
91
100
 
101
+ #: The environment variable that opts in to recording prompt and completion text.
102
+ CONTENT_ENV = "AID_TRACE_CONTENT"
103
+
104
+ #: Longest payload fragment a span records. A dataset needs enough of the input to
105
+ #: name the case, not a full transcript, and an unbounded attribute would put a
106
+ #: megabyte into one JSONL line.
107
+ MAX_CONTENT_CHARS = 4000
108
+
92
109
  _TRACER_NAME = "aid_runtime"
93
110
 
94
111
 
@@ -162,6 +179,41 @@ def record_attributes(span: Span, **values: Any) -> None:
162
179
  span.set_attribute(key, value)
163
180
 
164
181
 
182
+ def content_capture_enabled() -> bool:
183
+ """Whether ``AID_TRACE_CONTENT`` asks for prompt and completion text on spans."""
184
+ return os.environ.get(CONTENT_ENV, "").strip().lower() in {"1", "true", "yes", "on"}
185
+
186
+
187
+ def record_content(span: Span, *, input_text: str | None, output_text: str | None) -> None:
188
+ """Attach the prompt and the completion to a span **only** when asked to.
189
+
190
+ Everything else this runtime records is a fact about a call -- a model, a token
191
+ count, a price. Payloads are what a user said and what a model answered, so they
192
+ stay out of the trace unless the operator names :data:`CONTENT_ENV`. Three
193
+ reasons the default matters:
194
+
195
+ - The sink writes to a local directory that a repository may commit and a CI log
196
+ may echo, so text that is off by default cannot leak by accident.
197
+ - Nothing here redacts. A captured prompt is captured as it was sent, and the
198
+ operator who set the variable owns that decision; the spec's ``pii.redact``
199
+ governs the deployed feedback path, not this local switch.
200
+ - ``aid trace export --dataset`` is the one reader that wants the text, and it
201
+ can only build a case from a run that recorded one.
202
+
203
+ Empty and whitespace-only text is skipped, so a span that carried nothing does
204
+ not look like a decided empty answer.
205
+ """
206
+ if not content_capture_enabled():
207
+ return
208
+ for key, value in (
209
+ ("input_text", input_text),
210
+ ("output_text", output_text),
211
+ ):
212
+ if value is None or value.strip() == "":
213
+ continue
214
+ span.set_attribute(ATTRIBUTES[key], value[:MAX_CONTENT_CHARS])
215
+
216
+
165
217
  @contextlib.contextmanager
166
218
  def agent_span(
167
219
  *,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.153
3
+ Version: 0.1.155
4
4
  Summary: The AID Framework Python runtime: model gateway with pinning and cost budgets, prompt registry, tool executor, OTel GenAI tracing, plain-async pipeline, and an eval harness that gates on statistical regression against a stored baseline.
5
5
  Author: The AID Framework authors
6
6
  License-Expression: Apache-2.0
@@ -1,11 +1,11 @@
1
- aid_runtime/__init__.py,sha256=en6ZkSmIfy-1CbFpkfM2x44vAgoLjp678iWZs4j5OUs,14260
1
+ aid_runtime/__init__.py,sha256=YJf1EUITVngHd2HOYgNFwXtVboOluK-Vx1uYCG6cItE,14260
2
2
  aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
3
3
  aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
4
4
  aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
5
5
  aid_runtime/cost.py,sha256=Hc7X6mtkeanL9eJ1lEZZCQ9LxZurhL1it0gm3s9LpQE,5465
6
6
  aid_runtime/decisions.py,sha256=1V0d9EttfwTuh8jgtC5X3wQDTZrl20ZREcs6VqIdcHY,15662
7
7
  aid_runtime/errors.py,sha256=TKkZyZZcFTl5yxNKlA8jUF4ORpYHLGFEvc6r9FK4X70,12572
8
- aid_runtime/gateway.py,sha256=p0qsaQUAYrwSzlWwpUi1wY6pdOeU98QjsWLzq8045Fo,18267
8
+ aid_runtime/gateway.py,sha256=MEEjWucsAiDzjoVfWiLxjlS0bjkYYCj5lQYY0N2CsdI,18903
9
9
  aid_runtime/local_trace.py,sha256=eg5LDsg1VfNmJNqs8KjtdzTjSqg7SWTXunLwg-b3IcM,7870
10
10
  aid_runtime/metrics.py,sha256=w55qPlFRQXUfASxpskgYiSeFcFxkL_mwbDp2IhevAbg,6050
11
11
  aid_runtime/pipeline.py,sha256=Y2jiiYISLLxgBKGBj5J2rseEDzE8RXP8v5pJ_edjHfM,6334
@@ -15,7 +15,7 @@ aid_runtime/steps.py,sha256=44v8-ebN77VGZePmfhBYi0KORFBrH2GWFSzlfczaJhs,12047
15
15
  aid_runtime/structured.py,sha256=Uga-VoGJN56lUtr-M3nmUjAKGF32cAAk-p7WceUgPzg,5061
16
16
  aid_runtime/telemetry.py,sha256=ykAXCL3XFi0yok3xr-fjD3oGvVycdfyrqJ8nGKC_ZHc,12872
17
17
  aid_runtime/tools.py,sha256=7LeaPI5aJioNe3vexUv94tvIRpOwxj1Ujt723qvO3UI,9906
18
- aid_runtime/tracing.py,sha256=jZMl5btxcUSQQDeWGMT0lQB-YSdRW7cxl5QsJVeWE7Q,9795
18
+ aid_runtime/tracing.py,sha256=wcu-gKToGcWn6O_kKz_0kcWkyaQLoRCWLkWRDgBqDRE,12080
19
19
  aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
20
20
  aid_runtime/agents/__init__.py,sha256=H_WKu9u0fvF56W7ccTByEv1RdayzwUfRzEKsXXDRr2A,3113
21
21
  aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
@@ -59,7 +59,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
59
59
  aid_runtime/retrieval/retriever.py,sha256=0Cx-_6Qcf2TRolFy4ktMseyyyr-WLphMYYHane0t9lA,24412
60
60
  aid_runtime/retrieval/stores.py,sha256=mG261rUHZIzB7niiTj_DuUzcNMOd4QYENk5ArgI1iBo,6840
61
61
  aid_runtime/retrieval/types.py,sha256=hJ5IRSD-PTfrc1xhuK6CBdX0Yr-S6L_JeSMPity8ecM,20447
62
- aid_runtime_python-0.1.153.dist-info/METADATA,sha256=J9blBb6xZRGRazss9FcH6UqrIRLK0hSUVkeaQZDI3Zs,17163
63
- aid_runtime_python-0.1.153.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
64
- aid_runtime_python-0.1.153.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
65
- aid_runtime_python-0.1.153.dist-info/RECORD,,
62
+ aid_runtime_python-0.1.155.dist-info/METADATA,sha256=4a86PXRopy_bfucaIgT8yOt_89yzcdKEEMlxxN-vvj0,17163
63
+ aid_runtime_python-0.1.155.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
64
+ aid_runtime_python-0.1.155.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
65
+ aid_runtime_python-0.1.155.dist-info/RECORD,,