aid-runtime-python 0.1.93__py3-none-any.whl → 0.1.95__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
@@ -60,6 +60,7 @@ from aid_runtime.control import (
60
60
  map_step,
61
61
  parallel,
62
62
  reduce_step,
63
+ route,
63
64
  )
64
65
  from aid_runtime.cost import (
65
66
  Budget,
@@ -294,7 +295,7 @@ from aid_runtime.types import (
294
295
  )
295
296
 
296
297
  #: Kept in sync with ``pyproject.toml`` by a test, so the two cannot drift.
297
- __version__ = "0.1.93"
298
+ __version__ = "0.1.95"
298
299
 
299
300
  __all__ = [
300
301
  "ANTHROPIC_VERSION",
@@ -539,6 +540,7 @@ __all__ = [
539
540
  "resolve_uri",
540
541
  "retrieval_metrics",
541
542
  "retrieve_step",
543
+ "route",
542
544
  "rrf_score",
543
545
  "run_evaluation",
544
546
  "run_structured",
@@ -461,6 +461,17 @@ class GuardrailEngine:
461
461
  "input",
462
462
  f"decision {name!r} answered {label!r}, which this guardrail does not map",
463
463
  )
464
+ if guardrail.min_confidence > 0 and answer.confidence is None:
465
+ # A `noul` answer carries only its probability, and the wire contract forbids
466
+ # a confidence on it. A floor that cannot be read must not pass: silently
467
+ # treating the missing value as "no opinion, therefore allow" is exactly the
468
+ # failure this check exists to stop.
469
+ raise GuardrailBlockedError(
470
+ guardrail.id,
471
+ "input",
472
+ f"decision {name!r} answered without a confidence, and this guardrail "
473
+ f"requires at least {guardrail.min_confidence:.2f}",
474
+ )
464
475
  if answer.confidence is not None and answer.confidence < guardrail.min_confidence:
465
476
  raise GuardrailBlockedError(
466
477
  guardrail.id,
aid_runtime/control.py CHANGED
@@ -1,4 +1,4 @@
1
- """Composite pipeline steps: ``branch``, ``parallel``, ``map`` and ``reduce``.
1
+ """Composite pipeline steps: ``branch``, ``route``, ``parallel``, ``map`` and ``reduce``.
2
2
 
3
3
  :class:`~aid_runtime.pipeline.Pipeline` is a sequential reducer, and it stays one.
4
4
  Each factory here returns an ordinary :data:`~aid_runtime.pipeline.Step`, so a
@@ -27,13 +27,18 @@ from __future__ import annotations
27
27
 
28
28
  import asyncio
29
29
  from collections.abc import Awaitable, Callable, Mapping, Sequence
30
- from typing import Any, TypeVar
30
+ from typing import TYPE_CHECKING, Any, TypeVar
31
31
 
32
32
  from aid_runtime.agents.policy import Principal, evaluate_policy, policy_source
33
33
  from aid_runtime.concurrency import ConcurrencyLimiter, holds_slot
34
+ from aid_runtime.decisions import DecisionClient, DecisionQuestion
34
35
  from aid_runtime.errors import PipelineError
35
36
  from aid_runtime.pipeline import STEPS_KEY, Pipeline, PipelineContext
36
37
 
38
+ if TYPE_CHECKING:
39
+ from aid_runtime.agents.guardrails import DecisionAsker
40
+ from aid_runtime.agents.ir import DecisionDefinition
41
+
37
42
  __all__ = [
38
43
  "DEPTH_KEY",
39
44
  "INDEX_KEY",
@@ -47,6 +52,7 @@ __all__ = [
47
52
  "map_step",
48
53
  "parallel",
49
54
  "reduce_step",
55
+ "route",
50
56
  ]
51
57
 
52
58
  #: Deepest composite nesting a pipeline may declare.
@@ -208,6 +214,88 @@ def branch(
208
214
  return _named(_branch, name)
209
215
 
210
216
 
217
+ def route(
218
+ *,
219
+ decision: str,
220
+ state: str,
221
+ on: Mapping[str, Pipeline],
222
+ otherwise: Pipeline | None = None,
223
+ as_: str | None = None,
224
+ decisions: Mapping[str, DecisionDefinition] | None = None,
225
+ client_factory: Callable[[], DecisionAsker] | None = None,
226
+ max_depth: int = MAX_STEP_DEPTH,
227
+ name: str | None = None,
228
+ ) -> CompositeStep:
229
+ """Ask one ``choice`` decision which arm to run, then run that arm.
230
+
231
+ This is the one composite that asks a model a question, and it is the difference
232
+ between a ``branch`` and a ``route``: a branch's predicate is a policy over the
233
+ caller, so it can only answer who is asking. A route's question is asked about
234
+ ``state`` -- the rendered text of what the pipeline has produced so far -- so it
235
+ can answer what the request *means*.
236
+
237
+ Every way the question can fail fails the run rather than picking an arm:
238
+ a decision with no definition in this runtime, a client that raises, an answer
239
+ with no choice, and a label no arm maps. A default is deliberately not a fourth
240
+ outcome; it is the arm an *unexpected* label runs, and the spec gate refuses a
241
+ default that a mapped set already covers.
242
+
243
+ ``as_`` binds the label that was answered -- including one that fell through to
244
+ ``otherwise`` -- so a later step can name the arm that ran rather than re-deriving
245
+ it. The client is built on first use, not when the pipeline is assembled: an app
246
+ with no TypeSafe key still boots as long as no request reaches a route.
247
+ """
248
+ declared = dict(decisions or {})
249
+ factory = client_factory or DecisionClient
250
+ client: DecisionAsker | None = None
251
+
252
+ async def _route(context: PipelineContext) -> PipelineContext:
253
+ nonlocal client
254
+ nested = _enter(context, max_depth, f"route ({decision})")
255
+ definition = declared.get(decision)
256
+ if definition is None:
257
+ raise PipelineError(
258
+ f"route needs a definition for decision {decision!r}; the generated app "
259
+ f"carries one for every decision its routes name"
260
+ )
261
+ if client is None:
262
+ client = factory()
263
+ question = DecisionQuestion(
264
+ type="choice",
265
+ instructions=definition.instructions,
266
+ criteria=definition.criteria,
267
+ )
268
+ try:
269
+ response = await client.decide(
270
+ model=definition.model,
271
+ state=state,
272
+ questions={decision: question},
273
+ )
274
+ except Exception as error:
275
+ raise PipelineError(f"route decision {decision!r} could not answer: {error}") from error
276
+ answer = response.answers.get(decision)
277
+ label = None if answer is None else answer.choice
278
+ if label is None:
279
+ raise PipelineError(
280
+ f"route decision {decision!r} returned no choice, so no arm can be "
281
+ f"selected; a route does not guess which one was meant"
282
+ )
283
+ chosen = on.get(label)
284
+ if chosen is None:
285
+ if otherwise is None:
286
+ raise PipelineError(
287
+ f"route decision {decision!r} answered {label!r}, which no arm maps "
288
+ f"and no `default` catches"
289
+ )
290
+ chosen = otherwise
291
+ _, added = await _run_body(chosen, nested)
292
+ if as_ is not None:
293
+ added[as_] = label
294
+ return _escape(context.with_values(**added))
295
+
296
+ return _named(_route, name)
297
+
298
+
211
299
  def parallel(
212
300
  *,
213
301
  branches: Sequence[Pipeline],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aid-runtime-python
3
- Version: 0.1.93
3
+ Version: 0.1.95
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,7 +1,7 @@
1
- aid_runtime/__init__.py,sha256=3FmRoDVr4sokQBGpjUYRBs-wohXOIy13xf6e30AhvXg,12397
1
+ aid_runtime/__init__.py,sha256=hhoY33U-IBJSbNMTsPg_gNnhxyEpsP5aGTP1U6-q_ag,12421
2
2
  aid_runtime/cache.py,sha256=jhw_EBd9dfWEUzYeibOvIkVBqqiLUjqF5WKEPz9GkhE,6836
3
3
  aid_runtime/concurrency.py,sha256=z0tFQHw35ttFLKfazGBjCMPOLQOnUotE_OPpvR1piH4,2829
4
- aid_runtime/control.py,sha256=u4U97QFdYSSjOPY3D6f1-GZhkGhygEdR-apdgw_jJok,13797
4
+ aid_runtime/control.py,sha256=y6wPn0fqYJv0nVSsjzpzfiaq1Y7zBoMxCpdrEuqkfvg,17529
5
5
  aid_runtime/cost.py,sha256=Hc7X6mtkeanL9eJ1lEZZCQ9LxZurhL1it0gm3s9LpQE,5465
6
6
  aid_runtime/decisions.py,sha256=6rkpFODb0PAQ9cqtKuz5ZRA19C3Ljwz1aGWKcovVSc4,13859
7
7
  aid_runtime/errors.py,sha256=TKkZyZZcFTl5yxNKlA8jUF4ORpYHLGFEvc6r9FK4X70,12572
@@ -19,7 +19,7 @@ aid_runtime/agents/__init__.py,sha256=Qkcjm0YokmObQWRB3G_xFd_LI4sku_GajDbE_7TzyF
19
19
  aid_runtime/agents/chat_model.py,sha256=fZdCmSpoECuBVGZE1s8NmT2erT33RhDuLh3yJM6kAJE,14808
20
20
  aid_runtime/agents/deep_agents.py,sha256=ZM6ZSc92arCvtsYGsIGdF-NryqN_tvXtRqbdlHaGpeE,26948
21
21
  aid_runtime/agents/engine.py,sha256=RQUV49u7MLDmirnFv8YsRGdIutzUGsGaLA6Y2JT5wj0,4129
22
- aid_runtime/agents/guardrails.py,sha256=XwllB-33HE65FJumCMx--LGDVNfWjLrrupQHfUbCAG8,26624
22
+ aid_runtime/agents/guardrails.py,sha256=2ANNAXiKQKOija2ychgDBu-DfmQv0P3Byl4LRoI52cE,27276
23
23
  aid_runtime/agents/ir.py,sha256=GzIbaMFsq64M0eC8tB2xB6pKrXtJdT4Z1LtuM4wKEEE,25145
24
24
  aid_runtime/agents/loop.py,sha256=T-QnC9BQ9K3RyZfHdAuzWiwMXBpQjDLGdHrLgf2fnFM,52608
25
25
  aid_runtime/agents/memory.py,sha256=6_D6k2bzAiR2euNvzr6HB28wsUUq-R-WTQc12gkB8ao,9156
@@ -51,7 +51,7 @@ aid_runtime/retrieval/rerank.py,sha256=L4ICBi9YHDzhnKVtImKh5Wzre1HDzS8at-AKDkXBJ
51
51
  aid_runtime/retrieval/retriever.py,sha256=hVLP0xOLKioYsa-aS1akyJl5p-WbpVjm8OfCw26m4CE,13941
52
52
  aid_runtime/retrieval/stores.py,sha256=VEi68AaMHcQjdAbWOHflp3jCl1_1db71rIzZjTouLlo,5857
53
53
  aid_runtime/retrieval/types.py,sha256=8vKP21rmE9LglnB5gtv0dLBIkXzaPqdG3-GS98K0gpc,13498
54
- aid_runtime_python-0.1.93.dist-info/METADATA,sha256=15Fp_probWOEU1BrkhBzSGOIItM-zfqkK_FbnxklRKM,17005
55
- aid_runtime_python-0.1.93.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
56
- aid_runtime_python-0.1.93.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
57
- aid_runtime_python-0.1.93.dist-info/RECORD,,
54
+ aid_runtime_python-0.1.95.dist-info/METADATA,sha256=9zG6VmRnHRX23EssZU6sMfAiasijfh7EgHEcTGKA6HA,17005
55
+ aid_runtime_python-0.1.95.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
56
+ aid_runtime_python-0.1.95.dist-info/top_level.txt,sha256=QvODgNVRDDMmLcTnoIQ4jYnhqf-N4b9sHWEmaUUxp44,12
57
+ aid_runtime_python-0.1.95.dist-info/RECORD,,