跳到主要内容

LangSmithExporter

将 Tracing 数据发送到 LangSmith,用于可观测性。

构造函数
构造函数的直接链接

new LangSmithExporter(config: LangSmithExporterConfig)

LangSmithExporterConfig
langsmithexporterconfig的直接链接

interface LangSmithExporterConfig extends ClientConfig, BaseExporterConfig {
client?: Client
projectName?: string
}

同时扩展 ClientConfig(来自 LangSmith SDK)和 BaseExporterConfig

  • 来自 BaseExporterConfiglogger?: IMastraLoggerlogLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'
  • 来自 ClientConfigapiKeyapiUrlcallerOptionshideInputshideOutputs

方法
方法的直接链接

exportTracingEvent
exporttracingevent的直接链接

async exportTracingEvent(event: TracingEvent): Promise<void>

将 Tracing 事件导出到 LangSmith。

flush
flush的直接链接

async flush(): Promise<void>

强制将所有待处理的 Span 刷新到 LangSmith,而不关闭 Exporter。适用于需要确保运行时终止前已导出 Span 的 serverless 环境。

shutdown
shutdown的直接链接

async shutdown(): Promise<void>

结束所有活跃 Span 并清除 Trace map。

用法
用法的直接链接

import { LangSmithExporter } from '@mastra/langsmith'

const exporter = new LangSmithExporter({
apiKey: process.env.LANGSMITH_API_KEY,
projectName: 'my-project', // Optional: specify which project to send traces to
apiUrl: 'https://api.smith.langchain.com',
logLevel: 'info',
})

环境变量
环境变量的直接链接

变量说明
LANGSMITH_API_KEY你的 LangSmith API key
LANGCHAIN_PROJECTTrace 的默认项目名称(未指定 projectName 时使用)
LANGSMITH_BASE_URL自托管实例的 API URL

Span 类型映射
Span 类型映射的直接链接

Span 类型LangSmith 类型
MODEL_GENERATIONllm
MODEL_CHUNKllm
TOOL_CALLtool
MCP_TOOL_CALLtool
PROVIDER_TOOL_CALLtool
所有其他类型chain

withLangsmithMetadata
withlangsmithmetadata的直接链接

用于向 Tracing 选项添加 LangSmith 专用元数据的 helper 函数。

function withLangsmithMetadata(metadata: LangSmithMetadataInput): TracingOptionsUpdater

LangSmithMetadataInput
langsmithmetadatainput的直接链接

用法
用法的直接链接

import { buildTracingOptions } from '@mastra/observability'
import { withLangsmithMetadata } from '@mastra/langsmith'

// Route to a specific project
const tracingOptions = buildTracingOptions(withLangsmithMetadata({ projectName: 'my-project' }))

// Set multiple fields
const tracingOptions = buildTracingOptions(
withLangsmithMetadata({
projectName: 'my-project',
sessionId: 'user-123',
}),
)