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

ModelRouterMiddleware Exposes Middleware-Generated Model_route In Agent Input Schema

langchain-typesafe's ModelRouterMiddleware adds model_route to the agent input JSON schema even though the middleware unconditionally overwrites any caller-provided value in before_agent. This advertises an input field that is silently ignored, causing schema-driven clients to pass a route choice that has no effect.

mediumConfidence 85%LangchainAffected V0.0.1a3

Origin Analysis

ModelRouterMiddleware contributes model_route to the agent input schema via its schema hook, but the same field is intended as internal state generated by the middleware. The underlying LangChain middleware contract allows middleware to advertise input fields, and this middleware incorrectly exposes a generated field as caller-controllable.
1. Create an agent with create_agent(model, middleware=[ModelRouterMiddleware(...)]). 2. Inspect agent.get_input_jsonschema()['properties']; observe 'model_route' is present. 3. Invoke agent with a valid ChoiceAnswer assigning 'model_route' to 'powerful'. 4. Observe output: classifier overrides the provided route, and the response is generated by the classifier-selected model (e.g., 'FAST' even though 'powerful' was requested).

Fixing Code Block

from typing import Any def get_input_jsonschema(self) -> dict[str, Any] | None: # `model_route` is middleware-generated state and must not be exposed as an input. return None
Override get_input_jsonschema to return None, removing the model_route property from the agent's input schema. The output schema continues to include model_route so consumers can inspect the selected route. This aligns the advertised interface with the middleware's actual behavior: model_route is read-only and generated internally.

Edge Case Audit

Clients that currently send model_route based on the incorrect input schema may now receive a validation error or have the field silently ignored depending on the agent's strictness. After applying the fix, test any schema-generated clients and remove model_route from their payloads. Rolling back this change restores the misleading schema; instead, keep the fix and update clients to the corrected contract.

Ecosystem Topology