Routing, Fallbacks, and Graceful Degradation › Partial Service: Returning a Reduced but Honest Result
Language AI HandbookAll chapters If the attempt budget is exhausted or no tier passes, an allowed public policy excerpt yields a partial answer. This path says what is missing. Otherwise the router abstains. Withheld synthesis and missing evidence are different states. A private request cannot use the public excerpt path unless the caller has already determined that the excerpt is appropriate to return.
PythonIn [19]Expand
from enum import Enum
class Outcome(Enum):
COMPLETE = "complete"
PARTIAL = "partial"
ABSTAIN = "abstain"
@dataclass(frozen=True)
class Option:
tier: str
eligible: bool
available: bool
candidate: tuple[str, float] | None
@dataclass(frozen=True)
class ServiceResult:
outcome: Outcome
text: str
tier: str | None
attempted: tuple[str, ...]
def serve(
options: tuple[Option, ...],
*,
safe_excerpt: str | None,
max_attempts: int,
threshold: float = 0.65,
) -> ServiceResult:
attempts = []
for option in options:
if len(attempts) >= max_attempts:
break
if not option.eligible or not option.available:
continue
attempts.append(option.tier)
if option.candidate is None:
continue
text, score = option.candidate
if score >= threshold:
return ServiceResult(
Outcome.COMPLETE, text, option.tier, tuple(attempts)
)
if safe_excerpt:
return ServiceResult(
Outcome.PARTIAL,
"Public policy excerpt only. Summary unavailable.\n" + safe_excerpt,
None,
tuple(attempts),
)
return ServiceResult(
Outcome.ABSTAIN, "I couldn't verify an answer.", None, tuple(attempts)
)PythonIn [20]Expand
primary = Option("Primary", True, False, None)
backup = Option(
"Backup", True, True, ("Fictional returns period: 30 days.", 0.82)
)
unapproved = Option("Unapproved", False, True, ("Unverified answer", 0.99))
timeout = Option("Transient failure", True, True, None)
excerpt = "The demo store accepts unopened returns within 30 days."
complete = serve(
(unapproved, primary, backup), safe_excerpt=None, max_attempts=2
)
partial = serve((timeout, backup), safe_excerpt=excerpt, max_attempts=1)
abstain = serve((unapproved, primary), safe_excerpt=None, max_attempts=2)
assert complete.outcome is Outcome.COMPLETE and complete.attempted == (
"Backup",
)
assert partial.outcome is Outcome.PARTIAL and partial.attempted == (
"Transient failure",
)
assert abstain.outcome is Outcome.ABSTAIN and abstain.attempted == ()
assert serve((backup,), safe_excerpt=None, max_attempts=0).attempted == ()
borderline = Option("Exact threshold", True, True, ("Fixture answer", 0.65))
assert (
serve((borderline,), safe_excerpt=None, max_attempts=1).outcome
is Outcome.COMPLETE
)
no_answers = measure(rows, (0, 1, 2), 1.1)
assert no_answers.answered_accuracy is None
assert "answered accuracy=undefined" in format_benchmark(
"Always abstain", no_answers
)
for result in (complete, partial, abstain):
print(
f"{result.outcome.value} | tier={result.tier} | attempts={result.attempted}"
)ConsoleOut [20]Expand
complete | tier=Backup | attempts=('Backup',)
partial | tier=None | attempts=('Transient failure',)
abstain | tier=None | attempts=()These fixtures prove the route and outcome for their scripted conditions. They do not estimate real availability. If and are the failure events for two attempts, joint failure is when . If , joint failure is zero. It equals the product of marginal failure rates under independence. A shared network, region, or upstream dependency can make failure on the backup more likely given failure on the primary.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!