Jev + LangChain: TypeSafeClassifier and Agent Routing
Use Jev as a LangChain Runnable and distinguish classifier decisions from experimental agent middleware.
On this page
Install and configureComplete classifier examplePass LangChain messages as stateModel routing middlewareTool risk guardJev versus the agent LLMTracing and production checksInstall and configure
Install langchain-typesafe and provide TYPESAFE_API_KEY in the runtime environment. The integration exposes TypeSafeClassifier as a Runnable, so you can invoke, batch, and compose it with other LangChain components.
python -m pip install langchain-typesafe
Complete classifier example
from langchain_typesafe import TypeSafeClassifier, Choice, Score, Noul
classifier = TypeSafeClassifier(questions={
"department": Choice(instructions="Which team owns this ticket?",
criteria={"billing": "Payments and refunds", "technical": "Bugs", "other": "Neither"}),
"urgency": Score(instructions="How urgent is this?", criteria=["Routine", "This week", "Today"]),
"refund": Noul(instructions="Is a refund explicitly requested?")
})
response = classifier.invoke({
"ticket": "I was charged twice. Please refund the duplicate before Friday."
})
answer = response.choices["department"]
print({"department": answer.choice, "confidence": answer.confidence,
"urgency": response.scores["urgency"].score,
"refund_probability": response.nouls["refund"].noul})
Save this as classifier.py and run python classifier.py. It makes a live request; the example does not claim a measured response. Results are grouped by type under choices, scores, and nouls.
Pass LangChain messages as state
The adapter accepts strings, JSON structures, and LangChain message objects, including nested messages. It converts message roles and content into structured state. Keep only the relevant conversation window and remember that conversion does not make user instructions trusted.
Model routing middleware
The framework documents ModelRouterMiddleware in langchain_typesafe.experimental.middleware. It classifies a user message among named model choices and applies the selected model to a run. Install the experimental extra and the relevant generation-provider integration if using it; pin package versions because that API may change.
Tool risk guard
AutoModeMiddleware evaluates selected tool calls and refuses risky ones. According to the cited framework guide, it does not request human approval by itself. Pair it with a human-in-the-loop mechanism when approval is required, and keep host permissions and tool allowlists outside the classifier.
Only the tools you configure are classified. An omitted tool is not automatically protected. Review tool coverage, argument validation, and the handling of a model failure before enabling writes.
Jev versus the agent LLM
The classifier makes a narrow decision. The agent’s LLM generates content or proposes actions. Do not ask the classifier to perform generation, and do not let its label silently authorize an action. Use the risk-gate tutorial to inspect an independent deterministic boundary.
Tracing and production checks
The adapter records model usage and supports LangSmith tracing. Review what state is captured before enabling tracing on sensitive workloads. Log actual model, question revision, route, and reviewer correction; avoid persisting unnecessary private text.