AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

ModelRouterMiddleware Throws Unclear KeyError When Classifier Returns Unconfigured Route

The ModelRouterMiddleware directly indexes self.models with classifier-provided choice, causing an opaque KeyError if the choice is not a configured route. This occurs for malformed or unexpected classifier responses, preventing clear error handling.

highConfidence 85%Langchain-TypesafeAffected V0.0.1a3

Origin Analysis

The wrap_model_call method (and its async counterpart) does not validate that the classifier response contains the expected 'model_route' answer or that the selected choice exists in the configured models dictionary before attempting to index. This leads to KeyError when the classifier output does not match configured routes.
1. Configure ModelRouterMiddleware with choices={'fast': ModelChoice(model=model, criteria='Simple tasks.')}. 2. Make classifier return ClassifierResponse with answers={'model_route': ChoiceAnswer(type='choice', choice='not-configured', ...)}. 3. Invoke agent with middleware. 4. Observe KeyError: 'not-configured' from self.models[answer.choice].

Fixing Code Block

Edge Case Audit

This change may raise ValueError for previously uncaught KeyError, which will surface as a different exception type. Ensure downstream code handles ValueError appropriately. Also, if the classifier legitimately returns a route not in the initial choices (e.g., dynamic routes), this fix would reject it; consider whether dynamic addition of routes is desired before merging. Rollback: revert to previous indexing code if unexpected breakage occurs.

Ecosystem Topology