跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

LangSmithExporter

Sends Tracing data to LangSmith for observability.

Constructor
Constructor的直接链接

new LangSmithExporter(config: LangSmithExporterConfig)

LangSmithExporterConfig
langsmithexporterconfig的直接链接

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

Extends both ClientConfig (from LangSmith SDK) and BaseExporterConfig:

  • From BaseExporterConfig: logger?: IMastraLogger, logLevel?: LogLevel | 'debug' | 'info' | 'warn' | 'error'
  • From ClientConfig: apiKey, apiUrl, callerOptions, hideInputs, hideOutputs, etc.

Methods
Methods的直接链接

exportTracingEvent
exporttracingevent的直接链接

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

Exports a tracing event to LangSmith.

flush
flush的直接链接

async flush(): Promise<void>

Force flushes any pending spans to LangSmith without shutting down the exporter. Useful in serverless environments where you need to ensure spans are exported before the runtime terminates.

Waits for the LangSmith client's queued and in-flight batches. When the client uses manualFlushMode: true, this calls client.flush() for you, so you don't need to flush the client yourself. Overlapping calls wait for the earlier upload to finish.

shutdown
shutdown的直接链接

async shutdown(): Promise<void>

Ends all active spans and clears the trace map. Spans that are still open are ended with a shutdown error, and their updates are sent to LangSmith before the promise resolves.

Usage
Usage的直接链接

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',
})

Environment variables
Environment variables的直接链接

VariableDescription
LANGSMITH_API_KEYYour LangSmith API key
LANGCHAIN_PROJECTDefault project name for traces (used if projectName not specified)
LANGSMITH_BASE_URLAPI URL for self-hosted instances

Span type mapping
Span type mapping的直接链接

Span TypeLangSmith Type
MODEL_GENERATIONllm
MODEL_CHUNKllm
TOOL_CALLtool
MCP_TOOL_CALLtool
PROVIDER_TOOL_CALLtool
All otherschain

withLangsmithMetadata
withlangsmithmetadata的直接链接

Helper function to add LangSmith-specific metadata to tracing options.

function withLangsmithMetadata(metadata: LangSmithMetadataInput): TracingOptionsUpdater

LangSmithMetadataInput
langsmithmetadatainput的直接链接

Usage
Usage的直接链接

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',
}),
)