core.result

On this page

Typed result objects produced by the routing pipeline: scored candidates, the full decision trace, and the final result objects returned to callers.

RouteCandidate

Python · 10 lines
@dataclass
class RouteCandidate:
    route_name:      str
    group_name:      Optional[str]
    combined_score:  float
    semantic_score:  float
    keyword_score:   float
    llm_score:       float
    confidence:      ConfidenceLevel
    score_bias:      float = 0.0

A scored route candidate surfaced by the RoutingPipeline.

Fields

FieldTypeDescription
route_namestrName of the candidate route.
group_nameOptional[str]Group the route belongs to.
combined_scorefloatThe weighted hybrid score as computed by HybridScorer, which already includes route.score_bias added in (per HybridScorer.score_all: effective = min(1.0, combined + route.score_bias) — this value is what gets stored as combined_score here).
semantic_scorefloatRaw semantic (embedding) similarity component.
keyword_scorefloatRaw keyword score component.
llm_scorefloatAlways 0.0 in candidates produced by RoutingPipeline.run — the LLM signal is folded into combined_score upstream by HybridScorer rather than reported separately here (see the pipeline source comment: "already folded into combined").
confidenceConfidenceLevelConfidence tier for combined_score.
score_biasfloatThe route's bias value at scoring time (duplicated for reference; already reflected in combined_score).

Properties

effective_score -> float

Returns min(1.0, self.combined_score + self.score_bias).

[!WARNING] Potential double-application of score_bias Since RoutingPipeline.run already stores HybridScorer's bias-inclusive result into combined_score, and score_bias is also stored alongside it, RouteCandidate.effective_score re-adds score_bias on top of an already bias-inclusive combined_score. Based on static analysis alone, this means effective_score may double-count the bias relative to the value HybridScorer originally computed. This is stated as an observation from reading the source, not a claim about intended behavior — verify against your version's actual scoring output if precise bias accounting matters for your use case.

Methods

to_dict(self) -> Dict[str, Any]

Returns a dict with rounded (4 decimal places) score fields: route, group, score (= effective_score), semantic, keyword, llm, confidence (.value string).

Related APIs: RoutingTrace, HybridScorer


RoutingTrace

Python · 15 lines
@dataclass
class RoutingTrace:
    request_id:        str
    query:             str
    candidates:        List[RouteCandidate]      = field(default_factory=list)
    selected_group:    Optional[str]              = None
    selected_route:    Optional[str]              = None
    confidence:        Optional[ConfidenceLevel]   = None
    used_llm_fallback: bool                        = False
    used_cache:        bool                        = False
    embedding_time_ms: float                       = 0.0
    scoring_time_ms:   float                       = 0.0
    execution_time_ms: float                       = 0.0
    total_time_ms:     float                       = 0.0
    notes:             List[str]                   = field(default_factory=list)

Full audit trail of a single routing decision — emitted regardless of whether a match was found.

Fields

FieldTypeDescription
request_idstrCorrelation ID, shared with the originating HandlerRequest.
querystrThe raw query text.
candidatesList[RouteCandidate]Ranked candidates surfaced by the pipeline (up to top_k).
selected_group / selected_routeOptional[str]The winning group/route, once known.
confidenceOptional[ConfidenceLevel]Confidence of the winning candidate.
used_llm_fallbackboolDeclared field; not observed to be set to True anywhere in RoutingPipeline.run or HybridScorer in the analyzed source — HybridScorer invokes the LLM scorer internally but does not communicate that fact back onto the trace via this field. Treat as informational/reserved unless you've confirmed otherwise for your version.
used_cacheboolSet to True explicitly by HierarchicalRouter.route_query / route_async on the cache-hit path.
embedding_time_ms, scoring_time_ms, execution_time_ms, total_time_msfloatTiming breakdown in milliseconds, populated at different stages of route_query/RoutingPipeline.run.
notesList[str]Free-text annotations (see add_note).

Methods

add_note(self, note: str)

Appends a string to self.notes. Used throughout RoutingPipeline and ExecutionEngine to record human-readable context (e.g. "Sequential: succeeded on 'docs_qa'.", "No group matched the query.").

