aid-runtime-python 0.1.160__py3-none-any.whl → 0.1.161__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
@@ -201,6 +201,7 @@ from aid_runtime.providers import (
201
201
  default_bedrock_base_url,
202
202
  default_vertex_base_url,
203
203
  )
204
+ from aid_runtime.redaction import REDACTED_FORMAT, redact_values
204
205
  from aid_runtime.retrieval import (
205
206
  DEFAULT_CANDIDATE_FACTOR,
206
207
  DEFAULT_SYNC_TIMEOUT_SECONDS,
@@ -349,7 +350,7 @@ from aid_runtime.types import (
349
350
  )
350
351
 
351
352
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
352
- __version__ = "0.1.160"
353
+ __version__ = "0.1.161"
353
354
 
354
355
  __all__ = [
355
356
  "AGENT_LABEL",
@@ -380,6 +381,7 @@ __all__ = [
380
381
  "METRIC_NAMES",
381
382
  "OTLP_EXTRA",
382
383
  "PRINCIPAL_KEY",
384
+ "REDACTED_FORMAT",
383
385
  "REDUCED_KEY",
384
386
  "RESERVED_KEYS",
385
387
  "STEPS_KEY",
@@ -635,6 +637,7 @@ __all__ = [
635
637
  "record_exception",
636
638
  "record_hitl",
637
639
  "record_usage",
640
+ "redact_values",
638
641
  "reduce_step",
639
642
  "repair_prompt",
640
643
  "require_reviewed",
aid_runtime/gateway.py CHANGED
@@ -32,6 +32,7 @@ from aid_runtime.errors import (
32
32
  UnknownModelAliasError,
33
33
  )
34
34
  from aid_runtime.providers.base import Provider
35
+ from aid_runtime.redaction import redact_values
35
36
  from aid_runtime.tracing import (
36
37
  ATTRIBUTES,
37
38
  get_tracer,
@@ -67,8 +68,11 @@ def _prompt_text(request: CompletionRequest) -> str:
67
68
 
68
69
  Role-prefixed rather than concatenated, so a captured prompt still shows which
69
70
  turn a line came from once it is a row of JSONL rather than a live message list.
71
+ Redacted with the request's own bindings: what the provider received is
72
+ unchanged, and what a trace keeps no longer carries a value the spec forbids.
70
73
  """
71
- return "\n\n".join(f"{message.role}: {message.content}" for message in request.messages)
74
+ text = "\n\n".join(f"{message.role}: {message.content}" for message in request.messages)
75
+ return redact_values(text, request.redactions)
72
76
 
73
77
 
74
78
  def _sse(payload: dict[str, Any]) -> bytes:
@@ -286,7 +290,11 @@ class ModelGateway:
286
290
  cost = spec.cost.cost_usd(usage)
287
291
  record_usage(span, usage)
288
292
  record_cost(span, cost, alias=pinned.alias)
289
- record_content(span, input_text=_prompt_text(request), output_text=text)
293
+ record_content(
294
+ span,
295
+ input_text=_prompt_text(request),
296
+ output_text=redact_values(text, request.redactions),
297
+ )
290
298
 
291
299
  entry = LedgerEntry(
292
300
  alias=pinned.alias,
@@ -358,7 +366,11 @@ class ModelGateway:
358
366
  cost = spec.cost.cost_usd(response.usage)
359
367
  record_usage(span, response.usage)
360
368
  record_cost(span, cost, alias=pinned.alias)
361
- record_content(span, input_text=_prompt_text(request), output_text=response.text)
369
+ record_content(
370
+ span,
371
+ input_text=_prompt_text(request),
372
+ output_text=redact_values(response.text, request.redactions),
373
+ )
362
374
  if len(attempted) > 1:
363
375
  span.set_attribute(ATTRIBUTES["attempts"], list(attempted))
364
376
 
aid_runtime/prompts.py CHANGED
@@ -15,6 +15,7 @@ from typing import Self
15
15
  from pydantic import BaseModel, ConfigDict, Field
16
16
 
17
17
  from aid_runtime.errors import PromptNotFoundError, PromptRenderError
18
+ from aid_runtime.redaction import Redaction
18
19
 
19
20
  __all__ = ["Example", "PromptRegistry", "PromptTemplate", "RenderedPrompt"]
20
21
 
@@ -89,6 +90,7 @@ class PromptTemplate(BaseModel):
89
90
  version=self.version,
90
91
  text=body,
91
92
  examples=self.examples,
93
+ bindings=tuple(sorted(values.items())),
92
94
  )
93
95
 
94
96
 
@@ -106,6 +108,14 @@ class RenderedPrompt(BaseModel):
106
108
  version: str
107
109
  text: str
108
110
  examples: tuple[Example, ...] = ()
111
+ #: Every ``(name, value)`` pair the caller supplied, so a redact set naming a
112
+ #: field can be turned into the exact substring to strip from a recorded copy.
113
+ bindings: tuple[Redaction, ...] = ()
114
+
115
+ def redactions(self, names: Iterable[str]) -> tuple[Redaction, ...]:
116
+ """The bound pairs whose variable name appears in ``names``."""
117
+ wanted = set(names)
118
+ return tuple((name, value) for name, value in self.bindings if name in wanted)
109
119
 
110
120
  def as_messages(self, *, system: str | None = None) -> tuple[tuple[str, str], ...]:
111
121
  """Flatten to ``(role, content)`` pairs, examples inlined as few-shot turns."""
@@ -0,0 +1,46 @@
1
+ """Value-bound redaction for recorded traces.
2
+
3
+ ``observability.redact`` names *fields*, and a field name cannot be stripped out of
4
+ prose. What can be stripped is a **value**: the moment a prompt is rendered, the
5
+ runtime knows which variable supplied which substring, so a name in the redact set
6
+ becomes an exact value to remove from the copy that is written to a trace.
7
+
8
+ Redaction applies to the recorded copy only. The model still receives what the
9
+ application sent, because editing a request would change the answer and a framework
10
+ that silently rewrote one could not honestly report what it ran.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from collections.abc import Iterable, Sequence
16
+
17
+ __all__ = ["REDACTED_FORMAT", "redact_values"]
18
+
19
+ #: What replaces a stripped value. The field name, not the value, so a trace reader
20
+ #: can still see that a field was present and which one it was.
21
+ REDACTED_FORMAT = "[redacted:{name}]"
22
+
23
+ #: One ``(name, value)`` pair bound when a prompt was rendered.
24
+ Redaction = tuple[str, str]
25
+
26
+
27
+ def _usable(pairs: Iterable[Redaction]) -> list[Redaction]:
28
+ """The pairs that can be applied at all.
29
+
30
+ A blank value is dropped rather than replaced: ``str.replace("", marker)``
31
+ inserts a marker at every position, so an empty variable would destroy the
32
+ text it was meant to protect instead of protecting it.
33
+ """
34
+ return [(name, value) for name, value in pairs if value.strip() != ""]
35
+
36
+
37
+ def redact_values(text: str, pairs: Sequence[Redaction]) -> str:
38
+ """Replace every occurrence of a bound value with ``[redacted:name]``."""
39
+ if not text or not pairs:
40
+ return text
41
+ redacted = text
42
+ # Longest value first: where one value contains another, the shorter pass would
43
+ # otherwise leave a fragment of the longer one on either side of its marker.
44
+ for name, value in sorted(_usable(pairs), key=lambda pair: len(pair[1]), reverse=True):
45
+ redacted = redacted.replace(value, REDACTED_FORMAT.format(name=name))
46
+ return redacted
aid_runtime/tracing.py CHANGED
@@ -194,9 +194,11 @@ def record_content(span: Span, *, input_text: str | None, output_text: str | Non
194
194
 
195
195
  - The sink writes to a local directory that a repository may commit and a CI log
196
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.
197
+ - Redaction happens before this call, in the caller that rendered the prompt: the
198
+ gateway replaces every value the spec withholds with ``[redacted:<name>]`` in the
199
+ text it hands to this function, so what arrives here is already the recordable
200
+ form. This function does not redact, and must not: it receives prose, not the
201
+ field bindings a value could be recognised by.
200
202
  - ``aid trace export --dataset`` is the one reader that wants the text, and it
201
203
  can only build a case from a run that recorded one.
202
204
 
aid_runtime/types.py CHANGED
@@ -264,18 +264,29 @@ class CompletionRequest(BaseModel):
264
264
  tools: tuple[ToolDefinition, ...] = ()
265
265
  stop: tuple[str, ...] = ()
266
266
  metadata: dict[str, str] = Field(default_factory=dict)
267
+ #: ``(name, value)`` pairs bound by the redact set, used only for the recorded
268
+ #: copy of this call. They never reach the provider and never change a fingerprint.
269
+ redactions: tuple[tuple[str, str], ...] = ()
267
270
 
268
271
  @classmethod
269
- def prompt(cls, text: str, *, system: str | None = None) -> CompletionRequest:
272
+ def prompt(
273
+ cls,
274
+ text: str,
275
+ *,
276
+ system: str | None = None,
277
+ redactions: tuple[tuple[str, str], ...] = (),
278
+ ) -> CompletionRequest:
270
279
  messages = []
271
280
  if system is not None:
272
281
  messages.append(Message.system(system))
273
282
  messages.append(Message.user(text))
274
- return cls(messages=tuple(messages))
283
+ return cls(messages=tuple(messages), redactions=redactions)
275
284
 
276
285
  def fingerprint(self) -> str:
277
286
  """Stable hash of everything that can change the model's output."""
278
- payload = self.model_dump(mode="json", exclude_none=True)
287
+ # ``redactions`` is excluded: it describes how the call is *recorded*, so two
288
+ # calls that differ only in their redact set must share one cache entry.
289
+ payload = self.model_dump(mode="json", exclude_none=True, exclude={"redactions"})
279
290
  blob = json.dumps(payload, sort_keys=True, separators=(",", ":"))
280
291
  return hashlib.sha256(blob.encode("utf-8")).hexdigest()
281
292
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.160
3
+ Version: 0.1.161
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,22 +1,23 @@
1
- aid_runtime/__init__.py,sha256=2CQoFRQC_CoKpDFoxGcwPmt_D1-2_HrdjH0okiIq258,14794
1
+ aid_runtime/__init__.py,sha256=vvAyfJBnkNwshJLzm7W_4vAtn7MXYRmGXAaubqq6G9w,14903
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=gJsQ7FugsA5UU0LrTaOjtZrsRJijCDcG5mKjDNoCU48,13083
8
- aid_runtime/gateway.py,sha256=MEEjWucsAiDzjoVfWiLxjlS0bjkYYCj5lQYY0N2CsdI,18903
8
+ aid_runtime/gateway.py,sha256=wqIXW8vC1FyyG5BLZbTyIVkxJPMgeINPG5gCKe2rSXY,19388
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
12
- aid_runtime/prompts.py,sha256=eN3p_Yc-J6pyKaL2w4I-EmwvcPKErnXBvvI_Syq13ZM,7083
12
+ aid_runtime/prompts.py,sha256=kA2sBIy-e7R0OG6GO4-Knf30zHL4OA6Gt4uPZyNon9o,7646
13
13
  aid_runtime/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
+ aid_runtime/redaction.py,sha256=25oFaFjzmEP9asWVwyWO8bg42mGLncLnvuHQeR5qKnQ,1993
14
15
  aid_runtime/steps.py,sha256=44v8-ebN77VGZePmfhBYi0KORFBrH2GWFSzlfczaJhs,12047
15
16
  aid_runtime/structured.py,sha256=Uga-VoGJN56lUtr-M3nmUjAKGF32cAAk-p7WceUgPzg,5061
16
17
  aid_runtime/telemetry.py,sha256=ykAXCL3XFi0yok3xr-fjD3oGvVycdfyrqJ8nGKC_ZHc,12872
17
18
  aid_runtime/tools.py,sha256=7LeaPI5aJioNe3vexUv94tvIRpOwxj1Ujt723qvO3UI,9906
18
- aid_runtime/tracing.py,sha256=wcu-gKToGcWn6O_kKz_0kcWkyaQLoRCWLkWRDgBqDRE,12080
19
- aid_runtime/types.py,sha256=cVYgmacDIhStQT5dX_X67-q57sPBBaWC5nybtGVqVsw,10389
19
+ aid_runtime/tracing.py,sha256=zGZi7faB87IRv5873B-sV2d3J_JGiQS4pbHFqjTElUs,12247
20
+ aid_runtime/types.py,sha256=91hwvBkjTkvCJ-WdQO3ezZZCyqzXmS7Ns1WP1m0EQzc,10916
20
21
  aid_runtime/agents/__init__.py,sha256=H_WKu9u0fvF56W7ccTByEv1RdayzwUfRzEKsXXDRr2A,3113
21
22
  aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
22
23
  aid_runtime/agents/deep_agents.py,sha256=Eg_BLm2cg7T_tXSxR7ag8SEvTMCFBLHoIvln3BoWORw,26966
@@ -61,7 +62,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
61
62
  aid_runtime/retrieval/retriever.py,sha256=0Cx-_6Qcf2TRolFy4ktMseyyyr-WLphMYYHane0t9lA,24412
62
63
  aid_runtime/retrieval/stores.py,sha256=mG261rUHZIzB7niiTj_DuUzcNMOd4QYENk5ArgI1iBo,6840
63
64
  aid_runtime/retrieval/types.py,sha256=hJ5IRSD-PTfrc1xhuK6CBdX0Yr-S6L_JeSMPity8ecM,20447
64
- aid_runtime_python-0.1.160.dist-info/METADATA,sha256=L6CzOTJXKaWggEy6kaQO8J7bIFsdhSQvdAHfJe_btDw,17163
65
- aid_runtime_python-0.1.160.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
66
- aid_runtime_python-0.1.160.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
67
- aid_runtime_python-0.1.160.dist-info/RECORD,,
65
+ aid_runtime_python-0.1.161.dist-info/METADATA,sha256=0ziq3dW-iglC2Ucdafn_JCFv7j3mRWFnl8_PKDob4rs,17163
66
+ aid_runtime_python-0.1.161.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
67
+ aid_runtime_python-0.1.161.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
68
+ aid_runtime_python-0.1.161.dist-info/RECORD,,