@arizeai/openinference-tanstack-ai

中文文档
0.3.3发布于 2026/10/24Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 1.3万

OpenInference middleware for TanStack AI

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. Tracer Setup
  4. Custom Tracer
  5. What Gets Traced
  6. Examples
  7. Notes

OpenInference TanStack AI

npm version

This package provides an OpenInference middleware for TanStack AI. It emits OpenTelemetry spans shaped according to the OpenInference specification so TanStack AI runs can be visualized in systems like Arize AX and Phoenix.

Installation

npm install --save @arizeai/openinference-tanstack-ai @tanstack/ai

You will also need an OpenTelemetry setup in your application. For example:

npm install --save @arizeai/phoenix-otel

or:

npm install --save @opentelemetry/api @opentelemetry/sdk-trace-node @opentelemetry/exporter-trace-otlp-proto

Install the provider adapter you plan to use with TanStack AI as well, for example:

npm install --save @tanstack/ai-openai

Usage

@arizeai/openinference-tanstack-ai exports openInferenceMiddleware, which plugs directly into TanStack AI's middleware option.

import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";

import { openInferenceMiddleware } from "@arizeai/openinference-tanstack-ai";

const stream = chat({
  adapter: openaiText("gpt-4o-mini"),
  messages: [{ role: "user", content: "What is OpenInference?" }],
  middleware: [openInferenceMiddleware()],
});

The middleware works for both streaming and non-streaming TanStack AI calls.

const text = await chat({
  adapter: openaiText("gpt-4o-mini"),
  stream: false,
  systemPrompts: ["You are a concise technical explainer."],
  messages: [{ role: "user", content: "Explain OpenInference in one sentence." }],
  middleware: [openInferenceMiddleware()],
});

Tracer Setup

This package uses your application's existing OpenTelemetry tracer provider and exporters. It does not export spans by itself.

[!NOTE] Your instrumentation code should run before the middleware is applied. This ensures that the tracer provider is properly configured before the middleware starts emitting spans.

The recommended quick start is to pair it with @arizeai/phoenix-otel.

import { register } from "@arizeai/phoenix-otel";

register({
  projectName: "my-tanstack-ai-app",
  endpoint: process.env["PHOENIX_COLLECTOR_ENDPOINT"] ?? "http://localhost:6006/v1/traces",
  apiKey: process.env["PHOENIX_API_KEY"],
});

If you already have a standard OpenTelemetry setup, that works as well. For example, with a local Phoenix collector, a minimal manual setup looks like this:

import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { Resource } from "@opentelemetry/resources";
import { SimpleSpanProcessor } from "@opentelemetry/sdk-trace-base";
import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
import { SEMRESATTRS_PROJECT_NAME } from "@arizeai/openinference-semantic-conventions";

const tracerProvider = new NodeTracerProvider({
  resource: new Resource({
    [SEMRESATTRS_PROJECT_NAME]: "my-tanstack-ai-app",
  }),
  spanProcessors: [
    new SimpleSpanProcessor(
      new OTLPTraceExporter({
        url: process.env["PHOENIX_COLLECTOR_ENDPOINT"] ?? "http://localhost:6006/v1/traces",
        headers:
          process.env["PHOENIX_API_KEY"] == null
            ? undefined
            : {
                Authorization: `Bearer ${process.env["PHOENIX_API_KEY"]}`,
              },
      }),
    ),
  ],
});

tracerProvider.register();

Custom Tracer

By default, the middleware uses the global tracer for this package. If your application already has a request-scoped or custom tracer, pass it explicitly.

import { trace } from "@opentelemetry/api";

const tracer = trace.getTracer("tanstack-ai-request");

const middleware = openInferenceMiddleware({ tracer });

This is useful when you want the middleware to participate in a specific tracer setup without relying on the global default.

What Gets Traced

The middleware emits the following span structure for a TanStack AI run:

  • One AGENT span for the overall chat() invocation
  • One LLM span for each model turn
  • One TOOL span for each executed tool call

For a tool loop, the trace will typically look like:

  • AGENT
  • LLM 1
  • TOOL
  • LLM 2

The AGENT span captures the top-level request and final response. The LLM spans capture provider/model metadata, input messages, output messages, tool definitions, token counts, and the model's finish reason (llm.finish_reason) when provided. The TOOL spans capture tool names, arguments, outputs, and errors.

Examples

This package includes example files in examples/:

  • examples/chat-with-tools.ts - OpenAI example with one tool call
  • examples/anthropic-multi-tool.ts - Anthropic example with multiple tool calls
  • examples/non-streaming-chat.ts - Anthropic non-streaming example with a system prompt

See examples/README.md for setup and run commands.

Notes

  • This package is ESM-only because TanStack AI is ESM-only.
  • The middleware works in both server and client environments, but client/server trace stitching depends on your application's context propagation setup.

版本与发布记录

@arizeai/openinference-tanstack-ai 共发布 24 个版本,首次发布于 2026/04/28;过去 12 个月发布了 24 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.3latest2026/10/2414 天前
0.3.22026/10/021 个月前
0.3.12026/10/021 个月前
0.3.02026/10/011 个月前
0.2.132026/09/191 个月前
0.2.122026/09/151 个月前

关于 @arizeai/openinference-tanstack-ai 的常见问题

@arizeai/openinference-tanstack-ai 是什么?

OpenInference middleware for TanStack AI

如何安装 @arizeai/openinference-tanstack-ai?

在项目目录运行 npm install @arizeai/openinference-tanstack-ai,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@tanstack/ai(>=0.10.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@arizeai/openinference-tanstack-ai 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@arizeai/openinference-tanstack-ai 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@arizeai/openinference-tanstack-ai 可以商用吗?

@arizeai/openinference-tanstack-ai 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@arizeai/openinference-tanstack-ai 的下载量有多少?

根据 npm 官方下载统计,@arizeai/openinference-tanstack-ai 在 2026/10/07 – 2026/11/05 这 30 天内被下载 12,932 次,日均约 431 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@arizeai/openinference-tanstack-ai 还在维护吗?

最新版本 0.3.3 发布于 2026/10/24(14 天前);至今共发布 24 个版本,其中过去 12 个月发布了 24 个;首次发布于 2026/04/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。