to_dict(self) -> Dict[str, Any]

Returns a JSON-friendly summary: request_id, query (truncated to 120 chars), selected_group, selected_route, confidence, used_llm_fallback, used_cache, a timing_ms sub-dict (each value rounded to 2 decimals), the first 5 candidates (each via RouteCandidate.to_dict()), and notes.

[!NOTE] Trace serialization truncates candidates RoutingTrace.to_dict() includes only self.candidates[:5] regardless of ScoringConfig.top_k — if you need the full candidate list beyond 5, access trace.candidates directly rather than trace.to_dict()["candidates"].

Related APIs: RouteCandidate, RoutingResult


RoutingResult

Python · 5 lines
@dataclass
class RoutingResult:
    response:    Optional[HandlerResponse]
    trace:       RoutingTrace
    matched:     bool  = False

The final result object returned by HierarchicalRouter.route_query() / route_async() / route(). Bundles the handler's response with the full routing trace so callers can inspect why a route was chosen without additional instrumentation.

Fields

FieldTypeDescription
responseOptional[HandlerResponse]The executed handler's response, or None if nothing was executed.
traceRoutingTraceThe full decision audit trail.
matchedboolWhether a route was successfully matched and (attempted to be) executed.

Properties

PropertyReturnsDescription
contentAnyShortcut to response.content — None if response is None.
successboolbool(self.matched and self.response and self.response.success) — all three conditions must hold.
route_nameOptional[str]trace.selected_route.
group_nameOptional[str]trace.selected_group.
confidenceOptional[ConfidenceLevel]trace.confidence.
total_time_msfloattrace.total_time_ms.

Methods

to_dict(self) -> Dict[str, Any]

Returns {"matched", "success", "route", "group", "confidence", "content", "error", "time_ms", "trace"} — trace is the nested full RoutingTrace.to_dict() output.

Magic methods

  • __bool__ — returns self.success, so if result: means "matched and the handler succeeded."
  • __repr__ — if matched, shows route/group/confidence/time/success; otherwise shows RoutingResult(no_match, time=...ms).

Examples

1. Basic — check success before using content:

Python · 5 lines
result = router.route_query("What's the weather in Cairo?")
if result:
    print(result.content)
else:
    print("Routing failed:", result.response.error if result.response else "no response")

2. Intermediate — inspect the winning candidate's scores:

Python · 3 lines
if result.trace.candidates:
    best = result.trace.candidates[0]
    print(f"{best.route_name}: semantic={best.semantic_score:.3f} keyword={best.keyword_score:.3f}")

3. Advanced — full trace serialization for logging:

Python · 2 lines
import json
logger.info("routing_trace", extra={"trace": json.dumps(result.to_dict())})

4. Error handling — distinguishing "no match" from "handler failed":

Python · 6 lines
if not result.matched:
    print("No route matched:", result.trace.notes)
elif not result.success:
    print("Route matched but handler failed:", result.response.error)
else:
    print("Success:", result.content)

Related APIs: HandlerResponse, RoutingTrace, MultiRoutingResult


MultiRoutingResult

Python · 5 lines
@dataclass
class MultiRoutingResult:
    results:          List[RoutingResult]
    aggregated:       Optional[HandlerResponse]  = None
    trace:            Optional[RoutingTrace]     = None

Wraps results from multi-route (parallel) execution — produced by ExecutionEngine._execute_parallel / _execute_parallel_async.

Fields

FieldTypeDescription
resultsList[RoutingResult]One RoutingResult per executed candidate.
aggregatedOptional[HandlerResponse]The combined response, produced by a BaseAggregator (FirstWinsAggregator or MergeAggregator) or a first-success fallback.
traceOptional[RoutingTrace]The shared trace for the whole parallel batch.

Properties

  • success -> bool — bool(self.aggregated and self.aggregated.success).
  • content -> Any — self.aggregated.content if self.aggregated else None.

Methods

to_dict(self) -> Dict[str, Any]

Returns {"success", "content", "results" (list of each RoutingResult.to_dict()), "trace"}.

Related APIs: ExecutionEngine, BaseAggregator

Related pages