Skip to main content

TypeSafe integrations

还没翻译译自英文(docs.langchain.com)本页 30 段都还没译

Jev is a System One model that reads natural language but returns typed answers and calibrated probabilities instead of generated text.

TypeSafeClassifier classifies state into typed decisions and probabilities. It accepts strings, structured JSON, and LangChain message objects. Use it for focused decisions such as routing a request, choosing a model, or checking whether a tool call is safe to run.

Setup

Install @langchain/typesafe and its @langchain/core peer dependency:

npmyarnpnpm

npm install @langchain/typesafe @langchain/core

Keep your API key on the server. Do not expose it in browser code. Set dangerouslyAllowBrowser: false to reject browser use, as in the quickstart below.

Create an API key in the TypeSafe console and export it:

export TYPESAFE_API_KEY=...

Note

Optional: Set TYPESAFE_BASE_URL to use a compatible gateway, test server, or private deployment. The default is https://api.typesafe.ai.

export TYPESAFE_BASE_URL=https://gateway.example.com

Quickstart

Configure TypeSafeClassifier with a fixed set of named questions. Questions that share state are evaluated independently and in parallel in one request:

import { TypeSafeClassifier } from "@langchain/typesafe";

const classifier = new TypeSafeClassifier({
  dangerouslyAllowBrowser: false,
  questions: {
    urgent: {
      type: "noul",
      instructions: "Does this need attention right now?",
    },
    team: {
      type: "choice",
      instructions: "Which team should pick this up?",
      criteria: {
        infra: "Deploys, availability, and on-call incidents.",
        billing: "Payments, invoices, and subscriptions.",
      },
    },
    severity: {
      type: "score",
      instructions: "How severe is the impact?",
      criteria: ["Cosmetic.", "Degraded for some users.", "Full outage."],
    },
  },
});

const response = await classifier.invoke(
  "The deploy failed twice and customers are seeing 500s. Can someone look now?"
);

console.log(response.nouls.urgent.noul);
console.log(response.choices.team.choice, response.choices.team.confidence);
console.log(response.scores.severity.score);

Answers use your question IDs as keys in response.answers. The nouls, choices, and scores accessors group answer objects by type. The response also includes model, usage, and an optional requestId. The usage.inputTokens and usage.outputTokens fields are optional.

The grouped accessors are non-enumerable getters. Object spread and JSON serialization omit them; use answers when storing or transmitting the response.

State can be a string, a JSON object or array, or LangChain messages. The classifier converts message objects to transcript text, including when nested inside a larger object.

Configure requests

Constructor options control authentication and request behavior:

  • apiKey: Overrides TYPESAFE_API_KEY.
  • baseUrl: Overrides TYPESAFE_BASE_URL.
  • timeout: Sets the per-request timeout in milliseconds. Defaults to 30000.
  • maxRetries: Sets the maximum number of retries. Defaults to 2; set it to 0 to disable retries.
  • fetch: Supplies a custom transport for proxies or tests.

Pass request configuration, such as signal, tags, and metadata, as the second argument to invoke. Questions and the model are constructor settings, not per-call overrides.

The classifier also supports batch and pipe. Its stream method yields the complete classification result, not incremental answers.

Handle errors

The package exports TypeSafeError, TypeSafeAPIError, TypeSafeAuthenticationError, and TypeSafeRateLimitError. Use their isInstance(error) methods to identify failures. Avoid logging API error bodies indiscriminately: they can contain submitted state.

TypeSafe receives the state you submit, including message content and tool-call arguments. Remove secrets or sensitive data before invoking the classifier.

Decision types

Each question uses one of three primitives. Define questions as objects with a type of "noul", "choice", or "score". The package does not export Noul, Choice, or Score constructors:

PrimitiveAsksReturnsReach for it when
noulIs this true?noul, the probability of yesYour code branches on an if
choiceWhich of these options?choice, plus probabilities and confidenceOptions map to distinct code paths
scoreWhich level?score, plus legend, probabilities, and confidenceThe answer is a spectrum you compare to a threshold

A noul answer has no confidence or probabilities. Use score rather than noul for a spectrum: a noul of 0.5 means an even split between yes and no, not "medium."

Tracing

With LangSmith tracing enabled, TypeSafe classifications appear in LangSmith. You can inspect the input, output, and available token usage alongside the rest of your agent. LangSmith records usage in the traced output rather than as a costed LLM metric.

See also