Context
When using LangSmith with Cloudflare Workers, you may encounter issues with tracing not appearing in LangSmith or traces showing incomplete information. This is because Cloudflare Workers doesn't support standard environment variables like process.env, which affects how LangSmith tracing is configured.
Answer
To enable LangSmith tracing in Cloudflare Workers, you need to manually configure the tracing settings since Workers doesn't support LANGSMITH_TRACING=true as an environment variable.
Setting up traceable functions
When using traceable in Cloudflare Workers, you must manually pass tracingEnabled: true along with your client configuration:
const traceableFunction = traceable(() => {
// Your function logic here
}, {
project_name: projectName,
client: langsmithClient,
tracingEnabled: true,
});Customizing trace outputs
To make your traces more readable and consistent with other LangSmith integrations, use the processOutputs function to transform the raw output:
this.traceableGenerateText = traceable(generateText, {
name: "your-trace-name",
client: this.client,
project_name: "your-project",
tracingEnabled: true,
run_type: "llm",
processOutputs: ({ outputs }) => {
return {
messages: outputs.response.messages,
};
}
});Alternative approach with createRun/updateRun
While you can use createRun and updateRun directly, this approach is not recommended as it's more cumbersome and error-prone. The traceable wrapper is the preferred method as it handles many edge cases automatically, including proper error handling.
If you must use the direct API approach, ensure you call awaitPendingTraceBatches() after your updateRun call, not after createRun.
Creating nested traces
To create waterfall-style nested traces, wrap multiple traceable functions:
const outerTraceable = traceable(async () => {
const innerTraceable = traceable(/* inner function */);
return innerTraceable();
});Note: As of version 0.3.75, improved TypeScript typing support has been added for better development experience with processOutputs functions.